命令
获取python版本号
#!/usr/bin/python
import platform
print platform.python_version()
#########################################
#!/usr/bin/python
import sys
print sys.version
print sys.version_info
获取外部参数
sys.argv[0] # 获取本身,即此.py程序。
sys.argv[1] # 获取传入第一个参数。
a=sys.argv[2:] # 获取传入第二个及以后多个参数。
模块
SimpleHTTPServer - 快速搭建http服务
##启动服务
python -m SimpleHTTPServer 8000
##后台启动
nohup python -m SimpleHTTPServer 8000 &
##在Python 3已经合并到http.server模块中
python3 -m http.server
urillb2
在Pytho2.x 和 Pytho3.x中 模块名变化
在Pytho2.x中使用import urllib2——-对应的,在Python3.x中会使用import urllib.request,urllib.error。
在Pytho2.x中使用import urllib——-对应的,在Python3.x中会使用import urllib.request,urllib.error,urllib.parse
在Pytho2.x中使用import urlparse——-对应的,在Python3.x中会使用import urllib.parse。
在Pytho2.x中使用import urlopen——-对应的,在Python3.x中会使用import urllib.request.urlopen。
在Pytho2.x中使用import urlencode——-对应的,在Python3.x中会使用import urllib.parse.urlencode。
在Pytho2.x中使用import urllib.quote——-对应的,在Python3.x中会使用import urllib.request.quote。
在Pytho2.x中使用cookielib.CookieJar——-对应的,在Python3.x中会使用http.CookieJar。
在Pytho2.x中使用urllib2.Request——-对应的,在Python3.x中会使用urllib.request.Request。
configparser
ConfigParser模块在python中用来读取配置文件,可以包含一个或多个节(section), 每个节可以有多个参数(键=值)。
在python 3 中ConfigParser
模块名已更名为configparser
configparser函数常用方法:
读取配置文件:
1 read(filename) #读取配置文件,直接读取ini文件内容
2
3 sections() #获取ini文件内所有的section,以列表形式返回['logging', 'mysql']
4
5 options(sections) #获取指定sections下所有options ,以列表形式返回['host', 'port', 'user', 'password']
6
7 items(sections) #获取指定section下所有的键值对,[('host', '127.0.0.1'), ('port', '3306'), ('user', 'root'), ('password', '123456')]
8
9 get(section, option) #获取section中option的值,返回为string类型
10 >>>>>获取指定的section下的option <class 'str'> 127.0.0.1
11
12 getint(section,option) 返回int类型
13 getfloat(section, option) 返回float类型
14 getboolean(section,option) 返回boolen类型
jinja2
移除渲染后的空白(空格、制表符、换行符)等
在块(比如一个 for 标签、一段注释或变 量表达式)的开始或结束放置一个减号( - ),可以移除块前或块后的空白:
vip = ['10.110.10.130','10.110.10.131']
## 移除最后的空白
{% for i in vip -%}
{{ i }}
{% endfor -%}
提示: 标签和减号之间不能有空白。
## 有效的:
{%- if foo -%}...{% endif %}
## 无效的:
{% - if foo - %}...{% endif %}
功能
Session连接数
在同一Session中,requests库默认的连接池和最大连接数都是10,不管代码中并发写多大,都只有10个虚拟线程。
连接池可以理解为host,最大连接数可以理解为同一个host的连接数。
解决方法 session.mount()
s = requests.Session()
s.mount('https://', requests.adapters.HTTPAdapter(pool_connections=20, pool_maxsize=100))
s.mount('http://', requests.adapters.HTTPAdapter(pool_connections=20, pool_maxsize=100))
# http https 分别对应各自类型,只是需要分别设置
其中,HTTPAdapter()
默认参数为
pool_connections=10, pool_maxsize=10, max_retries=0, pool_block=False
pool_connections
是最多连接的不同host数,
pool_maxsize
同一host最多连接数。
正则匹配ip地址
import re
def match(x, pattern):
# 匹配字符串,如果包含则返回True
answer = pattern.search(x)
if answer == None:
return False
else : return True
# 只包含ip 形如x.x.x.x
ipre = re.compile(r'([0-9]{1,3}\.){3}[0-9]{1,3}$')
ip1df = addrDf[addrDf['地址段'].apply(lambda x: match(x,ipre))]
# 包含ip和掩码 形如 x.x.x.x/x
ip2df = addrDf[addrDf['地址段'].apply(lambda x: match(x,re.compile(r'([0-9]{1,3}\.){3}[0-9]{1,3}\/[0-9]{1,2}$') ))]
# 包含ip区间 形如 x.x.x.x~x 或 x.x.x.x-x
ip3df = addrDf[addrDf['地址段'].apply(lambda x: match(x,re.compile(r'([0-9]{1,3}\.){3}[0-9]{1,3}[\~|\-][0-9]{1,2}') ))]
# 正则提取数据 形如134.98.4.64~127(65)(sec) 提取为 x.x.x.x-x.x.x.x
matchObj = re.match(r'([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\.([0-9]+)\~([0-9]+)', '134.98.4.64~127(65)(sec)')
# print(matchObj.group(),matchObj.group(0),)
print(matchObj.group(1),matchObj.group(2),matchObj.group(3))
判断变量数据类型(int、字符串、列表、元组、字典)
#!/usr/bin/env python
a = [1,2,3,4,5]
if isinstance(a,int):
print "a is int"
elif isinstance(a,list):
print "a is list"
elif isinstance(a,tuple):
print "a is tuple"
elif isinstance(a,dict):
print "a is dict"
elif isinstance(a,str):
print "a is str"