Python学习笔记(四)中都有哪些?
摘要:一.Flask 1.1 什么是Flask Flask是由python实现的一个web微框架,可以使用Python语言快速实现一个网站或Web服务。 1.2.构造基本步骤 ste1.创建api对象; step2.生成resource子类,并定
一.Flask
1.1 什么是Flask
Flask是由python实现的一个web微框架,可以使用Python语言快速实现一个网站或Web服务。
1.2.构造基本步骤
ste1.创建api对象;
step2.生成resource子类,并定义该类的方法;
step3.绑定路由。
二.ConfigParser
ConfigParser 是用来读取配置文件的包。配置文件可以包含一个或多个节(section,中括号“[ ]”内包含的为section), 每个节可以有多个参数(键=值)。
注意:在python 3 中ConfigParser模块名已更名为configparser。
例如,操作读取的配置文件内容为
[db]
db_host = 110.110.110.110
db_port = 69
huaweiqq_uid = 119119119
huaweiqq_pwd = 120120120
host_port = 69
[concurrent]
thread = 10
processor = 2
2.1ConfigParser 初始化对象
使用ConfigParser 首选需要初始化实例,并读取配置文件:
import configparser
config = configparser.ConfigParser()
config.read("ini配置文件", encoding="utf-8")
2.2获取所用的section节点
接着上面的初始化后,才能操作
xxxxx = config.sections()
获取ini文件内所有的section,以列表形式返回['db', 'concurrent']
2.3 获取指定section 的options
即将配置文件某个section 内key 读取到列表中:
r = config.options("db")
获取指定sections下所有options ,以列表形式返回['host', 'port', 'huaweiqq_uid', 'huaweiqq_pwd']
2.4获取指点section下指点option的值
r = config.get("db", "db_host")
get(section, option) #获取section中option的值,返回为string类型.
本例的运行结果为110.110.110.110
2.5获取指点section的所用配置信息
r = config.items("db")
items(selections)#获取指定section下所有的键值对.
本例的运行结果为[('db_host', '110.110.110.110'), ('db_port', '69'), ('huaweiqq_uid', '119119119'), ('huaweiqq_pwd', '120120120), ('host_port', '69')]
2.6检查section或option是否存在,bool值
config.has_section("section") #是否存在该section
config.has_option("section", "option") #是否存在该option
2.7写入配置文件
添加/修改
add_section(section)添加一个新的section
set(section,option,value)对section中的option进行设置,如果不存在该option 则会创建。需要调用write将内容写入配置文件
config.write(open("ini配置文件", "w"))
删除
config.remove_section("????") #整个section下的所有内容都将删除;需要调用write将内容写入配置文件
config.write(open("ini", "w"))
三.调用shell命令常用方法
3.1使用os.popen()
该方法以文件的形式返回shell指令运行后的结果,然后再通过read()、readline() 或readlines()方法需要获取内容.
3.2使用commands模块
有以下三个方法可以使用:
(1)commands.getstatusoutput(cmd),其以字符串的形式返回的是输出结果和状态码,即(status,output)。
(2)commands.getoutput(cmd),返回cmd的输出结果。
(3)commands.getstatus(file),返回ls -l file的执行结果字符串,调用了getoutput,不建议使用此方法
3.3subprocess模块
允许创建很多子进程,创建的时候能指定子进程和子进程的输入、输出、错误输出管道,执行后能获取输出结果和执行状态。
四.正则表达式匹配IP地址
从string_ip 字符串变量中,提取处符合IP格式的数据。
string_ip= '????????????????????XXXXXXXXXXXXXXXXXX'
#精确提取IP
result = re.findall(r"\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b", string_ip):
if result:
print result
else:
print "re cannot find ip
五.mysql-connector驱动
有时候调试连接mysql的代码,会收到如下错误;
import MySQLdb
ModuleNotFoundError: No module named 'MySQLdb'
如果安装MySQLdb 感觉麻烦,我们可以使用mysql-connector来连接MySQL 数据库,mysql-connector是MySQL官方提供的驱动器。
引入import mysql.connector 。mysql.connector 和MySQLdb 很多方法相同,在一些简单的项目中,可能不需要修改代码或者修改很少。
去mysql的官网下载即可:https://dev.mysql.com/downloads/
下载网址https://dev.mysql.com/downloads/connector/python/
下载到本地,yum localinstall 文件 ,安装即可。
连接数据的代码如下:
import mysql.connector
cnx = mysql.connector.connect(user='scott', password='password',
host='127.0.0.1',
database='employees',port =3306)
cnx.close()
6.文件读取
readlines()用法:读取整个文件所有行,保存在一个列表(list)变量中,每行作为一个元素,但读取大文件会比较占内存。
readline()用法:该方法每次读出一行内容,所以,读取时占用内存小,比较适合大文件,该方法返回一个字符串对象。
read() 用法:从文件当前位置起读取size个字节,若无参数size,则表示读取至文件结束为止,它范围为字符串对象。
7.assert的功能用法
在 Python 中,assert 语句用于调试,主要用于判断一个条件是否为真。如果条件为真,程序将继续执行;如果条件为假,则会抛出一个 AssertionError 异常,您可以选择性地添加一个错误消息。
基本语法
assert condition, message
condition: 要测试的条件表达式。如果该表达式为False,则会触发断言失败。
message: 可选参数,当断言失败时提供的错误消息。
示例
Django 中rest_framework\generics.py的GenericAPIView类中的get_serializer_class方法。
其主要功能就是判断是否已经定义了serializer_class,即非空。
def get_serializer_class(self):
"""
Return the class to use for the serializer.
Defaults to using `self.serializer_class`.
You may want to override this if you need to provide different
serializations depending on the incoming request.
(Eg. admins get full serialization, others get basic serialization)
"""
assert self.serializer_class is not None, (
"'%s' should either include a `serializer_class` attribute, "
"or override the `get_serializer_class()` method."
% self.__class__.__name__
)
return self.serializer_class
参考学习
1.欢迎来到Flask的世界 —Flask中文文档 (2.0.1)
https://dormousehole.readthedocs.io/en/latest/
2.Python 之ConfigParser模块
https://www.cnblogs.com/ming5218/p/7965973.html
3.Python调用shell命令常用方法
https://www.cnblogs.com/pengpp/p/9833349.html
4.Python中匹配IP的正则表达式
https://www.cnblogs.com/brogong/p/7929298.html
5.Python正则表达式匹配IP地址
http://blog.sina.com.cn/s/blog_a1e9c7910102x4qp.html
6.Connecting to MySQL Using Connector/Python
https://dev.mysql.com/doc/connector-python/en/connector-python-example-connecting.html
