1. python实例手册
    2. #encoding:utf8
    3. # 设定编码-支持中文
    4. 0 说明
    5. 请使用"notepad++"或其它编辑器打开此文档, "alt+0"将函数折叠后方便查阅
    6. 请勿删除信息, 转载请说明出处, 抵制不道德行为
    7. 错误在所难免, 还望指正!
    8. [python实例手册] [shell实例手册] [LazyManage运维批量管理(shell/python两个版本)]
    9. github更新下载地址: https://github.com/liquanzhou/ops_doc
    10. 1 基础
    11. 安装python2.7
    12. wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
    13. tar xvf Python-2.7.9.tgz
    14. cd Python-2.7.9
    15. ./configure --prefix=/usr/local/python27
    16. make
    17. make install
    18. mv /usr/bin/python /usr/bin/python_old
    19. ln -s /usr/local/python27/bin/python /usr/bin/python
    20. python # 查看版本
    21. 解决YUM无法使用的问题
    22. vim /usr/bin/yum
    23. vim /usr/bin/repoquery
    24. 两文件首行#!/usr/bin/python 替换为老版本python #!/usr/bin/python2.6 注意可能为2.4
    25. pip模块安装
    26. yum install python-pip # centos安装pip
    27. sudo apt-get install python-pip # ubuntu安装pip
    28. pip官方安装脚本
    29. wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
    30. python get-pip.py
    31. pip编译安装
    32. # https://pypi.python.org/pypi/setuptools
    33. wget http://pypi.python.org/packages/source/s/setuptools/setuptools.tar.gz
    34. tar zxvf setuptools.tar.gz
    35. cd setuptools/
    36. python setup.py build
    37. python setup.py install
    38. # https://pypi.python.org/pypi/ez_setup
    39. tar zxvf ez_setup.tar.gz
    40. cd ez_setup/
    41. python setup.py build
    42. python setup.py install
    43. # https://pypi.python.org/pypi/pip
    44. tar zxvf pip.tar.gz
    45. cd pip/
    46. python setup.py build
    47. python setup.py install
    48. 加载环境变量
    49. vim /etc/profile
    50. export PATH=/usr/local/python27/bin:$PATH
    51. . /etc/profile
    52. pip freeze # 查看包版本
    53. pip install -r file # 安装包文件列表
    54. pip install Package # 安装包 pip install requests
    55. pip show --files Package # 查看安装包时安装了哪些文件
    56. pip show --files Package # 查看哪些包有更新
    57. pip install --upgrade Package # 更新一个软件包
    58. pip uninstall Package # 卸载软件包
    59. pip list # 查看pip安装的包及版本
    60. pip install django==1.5 # 指定版本安装
    61. pip install kafka-python -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
    62. python3安装
    63. yum install python36.x86_64 python36-pip
    64. 查看帮助
    65. python -c "help('modules')" # 查看python所有模块
    66. import os
    67. for i in dir(os):
    68. print i # 模块的方法
    69. help(os.path) # 方法的帮助
    70. python中关键字
    71. import keyword
    72. keyword.iskeyword(str) # 字符串是否为python关键字
    73. keyword.kwlist # 返回pytho所有关键字
    74. ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
    75. 调试
    76. python -m trace -t aaaaaa.py
    77. strace -p pid # 用系统命令跟踪系统调用
    78. 变量
    79. r=r'\n' # 输出时原型打印
    80. u=u'中文' # 定义为unicode编码
    81. global x # 全局变量
    82. a = 0 or 2 or 1 # 布尔运算赋值,a值为True既不处理后面,a值为2. None、字符串''、空元组()、空列表[],空字典{}、0、空字符串都是false
    83. name = raw_input("input:").strip() # 输入字符串变量
    84. num = int(raw_input("input:").strip()) # 输入字符串str转为int型
    85. locals() # 所有局部变量组成的字典
    86. locals().values() # 所有局部变量值的列表
    87. os.popen("date -d @{0} +'%Y-%m-%d %H:%M:%S'".format(12)).read() # 特殊情况引用变量 {0} 代表第一个参数
    88. 基于字典的字符串格式化
    89. params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
    90. "%(pwd)s" % params # 'secret'
    91. "%(pwd)s is not a good password for %(uid)s" % params # 'secret is not a good password for sa'
    92. "%(database)s of mind, %(database)s of body" % params # 'master of mind, master of body'
    93. 打印
    94. # 字符串 %s 整数 %d 浮点 %f 原样打印 %r
    95. print '字符串: %s 整数: %d 浮点: %f 原样打印: %r' % ('aa',2,1.0,'r')
    96. print 'abc', # 有逗号,代表不换行打印,在次打印会接着本行打印
    97. print '%-10s %s' % ('aaa','bbb') # 左对齐 占10个字符
    98. print '%10s %s' % ('aaa','bbb') # 右对齐 占10个字符
    99. 列表
    100. # 列表元素的个数最多 536870912
    101. shoplist = ['apple', 'mango', 'carrot', 'banana']
    102. shoplist[2] = 'aa'
    103. del shoplist[0]
    104. shoplist.insert(4,'www')
    105. shoplist.append('aaa')
    106. shoplist[::-1] # 倒着打印 对字符翻转串有效
    107. shoplist[2::3] # 从第二个开始每隔三个打印
    108. shoplist[:-1] # 排除最后一个
    109. '\t'.join(li) # 将列表转字符串 用字表符分割
    110. sys.path[1:1]=[5] # 在位置1前面插入列表中一个值
    111. list(set(['qwe', 'as', '123', '123'])) # 将列表通过集合去重复
    112. eval("['1','a']") # 将字符串当表达式求值,得到列表
    113. # enumerate 可得到每个值的对应位置
    114. for i, n in enumerate(['a','b','c']):
    115. print i,n
    116. 元组
    117. # 不可变
    118. zoo = ('wolf', 'elephant', 'penguin')
    119. 字典
    120. ab = { 'Swaroop' : 'swaroopch@byteofpython.info',
    121. 'Larry' : 'larry@wall.org',
    122. }
    123. ab['c'] = 80 # 添加字典元素
    124. del ab['Larry'] # 删除字典元素
    125. ab.keys() # 查看所有键值
    126. ab.values() # 打印所有值
    127. ab.has_key('a') # 查看键值是否存在
    128. ab.items() # 返回整个字典列表
    129. 复制字典
    130. a = {1: {1: 2, 3: 4}}
    131. b = a
    132. b[1][1] = 8888 # a和b都为 {1: {1: 8888, 3: 4}}
    133. import copy
    134. c = copy.deepcopy(a) # 再次赋值 b[1][1] = 9999 拷贝字典为新的字典,互不干扰
    135. a[2] = copy.deepcopy(a[1]) # 复制出第二个key,互不影响 {1: {1: 2, 3: 4},2: {1: 2, 3: 4}}
    136. 迭代器
    137. # 创建迭代接口,而不是原来的对象 支持字符串、列表和字典等序列对象
    138. i = iter('abcd')
    139. print i.next()
    140. s = {'one':1,'two':2,'three':3}
    141. m = iter(s)
    142. print m.next() # 迭代key
    143. 流程结构
    144. if判断
    145. # 布尔值操作符 and or not 实现多重判断
    146. if a == b:
    147. print '=='
    148. elif a < b:
    149. print b
    150. else:
    151. print a
    152. while循环
    153. while True:
    154. if a == b:
    155. print "=="
    156. break
    157. print "!="
    158. else:
    159. print 'over'
    160. count=0
    161. while(count<9):
    162. print count
    163. count += 1
    164. for循环
    165. sorted() # 返回一个序列(列表)
    166. zip() # 返回一个序列(列表)
    167. enumerate() # 返回循环列表序列 for i,v in enumerate(['a','b']):
    168. reversed() # 反序迭代器对象
    169. dict.iterkeys() # 通过键迭代
    170. dict.itervalues() # 通过值迭代
    171. dict.iteritems() # 通过键-值对迭代
    172. readline() # 文件迭代
    173. iter(obj) # 得到obj迭代器 检查obj是不是一个序列
    174. iter(a,b) # 重复调用a,直到迭代器的下一个值等于b
    175. for i in range(1, 5):
    176. print i
    177. else:
    178. print 'over'
    179. list = ['a','b','c','b']
    180. for i in range(len(list)):
    181. print list[i]
    182. for x, Lee in enumerate(list):
    183. print "%d %s Lee" % (x+1,Lee)
    184. # enumerate 使用函数得到索引值和对应值
    185. for i, v in enumerate(['tic', 'tac', 'toe']):
    186. print(i, v)
    187. 流程结构简写
    188. [ i * 2 for i in [8,-2,5]]
    189. [16,-4,10]
    190. [ i for i in range(8) if i %2 == 0 ]
    191. [0,2,4,6]
    192. tab补全
    193. # vim /usr/lib/python2.7/dist-packages/tab.py
    194. # python startup file
    195. import sys
    196. import readline
    197. import rlcompleter
    198. import atexit
    199. import os
    200. # tab completion
    201. readline.parse_and_bind('tab: complete')
    202. # history file
    203. histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
    204. 函数
    205. def printMax(a, b = 1):
    206. if a > b:
    207. print a
    208. return a
    209. else:
    210. print b
    211. return b
    212. x = 5
    213. y = 7
    214. printMax(x, y)
    215. def update(*args,**kwargs):
    216. p=''
    217. for i,t in kwargs.items():
    218. p = p+ '%s=%s,' %(i,str(t))
    219. sql = "update 'user' set (%s) where (%s)" %(args[0],p)
    220. print sql
    221. update('aaa',uu='uu',id=3)
    222. 模块
    223. # Filename: mymodule.py
    224. def sayhi():
    225. print 'mymodule'
    226. version = '0.1'
    227. # 使用模块中方法
    228. import mymodule
    229. from mymodule import sayhi, version
    230. mymodule.sayhi() # 使用模块中函数方法
    231. 装饰器
    232. # 为已存在的功能添加额外的功能,只在初始化脚本的时候执行一次
    233. #!/usr/bin/env python
    234. def deco(func):
    235. def wrapper(*args, **kwargs):
    236. print "Wrap start"
    237. func(*args, **kwargs)
    238. func(*args, **kwargs)
    239. print "Wrap end\n"
    240. return wrapper
    241. @deco
    242. def foo(x):
    243. print "In foo():"
    244. print "I have a para: %s" % x
    245. @deco
    246. def foo_dict(x,z='dict_para'):
    247. print "In foo_dict:"
    248. print "I have two para, %s and %s" % (x, z)
    249. if __name__ == "__main__":
    250. # 装饰器 @deco 等价于 foo = deco(foo)
    251. foo('x')
    252. foo_dict('x', z='dict_para')
    253. 结果
    254. Wrap start
    255. In foo():
    256. I have a para: x
    257. In foo():
    258. I have a para: x
    259. Wrap end
    260. Wrap start
    261. In foo_dict:
    262. I have two para, x and dict_para
    263. In foo_dict:
    264. I have two para, x and dict_para
    265. Wrap end
    266. 类对象的方法
    267. __xxx__ # 系统定义名字
    268. __init__ # 实例化初始化类的方法
    269. __all__ = ['xs'] # __all__ 用于模块import导入时限制,定义了只有all内指定的属性、方法、类可被导入,没定义则模块内的所有将被导入
    270. _xxx # _开头的为私有类,只有类对象和子类对象自己能访问到这些变量 不能用 from module import * 导入 class _status:
    271. __xxx # __开头的为类中的私有变量名,只有类对象自己能访问,连子类对象也不能访问到这个数据
    272. class Person:
    273. # 实例化初始化的方法
    274. def __init__(self, name ,age):
    275. self.name = name
    276. self.age = age
    277. print self.name
    278. # 有self此函数为方法
    279. def sayHi(self):
    280. print 'Hello, my name is', self.name
    281. # 对象消逝的时候被调用
    282. def __del__(self):
    283. print 'over'
    284. # 实例化对象
    285. p = Person('Swaroop',23)
    286. # 使用对象方法
    287. p.sayHi()
    288. # 继承
    289. class Teacher(Person):
    290. def __init__(self, name, age, salary):
    291. Person.__init__(self, name, age)
    292. self.salary = salary
    293. print '(Initialized Teacher: %s)' % self.name
    294. def tell(self):
    295. Person.tell(self)
    296. print 'Salary: "%d"' % self.salary
    297. t = Teacher('Mrs. Shrividya', 40, 30000)
    298. getattr(object,name,default)
    299. # 返回object的名称为name的属性的属性值,如果属性name存在,则直接返回其属性值.如果属性name不存在,则触发AttribetError异常或当可选参数default定义时返回default值
    300. class A:
    301. def __init__(self):
    302. self.name = 'zhangjing'
    303. def method(self):
    304. print"method print"
    305. Instance = A()
    306. print getattr(Instance, 'name', 'not find') # 如果Instance 对象中有属性name则打印self.name的值,否则打印'not find'
    307. print getattr(Instance, 'age', 'not find') # 如果Instance 对象中有属性age则打印self.age的值,否则打印'not find'
    308. print getattr(Instance, 'method', 'default') # 如果有方法method,否则打印其地址,否则打印default
    309. print getattr(Instance, 'method', 'default')() # 如果有方法method,运行函数并打印None否则打印default
    310. setattr(object,name,value)
    311. # 设置object的名称为name(type:string)的属性的属性值为value,属性name可以是已存在属性也可以是新属性。
    312. #等同多次 self.name = name 赋值 在外部可以直接把变量和值对应关系传进去
    313. #class Person:
    314. # def __init__(self, name ,age):
    315. # self.name = name
    316. # self.age = age
    317. config = {'name':'name','age','age'}
    318. class Configure(object):
    319. def __init__(self, config):
    320. self.register(config)
    321. def register(self, config):
    322. for key, value in config.items():
    323. if key.upper() == key:
    324. setattr(self, key, value)
    325. 模块包
    326. # 文件 ops/fileserver/__init__.py
    327. import readers
    328. import writers
    329. # 每个模块的包中,都有一个 __init__.py 文件,有了这个文件,才能导入这个目录下的module,在导入一个包时 import ops.fileserver ,实际上是导入了它的 __init__.py 文件,可以再 __init__.py 文件中再导入其他的包,或者模块。就不需要将所有的import语句写在一个文件里了,也可以减少代码量,不需要一个个去导入module了。
    330. # __init__.py 有一个重要的变量 __all__ 。有时会需要全部导入,from PackageName import * ,这时 import 就会把注册在包 __init__.py 文件中 __all__ 列表中的子模块和子包导入到当前作用域中来。如:
    331. __all__ = ["Module1", "Module2", "subPackage1", "subPackage2"]
    332. 执行模块类中的所有方法
    333. # moniItems.py
    334. import sys, time
    335. import inspect
    336. class mon:
    337. def __init__(self):
    338. self.data = dict()
    339. def run(self):
    340. return self.runAllGet()
    341. def getDisk(self):
    342. return 222
    343. def getCpu(self):
    344. return 111
    345. def runAllGet(self):
    346. for fun in inspect.getmembers(self, predicate=inspect.ismethod):
    347. print fun[0], fun[1]
    348. if fun[0][:3] == 'get':
    349. self.data[fun[0][3:]] = fun[1]()
    350. print self.data
    351. return self.data
    352. # 模块导入使用
    353. from moniItems import mon
    354. m = mon()
    355. m.runAllGet()
    356. 文件处理
    357. # 模式: 读'r' 写[清空整个文件]'w' 追加[文件需要存在]'a' 读写'r+' 二进制文件'b' 'rb','wb','rb+'
    358. 写文件
    359. i={'ddd':'ccc'}
    360. f = file('poem.txt', 'a')
    361. f.write("string")
    362. f.write(str(i))
    363. f.flush()
    364. f.close()
    365. 读文件
    366. f = file('/etc/passwd','r')
    367. c = f.read().strip() # 读取为一个大字符串,并去掉最后一个换行符
    368. for i in c.split('\n'): # 用换行符切割字符串得到列表循环每行
    369. print i
    370. f.close()
    371. 读文件1
    372. f = file('/etc/passwd','r')
    373. while True:
    374. line = f.readline() # 返回一行
    375. if len(line) == 0:
    376. break
    377. x = line.split(":") # 冒号分割定义序列
    378. #x = [ x for x in line.split(":") ] # 冒号分割定义序列
    379. #x = [ x.split("/") for x in line.split(":") ] # 先冒号分割,在/分割 打印x[6][1]
    380. print x[6],"\n",
    381. f.close()
    382. 读文件2
    383. f = file('/etc/passwd')
    384. c = f.readlines() # 读入所有文件内容,可反复读取,大文件时占用内存较大
    385. for line in c:
    386. print line.rstrip(),
    387. f.close()
    388. 读文件3
    389. for i in open('b.txt'): # 直接读取也可迭代,并有利于大文件读取,但不可反复读取
    390. print i,
    391. 追加日志
    392. log = open('/home/peterli/xuesong','a')
    393. print >> log,'faaa'
    394. log.close()
    395. with读文件
    396. # 自动关闭文件、线程锁的自动获取和释放等
    397. with open('a.txt') as f:
    398. for i in f:
    399. print i
    400. print f.read() # 打印所有内容为字符串
    401. print f.readlines() # 打印所有内容按行分割的列表
    402. 文件随机读写
    403. # 文件本没有换行,一切都是字符,文件也没有插入功能
    404. f.tell() # 当前读写位置
    405. f.read(5) # 读取5个字符并改变指针
    406. f.seek(5) # 改变用户态读写指针偏移位置,可做随机写
    407. f.seek(p,0) # 移动当文件第p个字节处,绝对位置
    408. f.seek(p,1) # 移动到相对于当前位置之后的p个字节
    409. f.seek(p,2) # 移动到相对文件尾之后的p个字节
    410. f.seek(0,2) # 指针指到尾部
    411. # 改变指针超出文件尾部,会造成文件洞,ll看占用较大,但du -sh却非常小
    412. f.read(65535) # 读取64K字节
    413. f.write("str") # 写会覆盖当前指针后的响应字符,无插入功能
    414. 内建函数
    415. dir(sys) # 显示对象的属性
    416. help(sys) # 交互式帮助
    417. int(obj) # 转型为整形
    418. str(obj) # 转为字符串
    419. len(obj) # 返回对象或序列长度
    420. open(file,mode) # 打开文件 #mode (r 读,w 写, a追加)
    421. range(0,3) # 返回一个整形列表
    422. raw_input("str:") # 等待用户输入
    423. type(obj) # 返回对象类型
    424. abs(-22) # 绝对值
    425. random # 随机数
    426. choice() # 随机返回给定序列的一个元素
    427. divmod(x,y) # 函数完成除法运算,返回商和余数。
    428. round(x[,n]) # 函数返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数
    429. strip() # 是去掉字符串两端多于空格,该句是去除序列中的所有字串两端多余的空格
    430. del # 删除列表里面的数据
    431. cmp(x,y) # 比较两个对象 #根据比较结果返回一个整数,如果x<y,则返回-1;如果x>y,则返回1,如果x==y则返回0
    432. max() # 字符串中最大的字符
    433. min() # 字符串中最小的字符
    434. sorted() # 对序列排序
    435. reversed() # 对序列倒序
    436. enumerate() # 返回索引位置和对应的值
    437. sum() # 总和
    438. list() # 变成列表可用于迭代
    439. eval('3+4') # 将字符串当表达式求值 得到7
    440. exec 'a=100' # 将字符串按python语句执行
    441. exec(a+'=new') # 将变量a的值作为新的变量
    442. tuple() # 变成元组可用于迭代 #一旦初始化便不能更改的数据结构,速度比list快
    443. zip(s,t) # 返回一个合并后的列表 s = ['11','22'] t = ['aa','bb'] [('11', 'aa'), ('22', 'bb')]
    444. isinstance(object,int) # 测试对象类型 int
    445. xrange([lower,]stop[,step]) # 函数与range()类似,但xrnage()并不创建列表,而是返回一个xrange对象
    446. 列表类型内建函数
    447. list.append(obj) # 向列表中添加一个对象obj
    448. list.count(obj) # 返回一个对象obj在列表中出现的次数
    449. list.extend(seq) # 把序列seq的内容添加到列表中
    450. list.index(obj,i=0,j=len(list)) # 返回list[k] == obj 的k值,并且k的范围在i<=k<j;否则异常
    451. list.insert(index.obj) # 在索引量为index的位置插入对象obj
    452. list.pop(index=-1) # 删除并返回指定位置的对象,默认是最后一个对象
    453. list.remove(obj) # 从列表中删除对象obj
    454. list.reverse() # 原地翻转列表
    455. list.sort(func=None,key=None,reverse=False) # 以指定的方式排序列表中成员,如果func和key参数指定,则按照指定的方式比较各个元素,如果reverse标志被置为True,则列表以反序排列
    456. 序列类型操作符
    457. seq[ind] # 获取下标为ind的元素
    458. seq[ind1:ind2] # 获得下标从ind1到ind2的元素集合
    459. seq * expr # 序列重复expr次
    460. seq1 + seq2 # 连接seq1和seq2
    461. obj in seq # 判断obj元素是否包含在seq中
    462. obj not in seq # 判断obj元素是否不包含在seq中
    463. 字符串类型内建方法
    464. string.expandtabs(tabsize=8) # tab符号转为空格 #默认8个空格
    465. string.endswith(obj,beg=0,end=len(staring)) # 检测字符串是否已obj结束,如果是返回True #如果beg或end指定检测范围是否已obj结束
    466. string.count(str,beg=0,end=len(string)) # 检测str在string里出现次数 f.count('\n',0,len(f)) 判断文件行数
    467. string.find(str,beg=0,end=len(string)) # 检测str是否包含在string中
    468. string.index(str,beg=0,end=len(string)) # 检测str不在string中,会报异常
    469. string.isalnum() # 如果string至少有一个字符并且所有字符都是字母或数字则返回True
    470. string.isalpha() # 如果string至少有一个字符并且所有字符都是字母则返回True
    471. string.isnumeric() # 如果string只包含数字字符,则返回True
    472. string.isspace() # 如果string包含空格则返回True
    473. string.isupper() # 字符串都是大写返回True
    474. string.islower() # 字符串都是小写返回True
    475. string.lower() # 转换字符串中所有大写为小写
    476. string.upper() # 转换字符串中所有小写为大写
    477. string.lstrip() # 去掉string左边的空格
    478. string.rstrip() # 去掉string字符末尾的空格
    479. string.replace(str1,str2) # 把string中的str1替换成str2,如果num指定,则替换不超过num次
    480. string.startswith(obj,beg=0,end=len(string)) # 检测字符串是否以obj开头
    481. string.zfill(width) # 返回字符长度为width的字符,原字符串右对齐,前面填充0
    482. string.isdigit() # 只包含数字返回True
    483. string.split("/") # 把string切片成一个列表
    484. ":".join(string.split()) # 以:作为分隔符,将所有元素合并为一个新的字符串
    485. 字典内建方法
    486. dict.clear() # 删除字典中所有元素
    487. dict copy() # 返回字典(浅复制)的一个副本
    488. dict.fromkeys(seq,val=None) # 创建并返回一个新字典,以seq中的元素做该字典的键,val做该字典中所有键对的初始值
    489. dict.get(key,default=None) # 对字典dict中的键key,返回它对应的值value,如果字典中不存在此键,则返回default值
    490. dict.has_key(key) # 如果键在字典中存在,则返回True 用in和not in代替
    491. dict.items() # 返回一个包含字典中键、值对元组的列表
    492. dict.keys() # 返回一个包含字典中键的列表
    493. dict.iter() # 方法iteritems()、iterkeys()、itervalues()与它们对应的非迭代方法一样,不同的是它们返回一个迭代子,而不是一个列表
    494. dict.pop(key[,default]) # 和方法get()相似.如果字典中key键存在,删除并返回dict[key]
    495. dict.setdefault(key,default=None) # 和set()相似,但如果字典中不存在key键,由dict[key]=default为它赋值
    496. dict.update(dict2) # 将字典dict2的键值对添加到字典dict
    497. dict.values() # 返回一个包含字典中所有值得列表
    498. dict([container]) # 创建字典的工厂函数。提供容器类(container),就用其中的条目填充字典
    499. len(mapping) # 返回映射的长度(键-值对的个数)
    500. hash(obj) # 返回obj哈希值,判断某个对象是否可做一个字典的键值
    501. 集合方法
    502. s.update(t) # 用t中的元素修改s,s现在包含s或t的成员 s |= t
    503. s.intersection_update(t) # s中的成员是共用属于s和t的元素 s &= t
    504. s.difference_update(t) # s中的成员是属于s但不包含在t中的元素 s -= t
    505. s.symmetric_difference_update(t) # s中的成员更新为那些包含在s或t中,但不是s和t共有的元素 s ^= t
    506. s.add(obj) # 在集合s中添加对象obj
    507. s.remove(obj) # 从集合s中删除对象obj;如果obj不是集合s中的元素(obj not in s),将引发KeyError错误
    508. s.discard(obj) # 如果obj是集合s中的元素,从集合s中删除对象obj
    509. s.pop() # 删除集合s中的任意一个对象,并返回它
    510. s.clear() # 删除集合s中的所有元素
    511. s.issubset(t) # 如果s是t的子集,则返回True s <= t
    512. s.issuperset(t) # 如果t是s的超集,则返回True s >= t
    513. s.union(t) # 合并操作;返回一个新集合,该集合是s和t的并集 s | t
    514. s.intersection(t) # 交集操作;返回一个新集合,该集合是s和t的交集 s & t
    515. s.difference(t) # 返回一个新集合,改集合是s的成员,但不是t的成员 s - t
    516. s.symmetric_difference(t) # 返回一个新集合,该集合是s或t的成员,但不是s和t共有的成员 s ^ t
    517. s.copy() # 返回一个新集合,它是集合s的浅复制
    518. obj in s # 成员测试;obj是s中的元素 返回True
    519. obj not in s # 非成员测试:obj不是s中元素 返回True
    520. s == t # 等价测试 是否具有相同元素
    521. s != t # 不等价测试
    522. s < t # 子集测试;s!=t且s中所有元素都是t的成员
    523. s > t # 超集测试;s!=t且t中所有元素都是s的成员
    524. 序列化
    525. #!/usr/bin/python
    526. import cPickle
    527. obj = {'1':['4124','1241','124'],'2':['12412','142','1241']}
    528. pkl_file = open('account.pkl','wb')
    529. cPickle.dump(obj,pkl_file)
    530. pkl_file.close()
    531. pkl_file = open('account.pkl','rb')
    532. account_list = cPickle.load(pkl_file)
    533. pkl_file.close()
    534. 文件对象方法
    535. file.close() # 关闭文件
    536. file.fileno() # 返回文件的描述符
    537. file.flush() # 刷新文件的内部缓冲区
    538. file.isatty() # 判断file是否是一个类tty设备
    539. file.next() # 返回文件的下一行,或在没有其他行时引发StopIteration异常
    540. file.read(size=-1) # 从文件读取size个字节,当未给定size或给定负值的时候,读取剩余的所有字节,然后作为字符串返回
    541. file.readline(size=-1) # 从文件中读取并返回一行(包括行结束符),或返回最大size个字符
    542. file.readlines(sizhint=0) # 读取文件的所有行作为一个列表返回
    543. file.xreadlines() # 用于迭代,可替换readlines()的一个更高效的方法
    544. file.seek(off, whence=0) # 在文件中移动文件指针,从whence(0代表文件起始,1代表当前位置,2代表文件末尾)偏移off字节
    545. file.tell() # 返回当前在文件中的位置
    546. file.truncate(size=file.tell()) # 截取文件到最大size字节,默认为当前文件位置
    547. file.write(str) # 向文件写入字符串
    548. file.writelines(seq) # 向文件写入字符串序列seq;seq应该是一个返回字符串的可迭代对象
    549. 文件对象的属性
    550. file.closed # 表示文件已被关闭,否则为False
    551. file.encoding # 文件所使用的编码 当unicode字符串被写入数据时,它将自动使用file.encoding转换为字节字符串;若file.encoding为None时使用系统默认编码
    552. file.mode # Access文件打开时使用的访问模式
    553. file.name # 文件名
    554. file.newlines # 未读取到行分隔符时为None,只有一种行分隔符时为一个字符串,当文件有多种类型的行结束符时,则为一个包含所有当前所遇到的行结束符的列表
    555. file.softspace # 为0表示在输出一数据后,要加上一个空格符,1表示不加
    556. 异常处理
    557. # try 中使用 sys.exit(2) 会被捕获,无法退出脚本,可使用 os._exit(2) 退出脚本
    558. class ShortInputException(Exception): # 继承Exception异常的类,定义自己的异常
    559. def __init__(self, length, atleast):
    560. Exception.__init__(self)
    561. self.length = length
    562. self.atleast = atleast
    563. try:
    564. s = raw_input('Enter something --> ')
    565. if len(s) < 3:
    566. raise ShortInputException(len(s), 3) # 触发异常
    567. except EOFError:
    568. print '\nWhy did you do an EOF on me?'
    569. except ShortInputException, x: # 捕捉指定错误信息
    570. print 'ShortInputException: %d | %d' % (x.length, x.atleast)
    571. except Exception as err: # 捕捉所有其它错误信息内容
    572. print str(err)
    573. #except urllib2.HTTPError as err: # 捕捉外部导入模块的错误
    574. #except: # 捕捉所有其它错误 不会看到错误内容
    575. # print 'except'
    576. finally: # 无论什么情况都会执行 关闭文件或断开连接等
    577. print 'finally'
    578. else: # 无任何异常 无法和finally同用
    579. print 'No exception was raised.'
    580. 不可捕获的异常
    581. NameError: # 尝试访问一个未申明的变量
    582. ZeroDivisionError: # 除数为零
    583. SyntaxErrot: # 解释器语法错误
    584. IndexError: # 请求的索引元素超出序列范围
    585. KeyError: # 请求一个不存在的字典关键字
    586. IOError: # 输入/输出错误
    587. AttributeError: # 尝试访问未知的对象属性
    588. ImportError # 没有模块
    589. IndentationError # 语法缩进错误
    590. KeyboardInterrupt # ctrl+C
    591. SyntaxError # 代码语法错误
    592. ValueError # 值错误
    593. TypeError # 传入对象类型与要求不符合
    594. 内建异常
    595. BaseException # 所有异常的基类
    596. SystemExit # python解释器请求退出
    597. KeyboardInterrupt # 用户中断执行
    598. Exception # 常规错误的基类
    599. StopIteration # 迭代器没有更多的值
    600. GeneratorExit # 生成器发生异常来通知退出
    601. StandardError # 所有的内建标准异常的基类
    602. ArithmeticError # 所有数值计算错误的基类
    603. FloatingPointError # 浮点计算错误
    604. OverflowError # 数值运算超出最大限制
    605. AssertionError # 断言语句失败
    606. AttributeError # 对象没有这个属性
    607. EOFError # 没有内建输入,到达EOF标记
    608. EnvironmentError # 操作系统错误的基类
    609. IOError # 输入/输出操作失败
    610. OSError # 操作系统错误
    611. WindowsError # windows系统调用失败
    612. ImportError # 导入模块/对象失败
    613. KeyboardInterrupt # 用户中断执行(通常是ctrl+c)
    614. LookupError # 无效数据查询的基类
    615. IndexError # 序列中没有此索引(index)
    616. KeyError # 映射中没有这个键
    617. MemoryError # 内存溢出错误(对于python解释器不是致命的)
    618. NameError # 未声明/初始化对象(没有属性)
    619. UnboundLocalError # 访问未初始化的本地变量
    620. ReferenceError # 若引用试图访问已经垃圾回收了的对象
    621. RuntimeError # 一般的运行时错误
    622. NotImplementedError # 尚未实现的方法
    623. SyntaxError # python语法错误
    624. IndentationError # 缩进错误
    625. TabError # tab和空格混用
    626. SystemError # 一般的解释器系统错误
    627. TypeError # 对类型无效的操作
    628. ValueError # 传入无效的参数
    629. UnicodeError # Unicode相关的错误
    630. UnicodeDecodeError # Unicode解码时的错误
    631. UnicodeEncodeError # Unicode编码时的错误
    632. UnicodeTranslateError # Unicode转换时错误
    633. Warning # 警告的基类
    634. DeprecationWarning # 关于被弃用的特征的警告
    635. FutureWarning # 关于构造将来语义会有改变的警告
    636. OverflowWarning # 旧的关于自动提升为长整形的警告
    637. PendingDeprecationWarning # 关于特性将会被废弃的警告
    638. RuntimeWarning # 可疑的运行时行为的警告
    639. SyntaxWarning # 可疑的语法的警告
    640. UserWarning # 用户代码生成的警告
    641. 触发异常
    642. raise exclass # 触发异常,从exclass生成一个实例(不含任何异常参数)
    643. raise exclass() # 触发异常,但现在不是类;通过函数调用操作符(function calloperator:"()")作用于类名生成一个新的exclass实例,同样也没有异常参数
    644. raise exclass, args # 触发异常,但同时提供的异常参数args,可以是一个参数也可以是元组
    645. raise exclass(args) # 触发异常,同上
    646. raise exclass, args, tb # 触发异常,但提供一个跟踪记录(traceback)对象tb供使用
    647. raise exclass,instance # 通过实例触发异常(通常是exclass的实例)
    648. raise instance # 通过实例触发异常;异常类型是实例的类型:等价于raise instance.__class__, instance
    649. raise string # 触发字符串异常
    650. raise string, srgs # 触发字符串异常,但触发伴随着args
    651. raise string,args,tb # 触发字符串异常,但提供一个跟踪记录(traceback)对象tb供使用
    652. raise # 重新触发前一个异常,如果之前没有异常,触发TypeError
    653. 跟踪异常栈
    654. # traceback 获取异常相关数据都是通过sys.exc_info()函数得到的
    655. import traceback
    656. import sys
    657. try:
    658. s = raw_input()
    659. print int(s)
    660. except ValueError:
    661. # sys.exc_info() 返回值是元组,第一个exc_type是异常的对象类型,exc_value是异常的值,exc_tb是一个traceback对象,对象中包含出错的行数、位置等数据
    662. exc_type, exc_value, exc_tb = sys.exc_info()
    663. print "\n%s \n %s \n %s\n" %(exc_type, exc_value, exc_tb )
    664. traceback.print_exc() # 打印栈跟踪信息
    665. 抓取全部错误信息存如字典
    666. import sys, traceback
    667. try:
    668. s = raw_input()
    669. int(s)
    670. except:
    671. exc_type, exc_value, exc_traceback = sys.exc_info()
    672. traceback_details = {
    673. 'filename': exc_traceback.tb_frame.f_code.co_filename,
    674. 'lineno' : exc_traceback.tb_lineno,
    675. 'name' : exc_traceback.tb_frame.f_code.co_name,
    676. 'type' : exc_type.__name__,
    677. 'message' : exc_value.message,
    678. }
    679. del(exc_type, exc_value, exc_traceback)
    680. print traceback_details
    681. f = file('test1.txt', 'a')
    682. f.write("%s %s %s %s %s\n" %(traceback_details['filename'],traceback_details['lineno'],traceback_details['name'],traceback_details['type'],traceback_details['message'], ))
    683. f.flush()
    684. f.close()
    685. 调试log
    686. # cgitb覆盖了默认sys.excepthook全局异常拦截器
    687. def func(a, b):
    688. return a / b
    689. if __name__ == '__main__':
    690. import cgitb
    691. cgitb.enable(format='text')
    692. func(1, 0)
    693. 函数式编程的内建函数
    694. apply(func[,nkw][,kw]) # 用可选的参数来调用func,nkw为非关键字参数,kw为关键字参数;返回值是函数调用的返回值
    695. filter(func,seq) # 调用一个布尔函数func来迭代遍历每个seq中的元素;返回一个使func返回值为true的元素的序列
    696. map(func,seq1[,seq2]) # 将函数func作用于给定序列(s)的每个元素,并用一个列表来提供返回值;如果func为None,func表现为一个身份函数,返回一个含有每个序列中元素集合的n个元组的列表
    697. reduce(func,seq[,init]) # 将二元函数作用于seq序列的元素,每次携带一堆(先前的结果以及下一个序列元素),连续地将现有的结果和下一个值作用在获得的随后的结果上,最后减少我们的序列为一个单一的返回值;如果初始值init给定,第一个比较会是init和第一个序列元素而不是序列的头两个元素
    698. lambda x,y:x+y # 创建一个匿名函数 可用于上面几种方法中直接创建匿名函数式
    699. # filter 即通过函数方法只保留结果为真的值组成列表
    700. def f(x): return x % 2 != 0 and x % 3 != 0
    701. f(3) # 函数结果是False 3被filter抛弃
    702. f(5) # 函数结果是True 5被加入filter最后的列表结果
    703. filter(f, range(2, 25))
    704. [5, 7, 11, 13, 17, 19, 23]
    705. # map 通过函数对列表进行处理得到新的列表
    706. def cube(x): return x*x*x
    707. map(cube, range(1, 11))
    708. [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
    709. # reduce 通过函数会先接收初始值和序列的第一个元素,然后是返回值和下一个元素,依此类推
    710. def add(x,y): return x+y
    711. reduce(add, range(1, 11)) # 结果55 是1到10的和 x的值是上一次函数返回的结果,y是列表中循环的值
    712. reduce(lambda x,y:x+y, range(1,11)) # 等同上面两条 lambda来创建匿名函数[ lambda x,y:x+y ] ,后面跟可迭代的对象
    713. 编码转换
    714. a='中文' # 编码未定义按输入终端utf8或gbk
    715. u=u'中文' # 定义为unicode编码 u值为 u'\u4e2d\u6587'
    716. u.encode('utf8') # 转为utf8格式 u值为 '\xe4\xb8\xad\xe6\x96\x87'
    717. print u # 结果显示 中文
    718. print u.encode('utf8') # 转为utf8格式,当显示终端编码为utf8 结果显示 中文 编码不一致则乱码
    719. print u.encode('gbk') # 当前终端为utf8 故乱码
    720. ord('4') # 字符转ASCII码
    721. chr(52) # ASCII码转字符
    722. 设置读取编码为utf8 避免转换出错
    723. #!/usr/bin/env python
    724. # -*- coding: utf-8 -*-
    725. import sys
    726. reload(sys)
    727. sys.setdefaultencoding('utf-8')
    728. 遍历递归
    729. [os.path.join(x[0],y) for x in os.walk('/root/python/5') for y in x[2]]
    730. for i in os.walk('/root/python/5/work/server'):
    731. print i
    732. 元类
    733. # 实现动态curd类的或者实例中的方法属性
    734. #!/usr/bin/env python
    735. # -*- coding:utf-8 -*-
    736. # Name: metaclass.py
    737. # Author: ZhiPeng Wang.
    738. # Created: 15/8/12
    739. # Copyright: (c) TigerJoys-SA 2015
    740. # -----------------------------------------------------------------------------
    741. """首先检查__metaclass__属性, 如果设置了此属性, 如果设置了此属性则调用对应Metaclass,
    742. Metaclass本身也是Class 当调用时先调用自身的__new__方法新建一个Instance然后Instance调
    743. 用__init__返回一个新对象(MyClss), 然后正常执行原Class
    744. """
    745. ext_attr = {
    746. 'wzp': 'wzp',
    747. 'test': 'test',
    748. }
    749. class CustomMeta(type):
    750. build_in_attr = ['name', ]
    751. def __new__(cls, class_name, bases, attributes):
    752. # 获取`Meta` Instance
    753. attr_meta = attributes.pop('Meta', None)
    754. if attr_meta:
    755. for attr in cls.build_in_attr: # 遍历内置属性
    756. # 自省, 获取Meta Attributes 不是build_in_attr的属性不处理
    757. print "Meta:", getattr(attr_meta, attr, False)
    758. # 扩展属性
    759. attributes.update(ext_attr)
    760. return type.__new__(cls, class_name, bases, attributes)
    761. def __init__(cls, class_name, bases, attributes):
    762. super(CustomMeta, cls).__init__(class_name, bases, attributes)
    763. class MyClass(object):
    764. __metaclass__ = CustomMeta # metaclass
    765. class Meta:
    766. name = 'Meta attr'
    767. if __name__ == '__main__':
    768. # TODO 此处返回一个类`Instance`对象
    769. print MyClass()
    770. # TODO 此处返回一个类对象, 并不是`Instance`
    771. print type("MyClass", (), {})
    772. 2 常用模块
    773. sys [系统操作模块]
    774. sys.argv # 取参数列表
    775. sys.exit(2) # 退出脚本返回状态 会被try截取
    776. sys.exc_info() # 获取当前正在处理的异常类
    777. sys.version # 获取Python解释程序的版本信息
    778. sys.maxint # 最大的Int值 9223372036854775807
    779. sys.maxunicode # 最大的Unicode值
    780. sys.modules # 返回系统导入的模块字段,key是模块名,value是模块
    781. sys.path # 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
    782. sys.platform # 返回操作系统平台名称
    783. sys.stdout # 标准输出
    784. sys.stdin # 标准输入
    785. sys.stderr # 错误输出
    786. sys.exec_prefix # 返回平台独立的python文件安装的位置
    787. sys.stdin.readline() # 从标准输入读一行
    788. sys.stdout.write("a") # 屏幕输出a
    789. sys.path.insert(1, os.path.join(sys.path[0], '/opt/script/')) # 将/opt/script/目录加入环境变量,可导入相应模块
    790. commands [执行系统操作]
    791. (status, output) = commands.getstatusoutput('cat /proc/cpuinfo')
    792. print status, output
    793. os [系统模块]
    794. # 相对sys模块 os模块更为底层 os._exit() try无法抓取
    795. os.popen('id').read() # 执行系统命令得到返回结果
    796. os.system() # 得到返回状态 返回无法截取
    797. os.name # 返回系统平台 Linux/Unix用户是'posix'
    798. os.getenv() # 读取环境变量
    799. os.environ['A']='1' # 设置环境变量
    800. os.getcwd() # 当前工作路径
    801. os.chdir() # 改变当前工作目录
    802. os.walk('/root/') # 递归路径
    803. os.environ['HOME'] # 查看系统环境变量
    804. os.statvfs("/") # 获取磁盘信息
    805. 文件处理
    806. mkfifo()/mknod() # 创建命名管道/创建文件系统节点
    807. remove()/unlink() # 删除文件
    808. rename()/renames() # 重命名文件
    809. stat() # 返回文件信息
    810. symlink() # 创建符号链接
    811. utime() # 更新时间戳
    812. tmpfile() # 创建并打开('w+b')一个新的临时文件
    813. walk() # 遍历目录树下的所有文件名
    814. oct(os.stat('th1.py').st_mode)[-3:] # 查看目录权限
    815. 目录/文件夹
    816. chdir()/fchdir() # 改变当前工作目录/通过一个文件描述符改变当前工作目录
    817. chroot() # 改变当前进程的根目录
    818. listdir() # 列出指定目录的文件
    819. getcwd()/getcwdu() # 返回当前工作目录/功能相同,但返回一个unicode对象
    820. mkdir()/makedirs() # 创建目录/创建多层目录
    821. rmdir()/removedirs() # 删除目录/删除多层目录
    822. 访问/权限
    823. saccess() # 检验权限模式
    824. chmod('txt',eval("0777")) # 改变权限模式
    825. chown()/lchown() # 改变owner和groupID功能相同,但不会跟踪链接
    826. umask() # 设置默认权限模式
    827. 文件描述符操作
    828. open() # 底层的操作系统open(对于稳健,使用标准的内建open()函数)
    829. read()/write() # 根据文件描述符读取/写入数据 按大小读取文件部分内容
    830. dup()/dup2() # 复制文件描述符号/功能相同,但是复制到另一个文件描述符
    831. 设备号
    832. makedev() # 从major和minor设备号创建一个原始设备号
    833. major()/minor() # 从原始设备号获得major/minor设备号
    834. os.path模块
    835. os.path.expanduser('~/.ssh/key') # 家目录下文件的全路径
    836. 分隔
    837. os.path.basename() # 去掉目录路径,返回文件名
    838. os.path.dirname() # 去掉文件名,返回目录路径
    839. os.path.join() # 将分离的各部分组合成一个路径名
    840. os.path.spllt() # 返回(dirname(),basename())元组
    841. os.path.splitdrive() # 返回(drivename,pathname)元组
    842. os.path.splitext() # 返回(filename,extension)元组
    843. 信息
    844. os.path.getatime() # 返回最近访问时间
    845. os.path.getctime() # 返回文件创建时间
    846. os.path.getmtime() # 返回最近文件修改时间
    847. os.path.getsize() # 返回文件大小(字节)
    848. 查询
    849. os.path.exists() # 指定路径(文件或目录)是否存在
    850. os.path.isabs() # 指定路径是否为绝对路径
    851. os.path.isdir() # 指定路径是否存在且为一个目录
    852. os.path.isfile() # 指定路径是否存在且为一个文件
    853. os.path.islink() # 指定路径是否存在且为一个符号链接
    854. os.path.ismount() # 指定路径是否存在且为一个挂载点
    855. os.path.samefile() # 两个路径名是否指向同一个文件
    856. 子进程
    857. os.fork() # 创建子进程,并复制父进程所有操作 通过判断pid = os.fork() 的pid值,分别执行父进程与子进程操作,0为子进程
    858. os.wait() # 等待子进程结束
    859. 跨平台os模块属性
    860. linesep # 用于在文件中分隔行的字符串
    861. sep # 用来分隔文件路径名字的字符串
    862. pathsep # 用于分割文件路径的字符串
    863. curdir # 当前工作目录的字符串名称
    864. pardir # 父目录字符串名称
    865. 磁盘空间
    866. import os
    867. disk = os.statvfs("/")
    868. # disk.f_bsize 块大小
    869. # disk.f_blocks 块总数
    870. # disk.f_bfree 剩余块总数
    871. # disk.f_bavail 非root用户的剩余块数 由于权限小会比root的剩余块总数小 用这个做报警会更准确
    872. # disk.f_files 总节点数
    873. # disk.f_ffree 剩余节点数
    874. # disk.f_favail 非root用户的剩余节点数
    875. disk.f_bsize * disk.f_bavail / 1024 / 1024 / 1024 # 非root用户剩余空间大小G
    876. disk.f_bsize * disk.f_blocks / 1024 / 1024 / 1024 # 分区空间总大小
    877. commands [执行系统命令]
    878. (status, output) = commands.getstatusoutput('cat /proc/cpuinfo')
    879. print status, output
    880. commands.getstatusoutput('id') # 返回元组(状态,标准输出)
    881. commands.getoutput('id') # 只返回执行的结果, 忽略返回值
    882. commands.getstatus('file') # 返回ls -ld file执行的结果
    883. re [perl风格正则]
    884. compile(pattern,flags=0) # 对正则表达式模式pattern进行编译,flags是可选标识符,并返回一个regex对象
    885. match(pattern,string,flags=0) # 尝试用正则表达式模式pattern匹配字符串string,flags是可选标识符,如果匹配成功,则返回一个匹配对象;否则返回None
    886. search(pattern,string,flags=0) # 在字符串string中搜索正则表达式模式pattern的第一次出现,flags是可选标识符,如果匹配成功,则返回一个匹配对象;否则返回None
    887. findall(pattern,string[,flags]) # 在字符串string中搜索正则表达式模式pattern的所有(非重复)出现:返回一个匹配对象的列表 # pattern=u'\u4e2d\u6587' 代表UNICODE
    888. finditer(pattern,string[,flags]) # 和findall()相同,但返回的不是列表而是迭代器;对于每个匹配,该迭代器返回一个匹配对象
    889. split(pattern,string,max=0) # 根据正则表达式pattern中的分隔符把字符string分割为一个列表,返回成功匹配的列表,最多分割max次(默认所有)
    890. sub(pattern,repl,string,max=0) # 把字符串string中所有匹配正则表达式pattern的地方替换成字符串repl,如果max的值没有给出,则对所有匹配的地方进行替换(subn()会返回一个表示替换次数的数值)
    891. group(num=0) # 返回全部匹配对象(或指定编号是num的子组)
    892. groups() # 返回一个包含全部匹配的子组的元组(如果没匹配成功,返回一个空元组)
    893. 零宽断言
    894. str = 'aaa111aaa , bbb222&, 333ccc'
    895. re.compile('\d+(?=[a-z]+)').findall(str) # 前向界定 (?=exp) 找出连续的数字并且最后一个数字跟着至少一个a-z ['111', '333']
    896. re.compile(r"\d+(?![a-z]+)").findall(str) # 前向否定界定 (?!exp) 找出连续数字,且最后一个数字后不能跟a-z ['11', '222', '33']
    897. re.compile(r"(?<=[a-z])\d+").findall(str) # 反向界定 (?<=exp) 逆序环视 找出连续的数字,且第一个数字前面是a-z ['111', '222']
    898. re.compile(r"(?<![a-z])\d+").findall(str) # 反向否定界定 (?<!exp) 否定逆序环视 找出连续的数字,且第一个数字前不能是a-z ['11', '22', '333']
    899. re.compile(r"(?:\d+)").findall(str) # 无捕获的匹配 (?:exp)
    900. s= 'Tom:9527 , Sharry:0003 '
    901. re.match( r'(?P<name>\w+):(?P<num>\d+)' , s).group(0) # 捕获组 <num>第二个标签变量[9527] 获取 group("num") 等同 group(2)[9527], group(0)全部[Tom:9527]
    902. 例子
    903. re.findall(r'a[be]c','123abc456eaec789') # 返回匹配对象列表 ['abc', 'aec']
    904. re.findall("(.)12[34](..)",a) # 取出匹配括号中内容 a='qedqwe123dsf'
    905. re.search("(.)123",a ).group(1) # 搜索匹配的取第1个标签
    906. re.match("^(1|2) *(.*) *abc$", str).group(2) # 取第二个标签
    907. re.match("^(1|2) *(.*) *abc$", str).groups() # 取所有标签
    908. re.sub('[abc]','A','alex') # 替换
    909. for i in re.finditer(r'\d+',s): # 迭代
    910. print i.group(),i.span() #
    911. 搜索网页中UNICODE格式的中文
    912. QueryAdd='http://www.anti-spam.org.cn/Rbl/Query/Result'
    913. Ip='222.129.184.52'
    914. s = requests.post(url=QueryAdd, data={'IP':Ip})
    915. re.findall(u'\u4e2d\u56fd', s.text, re.S)
    916. csv [访问csv逗号分隔的文件]
    917. csv读配置文件
    918. 192.168.1.5,web # 配置文件按逗号分割
    919. list = csv.reader(file('a.csv'))
    920. for line in list:
    921. print line # ['192.168.1.5', 'web']
    922. csv配合with读文件
    923. import csv
    924. with open('some.csv', 'rb') as f:
    925. reader = csv.reader(f)
    926. for row in reader:
    927. print row
    928. csv配合with写文件
    929. import csv
    930. with open('some.csv', 'wb') as f:
    931. writer = csv.writer(f)
    932. writer.writerow(['Column1', 'Column2', 'Column3']) # 写单行 列表
    933. writer.writerows([range(3) for i in range(5)]) # 写多行 列表套列表
    934. shutil [提供高级文件访问功能]
    935. import shutil
    936. shutil.copyfile('data.db', 'archive.db') # 拷贝文件
    937. shutil.move('/build/executables', 'installdir') # 移动文件或目录
    938. dircache [目录文件列表缓存]
    939. import dircache
    940. a = dircache.listdir('/data/xuesong') # 列出目录下所有的文件和目录
    941. dircache.annotate('/data/xuesong', a) # 判断指定目录下的是文件还是目录,目录则后面加/ 文件或不存在则不改变
    942. glob [文件通配符]
    943. import glob
    944. glob.glob('*.py') # 查找当前目录下py结尾的文件
    945. random [随机模块]
    946. import random
    947. random.choice(['apple', 'pear', 'banana']) # 随机取列表一个参数
    948. random.sample(xrange(100), 10) # 不重复抽取10个
    949. random.random() # 随机浮点数
    950. random.randrange(6) # 随机整数范围
    951. tempfile [创建临时文件]
    952. import os
    953. import tempfile
    954. temp = tempfile.TemporaryFile() # 定义一个临时文件对象
    955. try:
    956. temp.write('Some data') # 写入数据
    957. temp.writelines(['first\n', 'second\n']) # 写入多行
    958. temp.seek(0) # 写入
    959. print temp.read() # 读取
    960. for line in temp: # 循环读取每一行
    961. print line.rstrip()
    962. finally:
    963. temp.close() # 关闭后删除临时文件
    964. # 创建临时目录
    965. import os
    966. import tempfile
    967. directory_name = tempfile.mkdtemp()
    968. print directory_name # 打印临时目录地址 /var/folders...
    969. # Clean up the directory yourself
    970. os.removedirs(directory_name) # 创建临时目录需要手动删除
    971. # 控制临时文件名
    972. import tempfile
    973. temp = tempfile.NamedTemporaryFile(suffix='_suffix', prefix='prefix_', dir='/tmp')
    974. try:
    975. print 'temp:', temp
    976. print 'temp.name:', temp.name
    977. finally:
    978. temp.close()
    979. email [发送邮件]
    980. 发送邮件内容
    981. #!/usr/bin/python
    982. #encoding:utf8
    983. # 导入 smtplib 和 MIMEText
    984. import smtplib
    985. from email.mime.text import MIMEText
    986. # 定义发送列表
    987. mailto_list=["272121935@qq.com","272121935@163.com"]
    988. # 设置服务器名称、用户名、密码以及邮件后缀
    989. mail_host = "smtp.163.com"
    990. mail_user = "mailuser"
    991. mail_pass = "password"
    992. mail_postfix="163.com"
    993. # 发送邮件函数
    994. def send_mail(to_list, sub):
    995. me = mail_user + "<"+mail_user+"@"+mail_postfix+">"
    996. fp = open('context.txt')
    997. msg = MIMEText(fp.read(),_charset="utf-8")
    998. fp.close()
    999. msg['Subject'] = sub
    1000. msg['From'] = me
    1001. msg['To'] = ";".join(to_list)
    1002. try:
    1003. send_smtp = smtplib.SMTP()
    1004. send_smtp.connect(mail_host)
    1005. send_smtp.login(mail_user+"@"+mail_postfix, mail_pass)
    1006. send_smtp.sendmail(me, to_list, msg.as_string())
    1007. send_smtp.close()
    1008. return True
    1009. except Exception, e:
    1010. print str(e)
    1011. return False
    1012. if send_mail(mailto_list,"标题"):
    1013. print "测试成功"
    1014. else:
    1015. print "测试失败"
    1016. 发送附件
    1017. #!/usr/bin/python
    1018. #encoding:utf8
    1019. import smtplib
    1020. from email.mime.multipart import MIMEMultipart
    1021. from email.mime.base import MIMEBase
    1022. from email import encoders
    1023. def send_mail(to_list, sub, filename):
    1024. me = mail_user + "<"+mail_user+"@"+mail_postfix+">"
    1025. msg = MIMEMultipart()
    1026. msg['Subject'] = sub
    1027. msg['From'] = me
    1028. msg['To'] = ";".join(to_list)
    1029. submsg = MIMEBase('application', 'x-xz')
    1030. submsg.set_payload(open(filename,'rb').read())
    1031. encoders.encode_base64(submsg)
    1032. submsg.add_header('Content-Disposition', 'attachment', filename=filename)
    1033. msg.attach(submsg)
    1034. try:
    1035. send_smtp = smtplib.SMTP()
    1036. send_smtp.connect(mail_host)
    1037. send_smtp.login(mail_user, mail_pass)
    1038. send_smtp.sendmail(me, to_list, msg.as_string())
    1039. send_smtp.close()
    1040. return True
    1041. except Exception, e:
    1042. print str(e)[1]
    1043. return False
    1044. # 设置服务器名称、用户名、密码以及邮件后缀
    1045. mail_host = "smtp.163.com"
    1046. mail_user = "xuesong"
    1047. mail_pass = "mailpasswd"
    1048. mail_postfix = "163.com"
    1049. mailto_list = ["272121935@qq.com","quanzhou722@163.com"]
    1050. title = 'check'
    1051. filename = 'file_check.html'
    1052. if send_mail(mailto_list,title,filename):
    1053. print "发送成功"
    1054. else:
    1055. print "发送失败"
    1056. gzip [解压缩gzip 删除原文件]
    1057. #压缩gzip
    1058. import gzip
    1059. f_in = open('file.log', 'rb')
    1060. f_out = gzip.open('file.log.gz', 'wb')
    1061. f_out.writelines(f_in)
    1062. f_out.close()
    1063. f_in.close()
    1064. #压缩gzip
    1065. File = 'xuesong_18.log'
    1066. g = gzip.GzipFile(filename="", mode='wb', compresslevel=9, fileobj=open((r'%s.gz' %File),'wb'))
    1067. g.write(open(r'%s' %File).read())
    1068. g.close()
    1069. #解压gzip
    1070. g = gzip.GzipFile(mode='rb', fileobj=open((r'xuesong_18.log.gz'),'rb'))
    1071. open((r'xuesong_18.log'),'wb').write(g.read())
    1072. tarfile [归档压缩tar.gz 保留原文件]
    1073. # 压缩tar.gz
    1074. import os
    1075. import tarfile
    1076. tar = tarfile.open("/tmp/tartest.tar.gz","w:gz") # 创建压缩包名
    1077. for path,dir,files in os.walk("/tmp/tartest"): # 递归文件目录
    1078. for file in files:
    1079. fullpath = os.path.join(path,file)
    1080. tar.add(fullpath) # 创建压缩包
    1081. tar.close()
    1082. # 解压tar.gz
    1083. import tarfile
    1084. tar = tarfile.open("/tmp/tartest.tar.gz")
    1085. #tar.extract("/tmp") # 全部解压到指定路径
    1086. names = tar.getnames() # 包内文件名
    1087. for name in names:
    1088. tar.extract(name,path="./") # 解压指定文件
    1089. tar.close()
    1090. zipfile [解压缩zip 最大2G]
    1091. # 压缩zip
    1092. import zipfile,os
    1093. f = zipfile.ZipFile('filename.zip', 'w' ,zipfile.ZIP_DEFLATED) # ZIP_STORE 为默认表不压缩. ZIP_DEFLATED 表压缩
    1094. #f.write('file1.txt') # 将文件写入压缩包
    1095. for path,dir,files in os.walk("tartest"): # 递归压缩目录
    1096. for file in files:
    1097. f.write(os.path.join(path,file)) # 将文件逐个写入压缩包
    1098. f.close()
    1099. # 解压zip
    1100. if zipfile.is_zipfile('filename.zip'): # 判断一个文件是不是zip文件
    1101. f = zipfile.ZipFile('filename.zip')
    1102. for file in f.namelist(): # 返回文件列表
    1103. f.extract(file, r'/tmp/') # 解压指定文件
    1104. #f.extractall() # 解压全部
    1105. f.close()
    1106. time/datetime [时间]
    1107. import time
    1108. time.strftime('%Y%m%d_%H%M') # 格式化时间
    1109. time.time() # 时间戳[浮点]
    1110. int(time.time()) # 时间戳[整s]
    1111. time.localtime()[1] - 1 # 上个月
    1112. time.strftime('%Y-%m-%d_%X',time.localtime( time.time() ) ) # 时间戳转日期
    1113. time.mktime(time.strptime('2012-03-28 06:53:40', '%Y-%m-%d %H:%M:%S')) # 日期转时间戳
    1114. 最近的周五
    1115. from datetime import datetime
    1116. from dateutil.relativedelta import relativedelta, FR
    1117. (datetime.now() + relativedelta(weekday=FR(-1))).strftime('%Y%m%d')
    1118. 获取本周一
    1119. import datetime
    1120. datetime.date.today() - datetime.timedelta(days=datetime.date.today().weekday())
    1121. 判断输入时间格式是否正确
    1122. #encoding:utf8
    1123. import time
    1124. while 1:
    1125. atime=raw_input('输入格式如[14.05.13 13:00]:')
    1126. try:
    1127. btime=time.mktime(time.strptime('%s:00' %atime, '%y.%m.%d %H:%M:%S'))
    1128. break
    1129. except:
    1130. print '时间输入错误,请重新输入,格式如[14.05.13 13:00]'
    1131. 上一个月最后一天
    1132. import datetime
    1133. lastMonth=datetime.date(datetime.date.today().year,datetime.date.today().month,1)-datetime.timedelta(1)
    1134. lastMonth.strftime("%Y/%m")
    1135. 前一天
    1136. (datetime.datetime.now() + datetime.timedelta(days=-1) ).strftime('%Y%m%d')
    1137. 两日期相差天数
    1138. import datetime
    1139. d1 = datetime.datetime(2005, 2, 16)
    1140. d2 = datetime.datetime(2004, 12, 31)
    1141. (d1 - d2).days
    1142. 向后加10个小时
    1143. import datetime
    1144. d1 = datetime.datetime.now()
    1145. d3 = d1 + datetime.timedelta(hours=10)
    1146. d3.ctime()
    1147. optparse [解析参数及标准提示]
    1148. import os, sys
    1149. import time
    1150. import optparse
    1151. # python aaa.py -t file -p /etc/opt -o aaaaa
    1152. def do_fiotest( type, path, output,):
    1153. print type, path, output,
    1154. def main():
    1155. parser = optparse.OptionParser()
    1156. parser.add_option('-t', '--type', dest = 'type', default = None, help = 'test type[file, device]')
    1157. parser.add_option('-p', '--path', dest = 'path', default = None, help = 'test file path or device path')
    1158. parser.add_option('-o', '--output', dest = 'output', default = None, help = 'result dir path')
    1159. (o, a) = parser.parse_args()
    1160. if None == o.type or None == o.path or None == o.output:
    1161. print "No device or file or output dir"
    1162. return -1
    1163. if 'file' != o.type and 'device' != o.type:
    1164. print "You need specify test type ['file' or 'device']"
    1165. return -1
    1166. do_fiotest(o.type, o.path, o.output)
    1167. print "Test done!"
    1168. if __name__ == '__main__':
    1169. main()
    1170. getopt [解析参数]
    1171. import sys,os
    1172. import getopt
    1173. try:
    1174. options,argsErr = getopt.getopt(sys.argv[1:],"hu:c:",["help","user=","cmd="]) # 中间短参数,后面长参数对应. 不带:或=代表不带参数
    1175. except getopt.GetoptError:
    1176. print "Unknown parameters,More info with: %s -h" %(sys.argv[0])
    1177. sys.exit(2)
    1178. if argsErr != []:
    1179. print "Unknown parameters,More info with: %s -h" %(sys.argv[0])
    1180. sys.exit(2)
    1181. for o,a in options:
    1182. if o in ("-h","--help"):
    1183. print '''Usage: python te.py -u user -c "cmd -options" '''
    1184. sys.exit(2)
    1185. if o in ("-u","--user"):
    1186. user = a
    1187. if o in ("-c","--cmd"):
    1188. cmd = a
    1189. print user,cmd
    1190. argparse [命令行选项和参数解析库]
    1191. import argparse
    1192. parser = argparse.ArgumentParser( prog='usage_name', description='开头打印', epilog="结束打印")
    1193. parser.add_argument('-f', '--foo', help='foo help', action='append') # 可选参数,如使用此参数必须传值 action='store_true' 不加参数为True action='append' 多个参数可叠加为列表
    1194. parser.add_argument('--aa', type=int, default=42, help='aa!') # type规定参数类型,default设置默认值
    1195. parser.add_argument('bar', nargs='*', default=[1, 2, 3], help='BAR!') # 位置参数 必须传递 nargs=2 需要传递2个参数
    1196. parser.add_argument('args', nargs=argparse.REMAINDER) # 剩余参数收集到列表
    1197. parser.print_help() # 打印使用帮助
    1198. #parser.parse_args('BAR --foo FOO'.split()) # 设置位置参数
    1199. args = parser.parse_args() # 全部的值
    1200. parser.get_default('foo') # 获取
    1201. python a.py --foo ww --aa 40 xuesong 27 # 执行此脚本
    1202. subprocess [子进程管理]
    1203. import subprocess
    1204. s=subprocess.Popen('sleep 20', shell=True, \
    1205. stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
    1206. print s.wait() # 阻塞等待子进程完成并返回状态码 shell 0为正确 但管道内容过多会造成死锁可以用 communicate()
    1207. print s.stdout.read()
    1208. print s.stderr.read()
    1209. print s.communicate() # 返回元组 (stdout, stderr) 会阻塞等待进程完成 推荐使用
    1210. print s.returncode # 返回执行状态码
    1211. base64 [编码]
    1212. # 简单但容易被直接破解
    1213. import base64
    1214. s1 = base64.encodestring('hello world')
    1215. s2 = base64.decodestring(s1)
    1216. uu [对文件uu编码]
    1217. import uu
    1218. uu.encode('in_file','out_file') # 编码
    1219. uu.decode('in_file','out_file') # 解码
    1220. binascii [ascii和二进制编码转换]
    1221. md5 [单向MD5加密]
    1222. import md5
    1223. m = md5.new('123456').hexdigest()
    1224. hashlib [hash算法库]
    1225. import hashlib
    1226. m = hashlib.md5()
    1227. m.update("Nobody inspects") # 使用update方法对字符串md5加密
    1228. m.digest() # 加密后二进制结果
    1229. m.hexdigest() # 加密后十进制结果
    1230. hashlib.new("md5", "string").hexdigest() # 对字符串加密
    1231. hashlib.new("md5", open("file").read()).hexdigest() # 查看文件MD5值
    1232. hashlib.sha224("Nobody inspects the spammish repetition").hexdigest() # 几种hash算法 sha1 sha224 sha256 sha384 ha512
    1233. crypt [单向加密]
    1234. import crypt
    1235. import random,string
    1236. def getsalt(chars = string.letters+string.digits):
    1237. return random.choice(chars)+random.choice(chars)
    1238. salt = getsalt()
    1239. print salt
    1240. print crypt.crypt('bananas',salt)
    1241. pycrypto [加密]
    1242. # https://github.com/dlitz/pycrypto
    1243. SHA256 # 不可逆散列算法加密
    1244. from Crypto.Hash import SHA256
    1245. hash = SHA256.new()
    1246. hash.update('message')
    1247. hash.digest()
    1248. AES # 可逆加密,需要密钥
    1249. from Crypto.Cipher import AES
    1250. obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
    1251. message = "The answer is no"
    1252. ciphertext = obj.encrypt(message)
    1253. ciphertext
    1254. '\xd6\x83\x8dd!VT\x92\xaa`A\x05\xe0\x9b\x8b\xf1'
    1255. obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
    1256. obj2.decrypt(ciphertext)
    1257. 'The answer is no'
    1258. rsa [公钥加密算法]
    1259. http://www.heikkitoivonen.net/m2crypto/api/M2Crypto.RSA.RSA-class.html
    1260. pip install M2Crypto
    1261. from M2Crypto import RSA,BIO # help(RSA)
    1262. rsa = RSA.gen_key(2048, 'sha1') # 设置生成密钥为2048位,1024较不安全,默认算法sha1
    1263. rsa.save_key('rsa.priv.pem', None ) # 生成私钥pem文件
    1264. rsa.save_pub_key('rsa.pub.pem') # 生成公钥pem文件
    1265. rsa.save_key_bio() # 私钥保存到pem格式的M2Crypto.BIO.BIO对象
    1266. rsa.save_pub_key_bio() # 公钥保存到pem格式的M2Crypto.BIO.BIO对象
    1267. priv=RSA.load_key('rsa.priv.pem') # 加载私钥文件
    1268. pub=RSA.load_pub_key('rsa.pub.pem') # 加载公钥文件
    1269. rsa.check_key() # 检查key是否初始化
    1270. pub_key.public_encrypt('data',RSA.pkcs1_padding) # 公钥加密
    1271. priv_key.private_decrypt('密文',RSA.pkcs1_padding) # 私钥解密
    1272. from M2Crypto import RSA,BIO
    1273. rsa = RSA.gen_key(2048, 3, lambda *agr:None)
    1274. pub_bio = BIO.MemoryBuffer()
    1275. priv_bio = BIO.MemoryBuffer()
    1276. rsa.save_pub_key_bio(pub_bio)
    1277. rsa.save_key_bio(priv_bio, None)
    1278. # print pub_bio.read_all()
    1279. pub_key = RSA.load_pub_key_bio(pub_bio)
    1280. priv_key = RSA.load_key_bio(priv_bio)
    1281. message = 'i am luchanghong'
    1282. encrypted = pub_key.public_encrypt(message, RSA.pkcs1_padding) # 加密
    1283. decrypted = priv_key.private_decrypt(encrypted, RSA.pkcs1_padding) # 解密
    1284. print decrypted
    1285. getpass [隐藏输入密码]
    1286. import getpass
    1287. passwd=getpass.getpass()
    1288. string [字符串类]
    1289. import string
    1290. string.ascii_letters # a-zA-Z ascii的不受语言系统环境变化
    1291. string.ascii_lowercase # a-z
    1292. string.letters # a-zA-Z 受系统语言环境变化影响
    1293. string.lowercase # a-z
    1294. string.uppercase # A-Z大小
    1295. string.digits # 0-9
    1296. string.printable # 所有字符
    1297. string.whitespace # 空白字符
    1298. Gittle [pythongit库]
    1299. pip install gittle
    1300. from gittle import Gittle
    1301. repo_path = '/tmp/gittle_bare'
    1302. repo_url = 'git://github.com/FriendCode/gittle.git'
    1303. repo = Gittle.clone(repo_url, repo_path)
    1304. auth = GittleAuth(pkey=key) # 认证
    1305. Gittle.clone(repo_url, repo_path, auth=auth)
    1306. repo = Gittle.clone(repo_url, repo_path, bare=True) # 克隆仓库没有目录的
    1307. repo = Gittle.init(path) # 初始化
    1308. repo.commits # 获取提交列表
    1309. repo.branches # 获取分支列表
    1310. repo.modified_files # 被修改的文件列表
    1311. repo.diff('HEAD', 'HEAD~1') # 获取最新提交差异
    1312. repo.stage('file.txt') # 提交文件
    1313. repo.stage(['other1.txt', 'other2.txt']) # 提交文件列表
    1314. repo.commit(name="Samy Pesse", email="samy@friendco.de", message="This is a commit") # 更新信息
    1315. repo = Gittle(repo_path, origin_uri=repo_url)
    1316. key_file = open('/Users/Me/keys/rsa/private_rsa')
    1317. repo.auth(pkey=key_file)
    1318. repo.push() # 远端push提交操作
    1319. repo = Gittle(repo_path, origin_uri=repo_url)
    1320. key_file = open('/Users/Me/keys/rsa/private_rsa')
    1321. repo.auth(pkey=key_file)
    1322. repo.pull() # 拉取最新分支
    1323. repo.create_branch('dev', 'master') # 创建分支
    1324. repo.switch_branch('dev') # 切换到分支
    1325. repo.create_orphan_branch('NewBranchName') # 创建一个空的分支
    1326. repo.remove_branch('dev') # 删除分支
    1327. paramiko [ssh客户端]
    1328. 安装
    1329. sudo apt-get install python-setuptools
    1330. easy_install
    1331. sudo apt-get install python-all-dev
    1332. sudo apt-get install build-essential
    1333. paramiko实例(账号密码登录执行命令)
    1334. #!/usr/bin/python
    1335. #ssh
    1336. import paramiko
    1337. import sys,os
    1338. host = '10.152.15.200'
    1339. user = 'peterli'
    1340. password = '123456'
    1341. s = paramiko.SSHClient() # 绑定实例
    1342. s.load_system_host_keys() # 加载本地HOST主机文件
    1343. s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 允许连接不在know_hosts文件中的主机
    1344. s.connect(host,22,user,password,timeout=5) # 连接远程主机
    1345. while True:
    1346. cmd=raw_input('cmd:')
    1347. stdin,stdout,stderr = s.exec_command(cmd) # 执行命令
    1348. cmd_result = stdout.read(),stderr.read() # 读取命令结果
    1349. for line in cmd_result:
    1350. print line,
    1351. s.close()
    1352. paramiko实例(传送文件)
    1353. #!/usr/bin/evn python
    1354. import os
    1355. import paramiko
    1356. host='127.0.0.1'
    1357. port=22
    1358. username = 'peterli'
    1359. password = '123456'
    1360. ssh=paramiko.Transport((host,port))
    1361. privatekeyfile = os.path.expanduser('~/.ssh/id_rsa')
    1362. mykey = paramiko.RSAKey.from_private_key_file( os.path.expanduser('~/.ssh/id_rsa')) # 加载key 不使用key可不加
    1363. ssh.connect(username=username,password=password) # 连接远程主机
    1364. # 使用key把 password=password 换成 pkey=mykey
    1365. sftp=paramiko.SFTPClient.from_transport(ssh) # SFTP使用Transport通道
    1366. sftp.get('/etc/passwd','pwd1') # 下载 两端都要指定文件名
    1367. sftp.put('pwd','/tmp/pwd') # 上传
    1368. sftp.close()
    1369. ssh.close()
    1370. paramiko实例(密钥执行命令)
    1371. #!/usr/bin/python
    1372. #ssh
    1373. import paramiko
    1374. import sys,os
    1375. host = '10.152.15.123'
    1376. user = 'peterli'
    1377. s = paramiko.SSHClient()
    1378. s.load_system_host_keys()
    1379. s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    1380. privatekeyfile = os.path.expanduser('~/.ssh/id_rsa') # 定义key路径
    1381. mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
    1382. # mykey=paramiko.DSSKey.from_private_key_file(privatekeyfile,password='061128') # DSSKey方式 password是key的密码
    1383. s.connect(host,22,user,pkey=mykey,timeout=5)
    1384. cmd=raw_input('cmd:')
    1385. stdin,stdout,stderr = s.exec_command(cmd)
    1386. cmd_result = stdout.read(),stderr.read()
    1387. for line in cmd_result:
    1388. print line,
    1389. s.close()
    1390. ssh并发(Pool控制最大并发)
    1391. #!/usr/bin/env python
    1392. #encoding:utf8
    1393. #ssh_concurrent.py
    1394. import multiprocessing
    1395. import sys,os,time
    1396. import paramiko
    1397. def ssh_cmd(host,port,user,passwd,cmd):
    1398. msg = "-----------Result:%s----------" % host
    1399. s = paramiko.SSHClient()
    1400. s.load_system_host_keys()
    1401. s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    1402. try:
    1403. s.connect(host,22,user,passwd,timeout=5)
    1404. stdin,stdout,stderr = s.exec_command(cmd)
    1405. cmd_result = stdout.read(),stderr.read()
    1406. print msg
    1407. for line in cmd_result:
    1408. print line,
    1409. s.close()
    1410. except paramiko.AuthenticationException:
    1411. print msg
    1412. print 'AuthenticationException Failed'
    1413. except paramiko.BadHostKeyException:
    1414. print msg
    1415. print "Bad host key"
    1416. result = []
    1417. p = multiprocessing.Pool(processes=20)
    1418. cmd=raw_input('CMD:')
    1419. f=open('serverlist.conf')
    1420. list = f.readlines()
    1421. f.close()
    1422. for IP in list:
    1423. print IP
    1424. host=IP.split()[0]
    1425. port=int(IP.split()[1])
    1426. user=IP.split()[2]
    1427. passwd=IP.split()[3]
    1428. result.append(p.apply_async(ssh_cmd,(host,port,user,passwd,cmd)))
    1429. p.close()
    1430. for res in result:
    1431. res.get(timeout=35)
    1432. ssh并发(取文件状态并发送邮件)
    1433. #!/usr/bin/python
    1434. #encoding:utf8
    1435. #config file: ip.list
    1436. import paramiko
    1437. import multiprocessing
    1438. import smtplib
    1439. import sys,os,time,datetime,socket,re
    1440. from email.mime.text import MIMEText
    1441. # 配置文件(IP列表)
    1442. Conf = 'ip.list'
    1443. user_name = 'peterli'
    1444. user_pwd = 'passwd'
    1445. port = 22
    1446. PATH = '/home/peterli/'
    1447. # 设置服务器名称、用户名、密码以及邮件后缀
    1448. mail_host = "smtp.163.com"
    1449. mail_user = "xuesong"
    1450. mail_pass = "mailpasswd"
    1451. mail_postfix = "163.com"
    1452. mailto_list = ["272121935@qq.com","quanzhou722@163.com"]
    1453. title = 'file check'
    1454. DATE1=(datetime.datetime.now() + datetime.timedelta(days=-1) ).strftime('%Y%m%d')
    1455. file_path = '%s%s' %(PATH,DATE1)
    1456. def Ssh_Cmd(file_path,host_ip,user_name,user_pwd,port=22):
    1457. s = paramiko.SSHClient()
    1458. s.load_system_host_keys()
    1459. s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    1460. try:
    1461. s.connect(hostname=host_ip,port=port,username=user_name,password=user_pwd)
    1462. stdin,stdout,stderr = s.exec_command('stat %s' %file_path)
    1463. stat_result = '%s%s' %(stdout.read(),stderr.read())
    1464. if stat_result.find('No such file or directory') == -1:
    1465. file_status = 'OK\t'
    1466. stdin,stdout,stderr = s.exec_command('du -sh %s' %file_path)
    1467. cmd1_result = '%s_%s' %(stat_result.split()[32],stat_result.split()[33].split('.')[0])
    1468. cmd2_result = ('%s%s' %(stdout.read(),stderr.read())).split()[0]
    1469. else:
    1470. file_status = '未生成\t'
    1471. cmd1_result = 'null'
    1472. cmd2_result = 'null'
    1473. q.put(['Login successful'])
    1474. s.close()
    1475. except socket.error:
    1476. file_status = '主机或端口错误'
    1477. cmd1_result = '-'
    1478. cmd2_result = '-'
    1479. except paramiko.AuthenticationException:
    1480. file_status = '用户或密码错误'
    1481. cmd1_result = '-'
    1482. cmd2_result = '-'
    1483. except paramiko.BadHostKeyException:
    1484. file_status = 'Bad host key'
    1485. cmd1_result = '-'
    1486. cmd2_result = '-'
    1487. except:
    1488. file_status = 'ssh异常'
    1489. cmd1_result = '-'
    1490. cmd2_result = '-'
    1491. r.put('%s\t-\t%s\t%s\t%s\t%s\n' %(time.strftime('%Y-%m-%d_%H:%M'),host_ip,file_status,cmd2_result,cmd1_result))
    1492. def Concurrent(Conf,file_path,user_name,user_pwd,port):
    1493. # 执行总计
    1494. total = 0
    1495. # 读取配置文件
    1496. f=open(Conf)
    1497. list = f.readlines()
    1498. f.close()
    1499. # 并发执行
    1500. process_list = []
    1501. log_file = file('file_check.log', 'w')
    1502. log_file.write('检查时间\t\t业务\tIP\t\t文件状态\t大小\t生成时间\n')
    1503. for host_info in list:
    1504. # 判断配置文件中注释行跳过
    1505. if host_info.startswith('#'):
    1506. continue
    1507. # 取变量,其中任意变量未取到就跳过执行
    1508. try:
    1509. host_ip=host_info.split()[0].strip()
    1510. #user_name=host_info.split()[1]
    1511. #user_pwd=host_info.split()[2]
    1512. except:
    1513. log_file.write('Profile error: %s\n' %(host_info))
    1514. continue
    1515. #try:
    1516. # port=int(host_info.split()[3])
    1517. #except:
    1518. # port=22
    1519. total +=1
    1520. p = multiprocessing.Process(target=Ssh_Cmd,args=(file_path,host_ip,user_name,user_pwd,port))
    1521. p.start()
    1522. process_list.append(p)
    1523. for j in process_list:
    1524. j.join()
    1525. for j in process_list:
    1526. log_file.write(r.get())
    1527. successful = q.qsize()
    1528. log_file.write('执行完毕。 总执行:%s 登录成功:%s 登录失败:%s\n' %(total,successful,total - successful))
    1529. log_file.flush()
    1530. log_file.close()
    1531. def send_mail(to_list, sub):
    1532. me = mail_user + "<"+mail_user+"@"+mail_postfix+">"
    1533. fp = open('file_check.log')
    1534. msg = MIMEText(fp.read(),_charset="utf-8")
    1535. fp.close()
    1536. msg['Subject'] = sub
    1537. msg['From'] = me
    1538. msg['To'] = ";".join(to_list)
    1539. try:
    1540. send_smtp = smtplib.SMTP()
    1541. send_smtp.connect(mail_host)
    1542. send_smtp.login(mail_user, mail_pass)
    1543. send_smtp.sendmail(me, to_list, msg.as_string())
    1544. send_smtp.close()
    1545. return True
    1546. except Exception, e:
    1547. print str(e)[1]
    1548. return False
    1549. if __name__ == '__main__':
    1550. q = multiprocessing.Queue()
    1551. r = multiprocessing.Queue()
    1552. Concurrent(Conf,file_path,user_name,user_pwd,port)
    1553. if send_mail(mailto_list,title):
    1554. print "发送成功"
    1555. else:
    1556. print "发送失败"
    1557. pysnmp [snmp客户端]
    1558. #!/usr/bin/python
    1559. from pysnmp.entity.rfc3413.oneliner import cmdgen
    1560. cg = cmdgen.CommandGenerator()
    1561. # 注意IP 端口 组默认public oid值
    1562. varBinds = cg.getCmd( cmdgen.CommunityData('any-agent', 'public',0 ), cmdgen.UdpTransportTarget(('10.10.76.42', 161)), (1,3,6,1,4,1,2021,10,1,3,1), )
    1563. print varBinds[3][0][1]
    1564. PDB [单步调试]
    1565. # 很多程序因为被try了,看不到具体报错的地方, 用这个模块就很清晰可以看到错误的位置
    1566. # http://docs.python.org/2/library/pdb.html
    1567. (Pdb) h # 帮助
    1568. # 断点设置
    1569. (Pdb)b 10 # 断点设置在本py的第10行
    1570. (Pdb)b ots.py:20 # 断点设置到 ots.py第20行
    1571. (Pdb)b # 查看断点编号
    1572. (Pdb)cl 2 # 删除第2个断点
    1573. # 运行
    1574. (Pdb)n # 单步运行
    1575. (Pdb)s # 细点运行 也就是会下到,方法
    1576. (Pdb)c # 跳到下个断点
    1577. # 查看
    1578. (Pdb)p param # 查看当前 变量值
    1579. (Pdb)l # 查看运行到某处代码
    1580. (Pdb)a # 查看全部栈内变量
    1581. !a = 100 # 直接赋值
    1582. python -m pdb myscript.py # 直接对脚本单步调试
    1583. # 在程序里面加单步调试
    1584. import pdb
    1585. def tt():
    1586. pdb.set_trace()
    1587. for i in range(1, 5):
    1588. print i
    1589. >>> tt()
    1590. > <stdin>(3)tt()
    1591. (Pdb) n #这里支持 n p c 而已
    1592. pstats [源码性能分析测试]
    1593. import profile
    1594. import pstats
    1595. profile.run("run()", "prof.txt")
    1596. p = pstats.Stats("prof.txt")
    1597. p.sort_stats("time").print_stats()
    1598. apscheduler [任务调度]
    1599. # 安装 pip install apscheduler
    1600. # 例子 https://bitbucket.org/agronholm/apscheduler/src/e6298f953a68/tests/?at=master
    1601. scheduler.start() # 启动任务
    1602. job = scheduler.add_job(myfunc, 'interval', minutes=2) # 添加任务
    1603. job.remove() # 删除任务
    1604. scheduler.add_job(myfunc, 'interval', minutes=2, id='my_job_id') # 添加任务
    1605. scheduler.remove_job('my_job_id') # 删除任务
    1606. job.modify(max_instances=6, name='Alternate name') # 修改工作
    1607. scheduler.shutdown() # 关闭调度
    1608. scheduler.shutdown(wait=False) # 关闭调度 不等待
    1609. # 暂停
    1610. apscheduler.job.Job.pause()
    1611. apscheduler.schedulers.base.BaseScheduler.pause_job()
    1612. # 恢复
    1613. apscheduler.job.Job.resume()
    1614. apscheduler.schedulers.base.BaseScheduler.resume_job()
    1615. 定时任务
    1616. from pytz import utc
    1617. from apscheduler.schedulers.background import BackgroundScheduler
    1618. from apscheduler.executors.pool import ThreadPoolExecutor, ProcessPoolExecutor
    1619. import time
    1620. executors = {
    1621. 'default': ThreadPoolExecutor(20),
    1622. 'processpool': ProcessPoolExecutor(5)
    1623. }
    1624. job_defaults = {
    1625. 'coalesce': False,
    1626. 'max_instances': 3
    1627. }
    1628. scheduler = BackgroundScheduler( executors=executors, job_defaults=job_defaults, timezone=utc)
    1629. def myfunc():
    1630. print 'test'
    1631. scheduler.add_job(myfunc, 'interval', minutes=1, id='myworkid')
    1632. scheduler.start()
    1633. try:
    1634. while True:
    1635. time.sleep(2)
    1636. # add_job
    1637. except (KeyboardInterrupt, SystemExit):
    1638. scheduler.shutdown()
    1639. logging [日志记录]
    1640. # 日志级别大小关系为: critical > error > warning > info > debug > notset 也可自定义日志级别
    1641. import logging
    1642. logging.debug('debug') # 默认日志级别为 warning ,故debug日志不做打印
    1643. logging.warning('warning') # 达到默认日志级别为WARNING,打印到屏幕 warning
    1644. logging.basicConfig # 通过logging.basicConfig函数对日志的输出格式及方式做相关配置
    1645. # basicConfig 相关参数帮助
    1646. filename # 指定日志文件名
    1647. filemode # 和file函数意义相同,指定日志文件的打开模式,'w'或'a'
    1648. datefmt # 指定时间格式,同time.strftime()
    1649. level # 设置日志级别,默认为logging.WARNING
    1650. stream # 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略
    1651. format # 指定输出的格式和内容,format可以输出很多有用信息,如上例所示:
    1652. %(levelno)s # 打印日志级别的数值
    1653. %(levelname)s # 打印日志级别名称
    1654. %(pathname)s # 打印当前执行程序的路径,其实就是sys.argv[0]
    1655. %(filename)s # 打印当前执行程序名
    1656. %(funcName)s # 打印日志的当前函数
    1657. %(lineno)d # 打印日志的当前行号
    1658. %(asctime)s # 打印日志的时间
    1659. %(thread)d # 打印线程ID
    1660. %(threadName)s # 打印线程名称
    1661. %(process)d # 打印进程ID
    1662. %(message)s # 打印日志信息
    1663. logging.config.fileConfig("logger.conf") # 加载配置文件
    1664. logger = logging.getLogger("example02") # 使用已定义的日志记录器
    1665. logger.conf # 配置文件
    1666. ###############################################
    1667. [loggers]
    1668. keys=root,example01,example02 # 设置三种日志记录器
    1669. [logger_root] # 针对单一种设置
    1670. level=DEBUG
    1671. handlers=hand01,hand02
    1672. [logger_example01]
    1673. handlers=hand01,hand02 # 使用2中处理方式 应该是根据不同级别区分的
    1674. qualname=example01
    1675. propagate=0
    1676. [logger_example02]
    1677. handlers=hand01,hand03
    1678. qualname=example02
    1679. propagate=0
    1680. ###############################################
    1681. [handlers] # 不同的处理方式
    1682. keys=hand01,hand02,hand03 # 三种方式的名字
    1683. [handler_hand01] # 第一种方式配置
    1684. class=StreamHandler # 发送错误信息到流
    1685. level=INFO # 日志级别
    1686. formatter=form02 # 日志的格式方式
    1687. args=(sys.stderr,)
    1688. [handler_hand02]
    1689. class=FileHandler # FileHandler写入磁盘文件
    1690. level=DEBUG
    1691. formatter=form01
    1692. args=('myapp.log', 'a') # 追加到日志文件
    1693. [handler_hand03]
    1694. class=handlers.RotatingFileHandler
    1695. level=INFO
    1696. formatter=form02
    1697. args=('myapp.log', 'a', 10*1024*1024, 5) # 追加日志并切割日志
    1698. ###############################################
    1699. [formatters] # 针对不同处理日志方式设置具体的日志格式
    1700. keys=form01,form02
    1701. [formatter_form01]
    1702. format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s # 日志列
    1703. datefmt=%a, %d %b %Y %H:%M:%S # 时间格式
    1704. [formatter_form02]
    1705. format=%(name)-12s: %(levelname)-8s %(message)s
    1706. datefmt=
    1707. 通用日志记录
    1708. import logging
    1709. logging.basicConfig(level=logging.DEBUG,
    1710. format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
    1711. datefmt='%Y-%m-%d %H:%M:%S',
    1712. filename='/var/log/myapp.log',
    1713. filemode='a')
    1714. # 日志级别DEBUG或高于DEBUG的会写入文件 myapp.log 中
    1715. logging.debug('debug message')
    1716. logging.info('info message')
    1717. logging.warning('warning message')
    1718. ConfigParser [配置解析]
    1719. 写入配置文件
    1720. import ConfigParser
    1721. config = ConfigParser.RawConfigParser()
    1722. config.add_section('Section1') # 添加配置文件的块 [name]
    1723. config.set('Section1', 'an_int', '15') # 针对块设置配置参数和值
    1724. config.set('Section1', 'a_bool', 'true')
    1725. config.set('Section1', 'a_float', '3.1415')
    1726. config.set('Section1', 'baz', 'fun')
    1727. config.set('Section1', 'bar', 'Python')
    1728. config.set('Section1', 'foo', '%(bar)s is %(baz)s!')
    1729. with open('example.cfg', 'wb') as configfile: # 指定配置文件路径
    1730. config.write(configfile) # 写入配置文件
    1731. 读取配置文件
    1732. import ConfigParser
    1733. config = ConfigParser.RawConfigParser()
    1734. config.read('example.cfg') # 读取配置文件
    1735. a_float = config.getfloat('Section1', 'a_float') # 获取配置文件参数对应的浮点值,如参数值类型不对则报ValueError
    1736. an_int = config.getint('Section1', 'an_int') # 获取配置文件参数对应的整数值,可直接进行计算
    1737. print a_float + an_int
    1738. if config.getboolean('Section1', 'a_bool'): # 根据配置文件参数值是否为真
    1739. print config.get('Section1', 'foo') # 再获取依赖的配置参数 get获取后值为字符串
    1740. print config.get('Section1', 'foo', 0) # 获取配置文件参数的同时加载变量[配置文件中的参数]
    1741. print config.get('Section1', 'foo', 1) # 获取配置文件参数 原始值不做任何改动 不使用变量
    1742. config.remove_option('Section1', 'bar') # 删除读取配置文件获取bar的值
    1743. config.remove_option('Section1', 'baz')
    1744. print config.get('Section1', 'foo', 0, {'bar': 'str1', 'baz': 'str1'}) # 读取配置参数的同时设置变量的值
    1745. import ConfigParser
    1746. import io
    1747. sample_config = """
    1748. [mysqld]
    1749. user = mysql
    1750. pid-file = /var/run/mysqld/mysqld.pid
    1751. skip-external-locking
    1752. old_passwords = 1
    1753. skip-bdb
    1754. skip-innodb
    1755. """
    1756. config = ConfigParser.RawConfigParser(allow_no_value=True)
    1757. config.readfp(io.BytesIO(sample_config))
    1758. config.get("mysqld", "user")
    1759. ftplib [ftp客户端]
    1760. from ftplib import FTP
    1761. ftp = FTP('ftp.debian.org') # 连接ftp地址 FTP(host,port,timeout)
    1762. ftp.login() # 使用默认anonymous登录 login(user,passwd)
    1763. ftp.cwd('debian') # 切换到目录debian
    1764. ftp.retrlines('LIST') # 打印目录列表
    1765. ftp.retrbinary('RETR README', open('README', 'wb').write) # 下载文件写到本地
    1766. ftp.delete('filename') # 删除ftp中文件
    1767. ftp.mkd('dirname') # 在ftp上创建目录
    1768. ftp.size('filename') # 查看文件大小
    1769. ftp.quit()
    1770. difflib [对象比较]
    1771. import difflib
    1772. s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']
    1773. s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n']
    1774. for line in difflib.context_diff(s1, s2, fromfile='txt-s1', tofile='txt-s2'): # 两字列表比较差异
    1775. sys.stdout.write(line)
    1776. difflib.get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy']) # 模糊匹配 匹配列表与字符串相似的值,越相似越靠前
    1777. heapq [优先队列算法]
    1778. from heapq import *
    1779. h = []
    1780. heappush(h, (5, 'write code')) # 放入队列
    1781. heappush(h, (7, 'release product'))
    1782. heappush(h, (1, 'write spec'))
    1783. heappush(h, (3, 'create tests'))
    1784. heappop(h) # 从队列取出 第一次是1
    1785. from heapq import *
    1786. def heapsort(iterable):
    1787. h = []
    1788. for value in iterable:
    1789. heappush(h, value)
    1790. return [heappop(h) for i in range(len(h))]
    1791. heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
    1792. linecache [随机读取指定行]
    1793. import linecache
    1794. linecache.getline('/etc/passwd', 4)
    1795. json [数据交换格式]
    1796. #!/usr/bin/python
    1797. import json
    1798. #json file temp.json
    1799. #{ "name":"00_sample_case1", "description":"an example."}
    1800. f = file("temp.json");
    1801. s = json.load(f) # 直接读取json文件
    1802. print s
    1803. f.close
    1804. d = {"a":1}
    1805. j=json.dumps(d) # 字典转json
    1806. json.loads(j) # json转字典
    1807. s = json.loads('{"name":"test", "type":{"name":"seq", "parameter":["1", "2"]}}')
    1808. print type(s) # dic
    1809. print s
    1810. print s.keys()
    1811. print s["type"]["parameter"][1]
    1812. json.dumps({'ret':'cmd_ret0', 'out':'cmd_ret1'}, separators=(',', ':')) # 紧凑的json格式,去掉空格
    1813. filecmp [文件目录比较]
    1814. filecmp.cmp('/etc/passwd', '/etc/passwd') # 比较两文件是否一致
    1815. # 比较两目录下文件是否一致
    1816. from filecmp import dircmp
    1817. def print_diff_files(dcmp):
    1818. for name in dcmp.diff_files:
    1819. print "diff_file %s found in %s and %s" % (name, dcmp.left, dcmp.right)
    1820. for sub_dcmp in dcmp.subdirs.values():
    1821. print_diff_files(sub_dcmp)
    1822. dcmp = dircmp('dir1', 'dir2')
    1823. print_diff_files(dcmp)
    1824. errno [符号错误码]
    1825. https://docs.python.org/2/library/errno.html#module-errno
    1826. import errno
    1827. try:
    1828. fp = open("no.such.file")
    1829. except IOError, (error, message):
    1830. if error == errno.ENOENT:
    1831. print "no such file"
    1832. elif error == errno.EPERM:
    1833. print "permission denied"
    1834. else:
    1835. print message
    1836. Exceptions [标准异常类]
    1837. # 详见官网 不需要导入
    1838. https://docs.python.org/2/library/exceptions.html#module-exceptions
    1839. ctypes [调用C的动态库]
    1840. 提供和C语言兼容的数据类型,也可调用C的动态库
    1841. http://blog.csdn.net/linda1000/article/details/12623527
    1842. http://www.cnblogs.com/wuchang/archive/2010/04/04/1704456.html
    1843. http://www.ibm.com/developerworks/cn/linux/l-cn-pythonandc/
    1844. daemon [守护进程]
    1845. daemon.py
    1846. # 创建守护进程的模块
    1847. #!/usr/bin/env python
    1848. import sys, os, time, atexit
    1849. from signal import SIGTERM
    1850. class Daemon:
    1851. """
    1852. A generic daemon class.
    1853. Usage: subclass the Daemon class and override the run() method
    1854. """
    1855. def __init__(self, pidfile='nbMon.pid', stdin='/dev/null', stdout='nbMon.log', stderr='nbMon.log'):
    1856. self.stdin = stdin
    1857. self.stdout = stdout
    1858. self.stderr = stderr
    1859. self.pidfile = pidfile
    1860. def daemonize(self):
    1861. """
    1862. do the UNIX double-fork magic, see Stevens' "Advanced
    1863. Programming in the UNIX Environment" for details (ISBN 0201563177)
    1864. http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
    1865. """
    1866. try:
    1867. pid = os.fork()
    1868. if pid > 0:
    1869. # exit first parent
    1870. sys.exit(0)
    1871. except OSError, e:
    1872. sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
    1873. sys.exit(1)
    1874. # decouple from parent environment
    1875. #os.chdir("/")
    1876. os.setsid()
    1877. os.umask(0)
    1878. # do second fork
    1879. try:
    1880. pid = os.fork()
    1881. if pid > 0:
    1882. # exit from second parent
    1883. sys.exit(0)
    1884. except OSError, e:
    1885. sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
    1886. sys.exit(1)
    1887. # redirect standard file descriptors
    1888. sys.stdout.flush()
    1889. sys.stderr.flush()
    1890. si = file(self.stdin, 'r')
    1891. so = file(self.stdout, 'a+')
    1892. se = file(self.stderr, 'a+', 0)
    1893. os.dup2(si.fileno(), sys.stdin.fileno())
    1894. os.dup2(so.fileno(), sys.stdout.fileno())
    1895. os.dup2(se.fileno(), sys.stderr.fileno())
    1896. # write pidfile
    1897. atexit.register(self.delpid)
    1898. pid = str(os.getpid())
    1899. file(self.pidfile,'w+').write("%s\n" % pid)
    1900. def delpid(self):
    1901. os.remove(self.pidfile)
    1902. def start(self):
    1903. """
    1904. Start the daemon
    1905. """
    1906. # Check for a pidfile to see if the daemon already runs
    1907. try:
    1908. pf = file(self.pidfile,'r')
    1909. pid = int(pf.read().strip())
    1910. pf.close()
    1911. except IOError:
    1912. pid = None
    1913. if pid:
    1914. message = "pidfile %s already exist. Daemon already running?\n"
    1915. sys.stderr.write(message % self.pidfile)
    1916. sys.exit(1)
    1917. # Start the daemon
    1918. self.daemonize()
    1919. self.run()
    1920. def stop(self):
    1921. """
    1922. Stop the daemon
    1923. """
    1924. # Get the pid from the pidfile
    1925. try:
    1926. pf = file(self.pidfile,'r')
    1927. pid = int(pf.read().strip())
    1928. pf.close()
    1929. except IOError:
    1930. pid = None
    1931. if not pid:
    1932. message = "pidfile %s does not exist. Daemon not running?\n"
    1933. sys.stderr.write(message % self.pidfile)
    1934. return # not an error in a restart
    1935. # Try killing the daemon process
    1936. try:
    1937. while 1:
    1938. os.kill(pid, SIGTERM)
    1939. time.sleep(0.1)
    1940. except OSError, err:
    1941. err = str(err)
    1942. if err.find("No such process") > 0:
    1943. if os.path.exists(self.pidfile):
    1944. os.remove(self.pidfile)
    1945. else:
    1946. print str(err)
    1947. sys.exit(1)
    1948. def restart(self):
    1949. """
    1950. Restart the daemon
    1951. """
    1952. self.stop()
    1953. self.start()
    1954. def run(self):
    1955. """
    1956. You should override this method when you subclass Daemon. It will be called after the process has been
    1957. daemonized by start() or restart().
    1958. """
    1959. run_daemon.py
    1960. # 启动脚本,倒入需要后台启动的模块,继承Daemon类,覆盖run函数
    1961. # 启动方式 python run_daemon.py start
    1962. #!/usr/bin/env python
    1963. import Queue
    1964. import threading
    1965. import sys, time
    1966. import urllib2
    1967. import json
    1968. import framework
    1969. from moniItems import mon
    1970. from daemon import Daemon
    1971. class MyDaemon(Daemon):
    1972. def run(self):
    1973. print 'start'
    1974. framework.startTh()
    1975. print 'stop2'
    1976. if __name__ == "__main__":
    1977. daemon = MyDaemon()
    1978. if len(sys.argv) == 2:
    1979. if 'start' == sys.argv[1]:
    1980. daemon.start()
    1981. elif 'stop' == sys.argv[1]:
    1982. daemon.stop()
    1983. elif 'restart' == sys.argv[1]:
    1984. daemon.restart()
    1985. else:
    1986. print "Unknown command"
    1987. sys.exit(2)
    1988. sys.exit(0)
    1989. else:
    1990. print "usage: %s start|stop|restart" % sys.argv[0]
    1991. sys.exit(2)
    1992. psutil [获取系统信息]
    1993. pip install psutil # 安装
    1994. import psutil
    1995. dir(psutil)
    1996. psutil.boot_time() # 开机时间
    1997. psutil.virtual_memory() # 内存详细信息
    1998. psutil.virtual_memory().total # 内存总大小
    1999. psutil.disk_partitions() # 获取磁盘信息
    2000. psutil.disk_io_counters() # 磁盘IO信息
    2001. psutil.net_io_counters() # 获取网络IO信息
    2002. psutil.pids() # 返回所有进程PID
    2003. psutil.Process(PID) # 获取进程信息
    2004. psutil.Process(PID).name() # 指定进程的进程名
    2005. psutil.Process(PID).exe() # 进程的路径
    2006. psutil.Process(PID).cwd() # 进程工作路径
    2007. psutil.Process(PID).status() # 进程状态
    2008. psutil.Process(PID).create_time() # 进程创建时间
    2009. psutil.Process(PID).memory_percent() # 进程内存使用率
    2010. psutil.Process(PID).io_counters() # 进程IO信息
    2011. psutil.Process(PID).num_threads() # 进程线程数
    2012. ldap [统一认证]
    2013. yum install openldap openldap-clients openldap-devel openssl-devel setuptools==30.1.0
    2014. sudo pip uninstall ldap ldap3
    2015. pip install python-ldap
    2016. import ldap
    2017. con = ldap.initialize("ldap://10.10.10.156:389")
    2018. con.simple_bind_s("cn=admin,ou=People,dc=gt,dc=com", "pwd")
    2019. res = con.search_s("dc=gt,dc=com", ldap.SCOPE_SUBTREE, '(uid=*)', ['*', '+'], 0)
    2020. watchdog [监视文件实时写入]
    2021. https://pypi.python.org/pypi/watchdog
    2022. pip install watchdog
    2023. import sys
    2024. import time
    2025. import logging
    2026. from watchdog.observers import Observer
    2027. from watchdog.events import LoggingEventHandler
    2028. if __name__ == "__main__":
    2029. logging.basicConfig(level=logging.INFO,
    2030. format='%(asctime)s - %(message)s',
    2031. datefmt='%Y-%m-%d %H:%M:%S')
    2032. path = sys.argv[1] if len(sys.argv) > 1 else '.'
    2033. event_handler = LoggingEventHandler()
    2034. observer = Observer()
    2035. observer.schedule(event_handler, path, recursive=True)
    2036. observer.start()
    2037. try:
    2038. while True:
    2039. time.sleep(1)
    2040. except KeyboardInterrupt:
    2041. observer.stop()
    2042. observer.join()
    2043. yaml [标记语言]
    2044. pip install pyyaml
    2045. import yaml
    2046. a = yaml.load("""
    2047. name: Vorlin Laruknuzum
    2048. sex: Male
    2049. class: Priest
    2050. title: Acolyte
    2051. hp: [32, 71]
    2052. sp: [1, 13]
    2053. gold: 423
    2054. inventory:
    2055. - a Holy Book of Prayers (Words of Wisdom)
    2056. - an Azure Potion of Cure Light Wounds
    2057. - a Silver Wand of Wonder
    2058. """)
    2059. print a['inventory'][1] # 字典
    2060. print yaml.dump(a) # 把字典生成yaml文件
    2061. yaml.load_all # 生成迭代器
    2062. print yaml.dump({'name': "The Cloak 'Colluin'", 'depth': 5, 'rarity': 45,
    2063. 'weight': 10, 'cost': 50000, 'flags': ['INT', 'WIS', 'SPEED', 'STEALTH']})
    2064. itertools [迭代功能函数]
    2065. import itertools
    2066. # 全排序
    2067. print list(itertools.permutations(['a', 'b', 'c', 'd'],4))
    2068. # 无限迭代
    2069. ns = itertools.count(1)
    2070. for n in ns:
    2071. print n
    2072. # 指定次数循环
    2073. ns = itertools.repeat('A', 10)
    2074. for n in ns:
    2075. print n
    2076. 3 socket
    2077. socket.gethostname() # 获取主机名
    2078. from socket import * # 避免 socket.socket()
    2079. s=socket()
    2080. s.bind() # 绑定地址到套接字
    2081. s.listen() # 开始TCP监听
    2082. s.accept() # 被动接受TCP客户端连接,等待连接的到来
    2083. s.connect() # 主动初始化TCP服务器连接
    2084. s.connect_ex() # connect()函数的扩展版本,出错时返回出错码,而不是跑出异常
    2085. s.recv() # 接收TCP数据
    2086. s.send() # 发送TCP数据
    2087. s.sendall() # 完整发送TCP数据
    2088. s.recvfrom() # 接收UDP数据
    2089. s.sendto() # 发送UDP数据
    2090. s.getpeername() # 连接到当前套接字的远端的地址(TCP连接)
    2091. s.getsockname() # 当前套接字的地址
    2092. s.getsockopt() # 返回指定套接字的参数
    2093. s.setsockopt() # 设置指定套接字的参数
    2094. s.close() # 关闭套接字
    2095. s.setblocking() # 设置套接字的阻塞与非阻塞模式
    2096. s.settimeout() # 设置阻塞套接字操作的超时时间
    2097. s.gettimeout() # 得到阻塞套接字操作的超时时间
    2098. s.makefile() # 创建一个与该套接字关联的文件对象
    2099. s.fileno() # 套接字获取对应的文件描述符fd
    2100. socket.AF_UNIX # 只能够用于单一的Unix系统进程间通信
    2101. socket.AF_INET # 服务器之间网络通信
    2102. socket.AF_INET6 # IPv6
    2103. socket.SOCK_STREAM # 流式socket , for TCP
    2104. socket.SOCK_DGRAM # 数据报式socket , for UDP
    2105. socket.SOCK_RAW # 原始套接字,普通的套接字无法处理ICMP、IGMP等网络报文,而SOCK_RAW可以;其次,SOCK_RAW也可以处理特殊的IPv4报文;此外,利用原始套接字,可以通过IP_HDRINCL套接字选项由用户构造IP头。
    2106. socket.SOCK_RDM # 是一种可靠的UDP形式,即保证交付数据报但不保证顺序。SOCK_RAM用来提供对原始协议的低级访问,在需要执行某些特殊操作时使用,如发送ICMP报文。SOCK_RAM通常仅限于高级用户或管理员运行的程序使用。
    2107. socket.SOCK_SEQPACKET # 可靠的连续数据包服务
    2108. socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 关闭server后马上释放端口,避免被TIME_WAIT占用
    2109. select [IO多路复用的机制]
    2110. # select每次遍历都需要把fd集合从用户态拷贝到内核态,开销较大,受系统限制最大1024
    2111. select.select(rlist, wlist, xlist[, timeout])
    2112. # poll和select很像 通过一个pollfd数组向内核传递需要关注的事件,没有描述符1024限制
    2113. select.poll()
    2114. # 创建epoll句柄,注册监听事件,通过回调函数等待事件产生,不做主动扫描,整个过程对fd只做一次拷贝.打开最大文件数后,不受限制,1GB内存大约是10万链接
    2115. select.epoll([sizehint=-1])
    2116. select.epoll
    2117. EPOLLIN # 监听可读事件
    2118. EPOLLET # 高速边缘触发模式,即触发后不会再次触发直到新接收数据
    2119. EPOLLOUT # 监听写事件
    2120. epoll.poll([timeout=-1[, maxevents=-1]]) # 等待事件,未指定超时时间[毫秒]则为一直阻塞等待
    2121. epoll.register(fd,EPOLLIN) # 向epoll句柄中注册,新来socket链接,监听可读事件
    2122. epoll.modify(fd, EPOLLET | EPOLLOUT) # 改变监听事件为边缘触发,监听写事件
    2123. epoll.fileno() # 通过链接对象得到fd
    2124. epoll.unregister(fd) # 取消fd监听事件
    2125. SocketServer
    2126. #!/usr/bin/python
    2127. #server.py
    2128. import SocketServer
    2129. import os
    2130. class MyTCP(SocketServer.BaseRequestHandler):
    2131. def handle(self):
    2132. # 应该已经封装好了 不需要这层while了 可能会引起大量 close_wait
    2133. while True:
    2134. self.data=self.request.recv(1024).strip()
    2135. if self.data == 'quit' or not self.data:break
    2136. cmd=os.popen(self.data).read()
    2137. if cmd == '':cmd= self.data + ': Command not found'
    2138. self.request.sendall(cmd)
    2139. if __name__ == '__main__':
    2140. HOST,PORT = '10.0.0.119',50007
    2141. server = SocketServer.ThreadingTCPServer((HOST,PORT),MyTCP)
    2142. server.serve_forever()
    2143. SocketClient
    2144. #!/usr/bin/python
    2145. #client.py
    2146. import socket
    2147. HOST='10.0.0.119'
    2148. PORT=50007
    2149. s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    2150. s.connect((HOST,PORT))
    2151. while True:
    2152. while True:
    2153. cmd=raw_input('CMD:').strip()
    2154. if cmd != '':break
    2155. s.sendall(cmd)
    2156. data=s.recv(1024).split('\n')
    2157. print 'cmd:'
    2158. for line in data:print line
    2159. s.close()
    2160. ftp
    2161. ftpserver
    2162. #!/usr/bin/python
    2163. #ftpserver.py
    2164. import SocketServer
    2165. import os
    2166. import cPickle
    2167. import md5
    2168. from time import sleep
    2169. def filer(file1):
    2170. try:
    2171. f = file(file1,'rb')
    2172. return cPickle.load(f)
    2173. except IOError:
    2174. return {}
    2175. except EOFError:
    2176. return {}
    2177. f.close()
    2178. def filew(file1,content):
    2179. f = file(file1,'wb')
    2180. cPickle.dump(content,f)
    2181. f.close()
    2182. class MyTCP(SocketServer.BaseRequestHandler):
    2183. def handle(self):
    2184. i = 0
    2185. while i<3:
    2186. user=self.request.recv(1024).strip()
    2187. userinfo=filer('user.pkl')
    2188. if userinfo.has_key(user.split()[0]):
    2189. if md5.new(user.split()[1]).hexdigest() == userinfo[user.split()[0]]:
    2190. results='login successful'
    2191. self.request.sendall(results)
    2192. login='successful'
    2193. break
    2194. else:
    2195. i = i + 1
    2196. results='Error:password not correct'
    2197. self.request.sendall(results)
    2198. continue
    2199. else:
    2200. i = i + 1
    2201. results='Error:password not correct'
    2202. self.request.sendall(results)
    2203. continue
    2204. break
    2205. else:
    2206. results = 'Error:Wrong password too many times'
    2207. self.request.sendall(results)
    2208. login='failure'
    2209. home_path = os.popen('pwd').read().strip() + '/' + user.split()[0]
    2210. current_path = '/'
    2211. print home_path
    2212. while True:
    2213. if login == 'failure':
    2214. break
    2215. print 'home_path:%s=current_path:%s' %(home_path,current_path)
    2216. cmd=self.request.recv(1024).strip()
    2217. print cmd
    2218. if cmd == 'quit':
    2219. break
    2220. elif cmd == 'dir':
    2221. list=os.listdir('%s%s' %(home_path,current_path))
    2222. if list:
    2223. dirlist,filelist = '',''
    2224. for i in list:
    2225. if os.path.isdir('%s%s%s' %(home_path,current_path,i)):
    2226. dirlist = dirlist + '\033[32m' + i + '\033[m\t'
    2227. else:
    2228. filelist = filelist + i + '\t'
    2229. results = dirlist + filelist
    2230. else:
    2231. results = '\033[31mnot find\033[m'
    2232. self.request.sendall(results)
    2233. elif cmd == 'pdir':
    2234. self.request.sendall(current_path)
    2235. elif cmd.split()[0] == 'mdir':
    2236. if cmd.split()[1].isalnum():
    2237. tmppath='%s%s%s' %(home_path,current_path,cmd.split()[1])
    2238. os.makedirs(tmppath)
    2239. self.request.sendall('\033[32mcreating successful\033[m')
    2240. else:
    2241. self.request.sendall('\033[31mcreate failure\033[m')
    2242. elif cmd.split()[0] == 'cdir':
    2243. if cmd.split()[1] == '/':
    2244. tmppath='%s%s' %(home_path,cmd.split()[1])
    2245. if os.path.isdir(tmppath):
    2246. current_path = cmd.split()[1]
    2247. self.request.sendall(current_path)
    2248. else:
    2249. self.request.sendall('\033[31mnot_directory\033[m')
    2250. elif cmd.split()[1].startswith('/'):
    2251. tmppath='%s%s' %(home_path,cmd.split()[1])
    2252. if os.path.isdir(tmppath):
    2253. current_path = cmd.split()[1] + '/'
    2254. self.request.sendall(current_path)
    2255. else:
    2256. self.request.sendall('\033[31mnot_directory\033[m')
    2257. else:
    2258. tmppath='%s%s%s' %(home_path,current_path,cmd.split()[1])
    2259. if os.path.isdir(tmppath):
    2260. current_path = current_path + cmd.split()[1] + '/'
    2261. self.request.sendall(current_path)
    2262. else:
    2263. self.request.sendall('\033[31mnot_directory\033[m')
    2264. elif cmd.split()[0] == 'get':
    2265. if os.path.isfile('%s%s%s' %(home_path,current_path,cmd.split()[1])):
    2266. f = file('%s%s%s' %(home_path,current_path,cmd.split()[1]),'rb')
    2267. self.request.sendall('ready_file')
    2268. sleep(0.5)
    2269. self.request.send(f.read())
    2270. f.close()
    2271. sleep(0.5)
    2272. elif os.path.isdir('%s%s%s' %(home_path,current_path,cmd.split()[1])):
    2273. self.request.sendall('ready_dir')
    2274. sleep(0.5)
    2275. for dirpath in os.walk('%s%s%s' %(home_path,current_path,cmd.split()[1])):
    2276. dir=dirpath[0].replace('%s%s' %(home_path,current_path),'',1)
    2277. self.request.sendall(dir)
    2278. sleep(0.5)
    2279. for filename in dirpath[2]:
    2280. self.request.sendall(filename)
    2281. sleep(0.5)
    2282. f = file('%s/%s' %(dirpath[0],filename),'rb')
    2283. self.request.send(f.read())
    2284. f.close()
    2285. sleep(0.5)
    2286. self.request.sendall('file_get_done')
    2287. sleep(0.5)
    2288. else:
    2289. self.request.sendall('dir_get_done')
    2290. sleep(0.5)
    2291. else:
    2292. self.request.sendall('get_failure')
    2293. continue
    2294. self.request.sendall('get_done')
    2295. elif cmd.split()[0] == 'send':
    2296. if os.path.exists('%s%s%s' %(home_path,current_path,cmd.split()[1])):
    2297. self.request.sendall('existing')
    2298. action=self.request.recv(1024)
    2299. if action == 'cancel':
    2300. continue
    2301. self.request.sendall('ready')
    2302. msg=self.request.recv(1024)
    2303. if msg == 'ready_file':
    2304. f = file('%s%s%s' %(home_path,current_path,cmd.split()[1]),'wb')
    2305. while True:
    2306. data=self.request.recv(1024)
    2307. if data == 'file_send_done':break
    2308. f.write(data)
    2309. f.close()
    2310. elif msg == 'ready_dir':
    2311. os.system('mkdir -p %s%s%s' %(home_path,current_path,cmd.split()[1]))
    2312. while True:
    2313. dir=self.request.recv(1024)
    2314. if dir == 'get_done':break
    2315. os.system('mkdir -p %s%s%s' %(home_path,current_path,dir))
    2316. while True:
    2317. filename=self.request.recv(1024)
    2318. if filename == 'dir_send_done':break
    2319. f = file('%s%s%s/%s' %(home_path,current_path,dir,filename),'wb')
    2320. while True:
    2321. data=self.request.recv(1024)
    2322. if data == 'file_send_done':break
    2323. f.write(data)
    2324. f.close()
    2325. self.request.sendall('%s/%s\t\033[32mfile_done\033[m' %(dir,filename))
    2326. self.request.sendall('%s\t\033[32mdir_done\033[m' %(dir))
    2327. elif msg == 'unknown_file':
    2328. continue
    2329. else:
    2330. results = cmd.split()[0] + ': Command not found'
    2331. self.request.sendall(results)
    2332. if __name__ == '__main__':
    2333. HOST,PORT = '10.152.14.85',50007
    2334. server = SocketServer.ThreadingTCPServer((HOST,PORT),MyTCP)
    2335. server.serve_forever()
    2336. ftpmanage
    2337. #!/usr/bin/python
    2338. #manage_ftp.py
    2339. import cPickle
    2340. import sys
    2341. import md5
    2342. import os
    2343. import getpass
    2344. def filer(file1):
    2345. try:
    2346. f = file(file1,'rb')
    2347. return cPickle.load(f)
    2348. except IOError:
    2349. return {}
    2350. except EOFError:
    2351. return {}
    2352. f.close()
    2353. def filew(file1,content):
    2354. f = file(file1,'wb')
    2355. cPickle.dump(content,f)
    2356. f.close()
    2357. while True:
    2358. print '''
    2359. 1.add user
    2360. 2.del user
    2361. 3.change password
    2362. 4.query user
    2363. 0.exit
    2364. '''
    2365. i = raw_input(':').strip()
    2366. userinfo=filer('user.pkl')
    2367. if i == '':
    2368. continue
    2369. elif i == '1':
    2370. while True:
    2371. user=raw_input('user name:').strip()
    2372. if user.isalnum():
    2373. i = 0
    2374. while i<3:
    2375. passwd=getpass.getpass('passwd:').strip()
    2376. if passwd == '':
    2377. continue
    2378. else:
    2379. passwd1=getpass.getpass('Confirm password:').strip()
    2380. if passwd == passwd1:
    2381. mpasswd = md5.new(passwd).hexdigest()
    2382. userinfo[user] = mpasswd
    2383. os.system('mkdir -p %s' %user)
    2384. print '%s creating successful ' %user
    2385. break
    2386. else:
    2387. print "Passwords don't match "
    2388. i = i + 1
    2389. continue
    2390. else:
    2391. print 'Too many wrong'
    2392. continue
    2393. break
    2394. else:
    2395. print 'user not legal'
    2396. continue
    2397. elif i == '2':
    2398. user=raw_input('user name:').strip()
    2399. if userinfo.has_key(user):
    2400. del userinfo[user]
    2401. print 'Delete users successfully'
    2402. else:
    2403. print 'user not exist'
    2404. continue
    2405. elif i == '3':
    2406. user=raw_input('user name:').strip()
    2407. if userinfo.has_key(user):
    2408. i = 0
    2409. while i<3:
    2410. passwd=getpass.getpass('passwd:').strip()
    2411. if passwd == '':
    2412. continue
    2413. else:
    2414. passwd1=getpass.getpass('Confirm password:').strip()
    2415. if passwd == passwd1:
    2416. mpasswd = md5.new(passwd).hexdigest()
    2417. userinfo[user] = mpasswd
    2418. print '%s password is changed' %user
    2419. break
    2420. else:
    2421. print "Passwords don't match "
    2422. i = i + 1
    2423. continue
    2424. else:
    2425. print 'Too many wrong'
    2426. continue
    2427. else:
    2428. print 'user not exist'
    2429. continue
    2430. elif i == '4':
    2431. print userinfo.keys()
    2432. elif i == '0':
    2433. sys.exit()
    2434. else:
    2435. print 'select error'
    2436. continue
    2437. filew('user.pkl',content=userinfo)
    2438. ftpclient
    2439. #!/usr/bin/python
    2440. #ftpclient.py
    2441. import socket
    2442. import os
    2443. import getpass
    2444. from time import sleep
    2445. HOST='10.152.14.85'
    2446. PORT=50007
    2447. s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    2448. s.connect((HOST,PORT))
    2449. while True:
    2450. user = raw_input('user:').strip()
    2451. if user.isalnum():
    2452. while True:
    2453. passwd = getpass.getpass('passwd:').strip()
    2454. s.sendall(user + ' ' + passwd)
    2455. servercmd=s.recv(1024)
    2456. if servercmd == 'login successful':
    2457. print '\033[32m%s\033[m' %servercmd
    2458. break
    2459. else:
    2460. print servercmd
    2461. while True:
    2462. cmd=raw_input('FTP>').strip()
    2463. if cmd == '':
    2464. continue
    2465. if cmd.split()[0] == 'get':
    2466. if cmd == 'get':continue
    2467. for i in cmd.split()[1:]:
    2468. if os.path.exists(i):
    2469. confirm = raw_input("\033[31mPlease confirm whether the cover %s(Y/N):\033[m" %(i)).upper().startswith('Y')
    2470. if not confirm:
    2471. print '%s cancel' %i
    2472. continue
    2473. s.sendall('get ' + i)
    2474. servercmd=s.recv(1024)
    2475. if servercmd == 'inexistence':
    2476. print '%s \t\033[32minexistence\033[m' %i
    2477. continue
    2478. elif servercmd == 'ready_file':
    2479. f = file(i,'wb')
    2480. while True:
    2481. data=s.recv(1024)
    2482. if data == 'get_done':break
    2483. f.write(data)
    2484. f.close()
    2485. print '%s \t\033[32mfile_done\033[m' %(i)
    2486. elif servercmd == 'ready_dir':
    2487. try:
    2488. os.makedirs(i)
    2489. except:
    2490. pass
    2491. while True:
    2492. serverdir=s.recv(1024)
    2493. if serverdir == 'get_done':break
    2494. os.system('mkdir -p %s' %serverdir)
    2495. print '%s \t\033[32mdir_done\033[m' %(serverdir)
    2496. while True:
    2497. serverfile=s.recv(1024)
    2498. if serverfile == 'dir_get_done':break
    2499. f = file('%s/%s' %(serverdir,serverfile),'wb')
    2500. while True:
    2501. data=s.recv(1024)
    2502. if data == 'file_get_done':break
    2503. f.write(data)
    2504. f.close()
    2505. print '%s/%s \t\033[32mfile_done\033[m' %(serverdir,serverfile)
    2506. elif cmd.split()[0] == 'send':
    2507. if cmd == 'send':continue
    2508. for i in cmd.split()[1:]:
    2509. if not os.path.exists(i):
    2510. print '%s\t\033[31minexistence\033[m' %i
    2511. continue
    2512. s.sendall('send ' + i)
    2513. servercmd=s.recv(1024)
    2514. if servercmd == 'existing':
    2515. confirm = raw_input("\033[31mPlease confirm whether the cover %s(Y/N):\033[m" %(i)).upper().startswith('Y')
    2516. if confirm:
    2517. s.sendall('cover')
    2518. servercmd=s.recv(1024)
    2519. else:
    2520. s.sendall('cancel')
    2521. print '%s\tcancel' %i
    2522. continue
    2523. if os.path.isfile(i):
    2524. s.sendall('ready_file')
    2525. sleep(0.5)
    2526. f = file(i,'rb')
    2527. s.send(f.read())
    2528. sleep(0.5)
    2529. s.sendall('file_send_done')
    2530. print '%s\t\033[32mfile done\033[m' %(cmd.split()[1])
    2531. f.close()
    2532. elif os.path.isdir(i):
    2533. s.sendall('ready_dir')
    2534. sleep(0.5)
    2535. for dirpath in os.walk(i):
    2536. dir=dirpath[0].replace('%s/' %os.popen('pwd').read().strip(),'',1)
    2537. s.sendall(dir)
    2538. sleep(0.5)
    2539. for filename in dirpath[2]:
    2540. s.sendall(filename)
    2541. sleep(0.5)
    2542. f = file('%s/%s' %(dirpath[0],filename),'rb')
    2543. s.send(f.read())
    2544. f.close()
    2545. sleep(0.5)
    2546. s.sendall('file_send_done')
    2547. msg=s.recv(1024)
    2548. print msg
    2549. else:
    2550. s.sendall('dir_send_done')
    2551. msg=s.recv(1024)
    2552. print msg
    2553. else:
    2554. s.sendall('unknown_file')
    2555. print '%s\t\033[31munknown type\033[m' %i
    2556. continue
    2557. sleep(0.5)
    2558. s.sendall('get_done')
    2559. elif cmd.split()[0] == 'cdir':
    2560. if cmd == 'cdir':continue
    2561. s.sendall(cmd)
    2562. data=s.recv(1024)
    2563. print data
    2564. continue
    2565. elif cmd == 'ls':
    2566. list=os.popen(cmd).read().strip().split('\n')
    2567. if list:
    2568. dirlist,filelist = '',''
    2569. for i in list:
    2570. if os.path.isdir(i):
    2571. dirlist = dirlist + '\033[32m' + i + '\033[m\t'
    2572. else:
    2573. filelist = filelist + i + '\t'
    2574. results = dirlist + filelist
    2575. else:
    2576. results = '\033[31mnot find\033[m'
    2577. print results
    2578. continue
    2579. elif cmd == 'pwd':
    2580. os.system(cmd)
    2581. elif cmd.split()[0] == 'cd':
    2582. try:
    2583. os.chdir(cmd.split()[1])
    2584. except:
    2585. print '\033[31mcd failure\033[m'
    2586. elif cmd == 'dir':
    2587. s.sendall(cmd)
    2588. data=s.recv(1024)
    2589. print data
    2590. continue
    2591. elif cmd == 'pdir':
    2592. s.sendall(cmd)
    2593. data=s.recv(1024)
    2594. print data
    2595. continue
    2596. elif cmd.split()[0] == 'mdir':
    2597. if cmd == 'mdir':continue
    2598. s.sendall(cmd)
    2599. data=s.recv(1024)
    2600. print data
    2601. continue
    2602. elif cmd.split()[0] == 'help':
    2603. print '''
    2604. get [file] [dir]
    2605. send [file] [dir]
    2606. dir
    2607. mdir
    2608. cdir
    2609. pdir
    2610. pwd
    2611. md
    2612. cd
    2613. ls
    2614. help
    2615. quit
    2616. '''
    2617. continue
    2618. elif cmd == 'quit':
    2619. break
    2620. else:
    2621. print '\033[31m%s: Command not found,Please see the "help"\033[m' %cmd
    2622. else:
    2623. continue
    2624. break
    2625. s.close()
    2626. 扫描主机开放端口
    2627. #!/usr/bin/env python
    2628. import socket
    2629. def check_server(address,port):
    2630. s=socket.socket()
    2631. try:
    2632. s.connect((address,port))
    2633. return True
    2634. except socket.error,e:
    2635. return False
    2636. if __name__=='__main__':
    2637. from optparse import OptionParser
    2638. parser=OptionParser()
    2639. parser.add_option("-a","--address",dest="address",default='localhost',help="Address for server",metavar="ADDRESS")
    2640. parser.add_option("-s","--start",dest="start_port",type="int",default=1,help="start port",metavar="SPORT")
    2641. parser.add_option("-e","--end",dest="end_port",type="int",default=1,help="end port",metavar="EPORT")
    2642. (options,args)=parser.parse_args()
    2643. print 'options: %s, args: %s' % (options, args)
    2644. port=options.start_port
    2645. while(port<=options.end_port):
    2646. check = check_server(options.address, port)
    2647. if (check):
    2648. print 'Port %s is on' % port
    2649. port=port+1
    2650. zmq [网络通讯库]
    2651. # https://github.com/zeromq/pyzmq
    2652. # pip install pyzmq
    2653. # ZMQ是一个开源的、跨语言的、非常简洁的、非常高性能、非常灵活的网络通讯库
    2654. 服务端程序
    2655. import zmq
    2656. context = zmq.Context()
    2657. socket = context.socket(zmq.REP)
    2658. socket.bind("tcp://127.0.0.1:1234") # 提供传输协议 INPROC IPC MULTICAST TCP
    2659. while True :
    2660. msg = socket.recv()
    2661. socket.send(msg)
    2662. 客户端端程序
    2663. import zmq
    2664. context = zmq.Context()
    2665. socket = context.socket(zmq.REQ)
    2666. socket.connect("tcp://127.0.0.1:1234")
    2667. # socket.connect("tcp://127.0.0.1:6000") # 设置2个可以均衡负载请求到2个监听的server
    2668. msg_send = "xxx"socket.send(msg_send)
    2669. print "Send:", msg_send
    2670. msg_recv = socket.recv()
    2671. print "Receive:", msg_recv
    2672. epoll
    2673. https://docs.python.org/2/library/select.html # python官网
    2674. epoll短链接server
    2675. # 原文 http://my.oschina.net/moooofly/blog/147297
    2676. # 此代码还有改进地方,在接收数据和发送数据都是阻塞死循环处理,必须等待全部接收完毕才会继续操作
    2677. server端代码:
    2678. #!/usr/bin/python
    2679. #-*- coding:utf-8 -*-
    2680. import socket, logging
    2681. import select, errno
    2682. logger = logging.getLogger("network-server")
    2683. def InitLog():
    2684. logger.setLevel(logging.DEBUG)
    2685. fh = logging.FileHandler("network-server.log")
    2686. fh.setLevel(logging.DEBUG)
    2687. ch = logging.StreamHandler()
    2688. ch.setLevel(logging.ERROR)
    2689. formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    2690. ch.setFormatter(formatter)
    2691. fh.setFormatter(formatter)
    2692. logger.addHandler(fh)
    2693. logger.addHandler(ch)
    2694. if __name__ == "__main__":
    2695. InitLog()
    2696. try:
    2697. # 创建 TCP socket 作为监听 socket
    2698. listen_fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    2699. except socket.error, msg:
    2700. logger.error("create socket failed")
    2701. try:
    2702. # 设置 SO_REUSEADDR 选项
    2703. listen_fd.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    2704. except socket.error, msg:
    2705. logger.error("setsocketopt SO_REUSEADDR failed")
    2706. try:
    2707. # 进行 bind -- 此处未指定 ip 地址,即 bind 了全部网卡 ip 上
    2708. listen_fd.bind(('', 2003))
    2709. except socket.error, msg:
    2710. logger.error("bind failed")
    2711. try:
    2712. # 设置 listen 的 backlog 数
    2713. listen_fd.listen(10)
    2714. except socket.error, msg:
    2715. logger.error(msg)
    2716. try:
    2717. # 创建 epoll 句柄
    2718. epoll_fd = select.epoll()
    2719. # 向 epoll 句柄中注册 监听 socket 的 可读 事件
    2720. epoll_fd.register(listen_fd.fileno(), select.EPOLLIN)
    2721. except select.error, msg:
    2722. logger.error(msg)
    2723. connections = {}
    2724. addresses = {}
    2725. datalist = {}
    2726. while True:
    2727. # epoll 进行 fd 扫描的地方 -- 未指定超时时间则为阻塞等待
    2728. epoll_list = epoll_fd.poll()
    2729. for fd, events in epoll_list:
    2730. # 若为监听 fd 被激活
    2731. if fd == listen_fd.fileno():
    2732. # 进行 accept -- 获得连接上来 client 的 ip 和 port,以及 socket 句柄
    2733. conn, addr = listen_fd.accept()
    2734. logger.debug("accept connection from %s, %d, fd = %d" % (addr[0], addr[1], conn.fileno()))
    2735. # 将连接 socket 设置为 非阻塞
    2736. conn.setblocking(0)
    2737. # 向 epoll 句柄中注册 连接 socket 的 可读 事件
    2738. epoll_fd.register(conn.fileno(), select.EPOLLIN | select.EPOLLET)
    2739. # 将 conn 和 addr 信息分别保存起来
    2740. connections[conn.fileno()] = conn
    2741. addresses[conn.fileno()] = addr
    2742. elif select.EPOLLIN & events:
    2743. # 有 可读 事件激活
    2744. datas = ''
    2745. while True:
    2746. try:
    2747. # 从激活 fd 上 recv 10 字节数据
    2748. data = connections[fd].recv(10)
    2749. # 若当前没有接收到数据,并且之前的累计数据也没有
    2750. if not data and not datas:
    2751. # 从 epoll 句柄中移除该 连接 fd
    2752. epoll_fd.unregister(fd)
    2753. # server 侧主动关闭该 连接 fd
    2754. connections[fd].close()
    2755. logger.debug("%s, %d closed" % (addresses[fd][0], addresses[fd][1]))
    2756. break
    2757. else:
    2758. # 将接收到的数据拼接保存在 datas 中
    2759. datas += data
    2760. except socket.error, msg:
    2761. # 在 非阻塞 socket 上进行 recv 需要处理 读穿 的情况
    2762. # 这里实际上是利用 读穿 出 异常 的方式跳到这里进行后续处理
    2763. if msg.errno == errno.EAGAIN:
    2764. logger.debug("%s receive %s" % (fd, datas))
    2765. # 将已接收数据保存起来
    2766. datalist[fd] = datas
    2767. # 更新 epoll 句柄中连接d 注册事件为 可写
    2768. epoll_fd.modify(fd, select.EPOLLET | select.EPOLLOUT)
    2769. break
    2770. else:
    2771. # 出错处理
    2772. epoll_fd.unregister(fd)
    2773. connections[fd].close()
    2774. logger.error(msg)
    2775. break
    2776. elif select.EPOLLHUP & events:
    2777. # 有 HUP 事件激活
    2778. epoll_fd.unregister(fd)
    2779. connections[fd].close()
    2780. logger.debug("%s, %d closed" % (addresses[fd][0], addresses[fd][1]))
    2781. elif select.EPOLLOUT & events:
    2782. # 有 可写 事件激活
    2783. sendLen = 0
    2784. # 通过 while 循环确保将 buf 中的数据全部发送出去
    2785. while True:
    2786. # 将之前收到的数据发回 client -- 通过 sendLen 来控制发送位置
    2787. sendLen += connections[fd].send(datalist[fd][sendLen:])
    2788. # 在全部发送完毕后退出 while 循环
    2789. if sendLen == len(datalist[fd]):
    2790. break
    2791. # 更新 epoll 句柄中连接 fd 注册事件为 可读
    2792. epoll_fd.modify(fd, select.EPOLLIN | select.EPOLLET)
    2793. else:
    2794. # 其他 epoll 事件不进行处理
    2795. continue
    2796. client 端代码
    2797. import socket
    2798. import time
    2799. import logging
    2800. logger = logging.getLogger("network-client")
    2801. logger.setLevel(logging.DEBUG)
    2802. fh = logging.FileHandler("network-client.log")
    2803. fh.setLevel(logging.DEBUG)
    2804. ch = logging.StreamHandler()
    2805. ch.setLevel(logging.ERROR)
    2806. formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    2807. ch.setFormatter(formatter)
    2808. fh.setFormatter(formatter)
    2809. logger.addHandler(fh)
    2810. logger.addHandler(ch)
    2811. if __name__ == "__main__":
    2812. try:
    2813. connFd = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    2814. except socket.error, msg:
    2815. logger.error(msg)
    2816. try:
    2817. connFd.connect(("127.0.0.1", 2003))
    2818. logger.debug("connect to network server success")
    2819. except socket.error,msg:
    2820. logger.error(msg)
    2821. for i in range(1, 11):
    2822. data = "The Number is %d" % i
    2823. if connFd.send(data) != len(data):
    2824. logger.error("send data to network server failed")
    2825. break
    2826. readData = connFd.recv(1024)
    2827. print readData
    2828. time.sleep(1)
    2829. connFd.close()
    2830. 4 mysql
    2831. # yum install mysql-devel python-tools gcc openssl-devel
    2832. pip install MySQL-python
    2833. # yum install python-MySQLdb MySQL-python
    2834. help(MySQLdb.connections.Connection) # 查看链接参数
    2835. conn=MySQLdb.connect(host='localhost',user='root',passwd='123456',db='fortress',port=3306) # 定义连接
    2836. #conn=MySQLdb.connect(unix_socket='/var/run/mysqld/mysqld.sock',user='root',passwd='123456') # 使用socket文件链接
    2837. conn.autocommit(True) # 自动提交
    2838. cur=conn.cursor() # 定义游标
    2839. conn.select_db('fortress') # 选择数据库
    2840. sqlcmd = 'insert into user(name,age) value(%s,%s)' # 定义sql命令
    2841. cur.executemany(sqlcmd,[('aa',1),('bb',2),('cc',3)]) # 插入多条值
    2842. cur.execute('delete from user where id=20') # 删除一条记录
    2843. cur.execute("update user set name='a' where id=20") # 更细数据
    2844. sqlresult = cur.fetchall() # 接收全部返回结果
    2845. conn.commit() # 提交
    2846. cur.close() # 关闭游标
    2847. conn.close() # 关闭连接
    2848. import MySQLdb
    2849. def mydb(dbcmdlist):
    2850. try:
    2851. conn=MySQLdb.connect(host='localhost',user='root',passwd='123456',db='fortress',port=3306)
    2852. conn.autocommit(True)
    2853. cur=conn.cursor()
    2854. cur.execute('create database if not exists fortress;') # 创建数据库
    2855. conn.select_db('fortress') # 选择数据库
    2856. cur.execute('drop table if exists log;') # 删除表
    2857. cur.execute('CREATE TABLE log ( id BIGINT(20) NOT NULL AUTO_INCREMENT, loginuser VARCHAR(50) DEFAULT NULL, remoteip VARCHAR(50) DEFAULT NULL, PRIMARY KEY (id) );') # 创建表
    2858. result=[]
    2859. for dbcmd in dbcmdlist:
    2860. cur.execute(dbcmd) # 执行sql
    2861. sqlresult = cur.fetchall() # 接收全部返回结果
    2862. result.append(sqlresult)
    2863. conn.commit() # 提交
    2864. cur.close()
    2865. conn.close()
    2866. return result
    2867. except MySQLdb.Error,e:
    2868. print 'mysql error msg: ',e
    2869. sqlcmd=[]
    2870. sqlcmd.append("insert into log (loginuser,remoteip)values('%s','%s');" %(loginuser,remoteip))
    2871. mydb(sqlcmd)
    2872. sqlcmd=[]
    2873. sqlcmd.append("select * from log;")
    2874. result = mydb(sqlcmd)
    2875. for i in result[0]:
    2876. print i
    2877. mysql链接失败重试
    2878. import MySQLdb as mysql
    2879. import time
    2880. class my():
    2881. def executeSQL(self, sql="select * from `serverinfo` limit 1;"):
    2882. while True:
    2883. try:
    2884. self.conn.ping()
    2885. break
    2886. except Exception,e:
    2887. print('warning: mysql test ping fail')
    2888. print(str(e))
    2889. try:
    2890. self.conn = mysql.connect(user="opsdeploy", passwd="123456", host='172.222.50.50', port=3306, db="ops_deploy", connect_timeout=10, compress=True, charset="utf8")
    2891. self.cursor = self.conn.cursor()
    2892. break
    2893. except Exception,e:
    2894. print("mysql reconnect fail ...")
    2895. print(str(e))
    2896. time.sleep(2)
    2897. try:
    2898. self.cursor.execute(sql)
    2899. self.conn.commit()
    2900. print self.cursor.fetchall()
    2901. except Exception,e:
    2902. print(str(e))
    2903. m=my()
    2904. m.executeSQL()
    2905. 5 处理信号
    2906. 信号的概念
    2907. 信号(signal): 进程之间通讯的方式,是一种软件中断。一个进程一旦接收到信号就会打断原来的程序执行流程来处理信号。
    2908. 发送信号一般有两种原因:
    2909. 1(被动式) 内核检测到一个系统事件.例如子进程退出会像父进程发送SIGCHLD信号.键盘按下control+c会发送SIGINT信号
    2910. 2(主动式) 通过系统调用kill来向指定进程发送信号
    2911. 操作系统规定了进程收到信号以后的默认行为,可以通过绑定信号处理函数来修改进程收到信号以后的行为,有两个信号是不可更改的 SIGTOP SIGKILL
    2912. 如果一个进程收到一个SIGUSR1信号,然后执行信号绑定函数,第二个SIGUSR2信号又来了,第一个信号没有被处理完毕的话,第二个信号就会丢弃。
    2913. 进程结束信号 SIGTERM SIGKILL 的区别: SIGTERM 比较友好,进程能捕捉这个信号,根据您的需要来关闭程序。在关闭程序之前,您可以结束打开的记录文件和完成正在做的任务。在某些情况下,假如进程正在进行作业而且不能中断,那么进程可以忽略这个SIGTERM信号。
    2914. 常见信号
    2915. kill -l # 查看linux提供的信号
    2916. SIGHUP 1 A # 终端挂起或者控制进程终止
    2917. SIGINT 2 A # 键盘终端进程(如control+c)
    2918. SIGQUIT 3 C # 键盘的退出键被按下
    2919. SIGILL 4 C # 非法指令
    2920. SIGABRT 6 C # 由abort(3)发出的退出指令
    2921. SIGFPE 8 C # 浮点异常
    2922. SIGKILL 9 AEF # Kill信号 立刻停止
    2923. SIGSEGV 11 C # 无效的内存引用
    2924. SIGPIPE 13 A # 管道破裂: 写一个没有读端口的管道
    2925. SIGALRM 14 A # 闹钟信号 由alarm(2)发出的信号
    2926. SIGTERM 15 A # 终止信号,可让程序安全退出 kill -15
    2927. SIGUSR1 30,10,16 A # 用户自定义信号1
    2928. SIGUSR2 31,12,17 A # 用户自定义信号2
    2929. SIGCHLD 20,17,18 B # 子进程结束自动向父进程发送SIGCHLD信号
    2930. SIGCONT 19,18,25 # 进程继续(曾被停止的进程)
    2931. SIGSTOP 17,19,23 DEF # 终止进程
    2932. SIGTSTP 18,20,24 D # 控制终端(tty)上按下停止键
    2933. SIGTTIN 21,21,26 D # 后台进程企图从控制终端读
    2934. SIGTTOU 22,22,27 D # 后台进程企图从控制终端写
    2935. 缺省处理动作一项中的字母含义如下:
    2936. A 缺省的动作是终止进程
    2937. B 缺省的动作是忽略此信号,将该信号丢弃,不做处理
    2938. C 缺省的动作是终止进程并进行内核映像转储(dump core),内核映像转储是指将进程数据在内存的映像和进程在内核结构中的部分内容以一定格式转储到文件系统,并且进程退出执行,这样做的好处是为程序员提供了方便,使得他们可以得到进程当时执行时的数据值,允许他们确定转储的原因,并且可以调试他们的程序。
    2939. D 缺省的动作是停止进程,进入停止状况以后还能重新进行下去,一般是在调试的过程中(例如ptrace系统调用)
    2940. E 信号不能被捕获
    2941. F 信号不能被忽略
    2942. Python提供的信号
    2943. import signal
    2944. dir(signal)
    2945. ['NSIG', 'SIGABRT', 'SIGALRM', 'SIGBUS', 'SIGCHLD', 'SIGCLD', 'SIGCONT', 'SIGFPE', 'SIGHUP', 'SIGILL', 'SIGINT', 'SIGIO', 'SIGIOT', 'SIGKILL', 'SIGPIPE', 'SIGPOLL', 'SIGPROF', 'SIGPWR', 'SIGQUIT', 'SIGRTMAX', 'SIGRTMIN', 'SIGSEGV', 'SIGSTOP', 'SIGSYS', 'SIGTERM', 'SIGTRAP', 'SIGTSTP', 'SIGTTIN', 'SIGTTOU', 'SIGURG', 'SIGUSR1', 'SIGUSR2', 'SIGVTALRM', 'SIGWINCH', 'SIGXCPU', 'SIGXFSZ', 'SIG_DFL', 'SIG_IGN', '__doc__', '__name__', 'alarm', 'default_int_handler', 'getsignal', 'pause', 'signal']
    2946. 绑定信号处理函数
    2947. #encoding:utf8
    2948. import os,signal
    2949. from time import sleep
    2950. def onsignal_term(a,b):
    2951. print 'SIGTERM' # kill -15
    2952. signal.signal(signal.SIGTERM,onsignal_term) # 接收信号,执行相应函数
    2953. def onsignal_usr1(a,b):
    2954. print 'SIGUSR1' # kill -10
    2955. signal.signal(signal.SIGUSR1,onsignal_usr1)
    2956. while 1:
    2957. print 'ID',os.getpid()
    2958. sleep(10)
    2959. 通过另外一个进程发送信号
    2960. import os,signal
    2961. os.kill(16175,signal.SIGTERM) # 发送信号,16175是绑定信号处理函数的进程pid,需要自行修改
    2962. os.kill(16175,signal.SIGUSR1)
    2963. 父进程接收子进程结束发送的SIGCHLD信号
    2964. #encoding:utf8
    2965. import os,signal
    2966. from time import sleep
    2967. def onsigchld(a,b):
    2968. print '收到子进程结束信号'
    2969. signal.signal(signal.SIGCHLD,onsigchld)
    2970. pid = os.fork() # 创建一个子进程,复制父进程所有资源操作
    2971. if pid == 0: # 通过判断子进程os.fork()是否等于0,分别同时执行父进程与子进程操作
    2972. print '我是子进程,pid是',os.getpid()
    2973. sleep(2)
    2974. else:
    2975. print '我是父进程,pid是',os.getpid()
    2976. os.wait() # 等待子进程结束
    2977. 接收信号的程序,另外一端使用多线程向这个进程发送信号,会遗漏一些信号
    2978. #encoding:utf8
    2979. import os
    2980. import signal
    2981. from time import sleep
    2982. import Queue
    2983. QCOUNT = Queue.Queue() # 初始化队列
    2984. def onsigchld(a,b):
    2985. '''收到信号后向队列中插入一个数字1'''
    2986. print '收到SIGUSR1信号'
    2987. sleep(1)
    2988. QCOUNT.put(1) # 向队列中写入
    2989. signal.signal(signal.SIGUSR1,onsigchld) # 绑定信号处理函数
    2990. while 1:
    2991. print '我的pid是',os.getpid()
    2992. print '现在队列中元素的个数是',QCOUNT.qsize()
    2993. sleep(2)
    2994. 多线程发信号端的程序
    2995. #encoding:utf8
    2996. import threading
    2997. import os
    2998. import signal
    2999. def sendusr1():
    3000. print '发送信号'
    3001. os.kill(17788, signal.SIGUSR1) # 这里的进程id需要写前一个程序实际运行的pid
    3002. WORKER = []
    3003. for i in range(1, 7): # 开启6个线程
    3004. threadinstance = threading.Thread(target = sendusr1)
    3005. WORKER.append(threadinstance)
    3006. for i in WORKER:
    3007. i.start()
    3008. for i in WORKER:
    3009. i.join()
    3010. print '主线程完成'
    3011. 6 缓存数据库
    3012. python使用memcache
    3013. easy_install python-memcached # 安装(python2.7+)
    3014. import memcache
    3015. mc = memcache.Client(['10.152.14.85:12000'],debug=True) # 也可以使用socket直接连接IP端口
    3016. mc.set('name','luo',60)
    3017. mc.get('name')
    3018. mc.delete('name1')
    3019. # 豆瓣的python-memcache模块,大于1M自动切割 性能是纯python的3倍+
    3020. https://code.google.com/p/python-libmemcached/
    3021. 保存数据
    3022. set(key,value,timeout) # 把key映射到value,timeout指的是什么时候这个映射失效
    3023. add(key,value,timeout) # 仅当存储空间中不存在键相同的数据时才保存
    3024. replace(key,value,timeout) # 仅当存储空间中存在键相同的数据时才保存
    3025. 获取数据
    3026. get(key) # 返回key所指向的value
    3027. get_multi(key1,key2,key3) # 可以非同步地同时取得多个键值, 比循环调用get快数十倍
    3028. python使用mongodb
    3029. # 新版本
    3030. http://api.mongodb.org/python/2.7.2/tutorial.html
    3031. http://api.mongodb.org/python/current/examples/custom_type.html
    3032. easy_install pymongo # 安装
    3033. import pymongo
    3034. cl = pymongo.MongoClient("127.0.0.1", 27017)
    3035. db = cl.ops # 选择库
    3036. db.name # 查看库名
    3037. db.collection_names() # 查看所有文档
    3038. db.project # 选择文档
    3039. db.project.insert({'name':'live','group':'a'})
    3040. db.project.insert({'name':'news','group':'b'})
    3041. db.project.find_one({'group':'a'})
    3042. for post in db.project.find():
    3043. print post['name']
    3044. db.project.remove()
    3045. # 执行mongo命令
    3046. # https://api.mongodb.com/python/current/api/pymongo/database.html
    3047. db.command("filemd5", object_id, root=file_root)
    3048. db.command("dropUser", "user")
    3049. db.command("createUser", "admin", pwd="password", roles=["root"])
    3050. for x,y in db.command("currentOp").items():
    3051. print x,y
    3052. # currentOp在mongo3.9废弃,建议使用 aggregate()
    3053. with client.admin.aggregate([{"$currentOp": {}}]) as cursor:
    3054. for operation in cursor:
    3055. print(operation)
    3056. python使用redis
    3057. https://pypi.python.org/pypi/redis # redis的python官网
    3058. pip install redis OR easy_install redis # 安装
    3059. http://redis.readthedocs.org/en/latest/index.html # redis命令详解
    3060. http://redis.readthedocs.org/en/2.4/index.html
    3061. import redis
    3062. rds = redis.Redis(host=host, port=port, password=passwd, socket_timeout=10,db=0)
    3063. rds.info() # redis信息
    3064. rds.set(key, value) # 将值value关联到key
    3065. rds.get(key) # 取key值
    3066. rds.delete(key1,key2) # 删除key
    3067. rds.rename(key,new_key2) # 将key改名 存在覆盖
    3068. rds.seten(key,value) # 将值value关联到key,如果key存在不做任何动作
    3069. rds.setex(key, value, 10800) # 将值value关联到key,并设置key的过期时间
    3070. rds.mset() # 同时设置一个或多个key-value对 如果key存在则覆盖
    3071. rds.msetnx() # 同时设置一个或多个key-value对 如果有key存在则失败
    3072. rds.mget(key1, key2, key3) # 取多个key值 不存在返回nil
    3073. rds.expire(key seconds) # 设置key的过期时间
    3074. rds.persist(key) # 移除key的过期时间
    3075. rds.ttl(key) # 查看超时时间 -1为不过期
    3076. rds.sadd(key,value1) # 将value1加入集合中 集合不重复
    3077. rds.smembers(key) # 返回key中所有成员
    3078. rds.scard(key) # 集合中元素的数量
    3079. rds.srandmember(key) # 对集合随机返回一个元素 而不对集合改动 当key不存在或key是空集时,返回nil
    3080. rds.sinter(key1,key2) # 两个集合的交集
    3081. rds.sdiff(key1,key2) # 两个集合的差集
    3082. rds.sismember(key,value) # 判断value元素是否是集合key的成员 1存在 0不存在
    3083. rds.lpush(key,value1) # 将value1加入列表中 从左到右
    3084. rds.lpop(key,value1) # 移除并返回列表key的头元素
    3085. rds.llen(key) # 返回列表长度
    3086. rds.sort(key) # 对列表、集合、有序集合排序[大列表排序非常影响性能,甚至把redis拖死]
    3087. rds.append(key,value) # 字符串拼接为新的value
    3088. rds.ltrim(key, 0, -10) # 保留指定区间内的元素,不在都被删除 0第一个 -1最后一个
    3089. rds.incr(key , amount=1) # 计数加1 默认1或请先设置key的数值
    3090. rds.decr(key) # 计数减1 请先设置key的数值
    3091. rds.save() # 保存数据
    3092. python使用kestrel队列
    3093. # pykestrel
    3094. import kestrel
    3095. q = kestrel.Client(servers=['127.0.0.1:22133'],queue='test_queue')
    3096. q.add('some test job')
    3097. job = q.get() # 从队列读取工作
    3098. job = q.peek() # 读取下一份工作
    3099. # 读取一组工作
    3100. while True:
    3101. job = q.next(timeout=10) # 完成工作并获取下一个工作,如果没有工作,则等待10秒
    3102. if job is not None:
    3103. try:
    3104. # 流程工作
    3105. except:
    3106. q.abort() # 标记失败工作
    3107. q.finish() # 完成最后工作
    3108. q.close() # 关闭连接
    3109. kestrel状态检查
    3110. # kestrel支持memcache协议客户端
    3111. #!/usr/local/bin/python
    3112. # 10.13.81.125 22133 10000
    3113. import memcache
    3114. import sys
    3115. import traceback
    3116. ip="%s:%s" % (sys.argv[1],sys.argv[2])
    3117. try:
    3118. mc = memcache.Client([ip,])
    3119. st=mc.get_stats()
    3120. except:
    3121. print "kestrel connection exception"
    3122. sys.exit(2)
    3123. if st:
    3124. for s in st[0][1].keys():
    3125. if s.startswith('queue_') and s.endswith('_mem_items'):
    3126. num = int(st[0][1][s])
    3127. if num > int(sys.argv[3]):
    3128. print "%s block to %s" %(s[6:-6],num)
    3129. sys.exit(2)
    3130. print "kestrel ok!"
    3131. sys.exit(0)
    3132. else:
    3133. print "kestrel down"
    3134. sys.exit(2)
    3135. python使用tarantool
    3136. # pip install tarantool-queue
    3137. from tarantool_queue import Queue
    3138. queue = Queue("localhost", 33013, 0) # 连接读写端口 空间0
    3139. tube = queue.tube("name_of_tube") #
    3140. tube.put([1, 2, 3])
    3141. task = tube.take()
    3142. task.data # take task and read data from it
    3143. task.ack() # move this task into state DONE
    3144. python-etcd
    3145. http://python-etcd.readthedocs.io/en/latest/
    3146. pip install python-etcd
    3147. import etcd
    3148. client = etcd.Client(host='etcd-01', port=2379)
    3149. client = etcd.Client( (('etcd-01', 2379), ('etcd-02', 2379), ('etcd-03', 2379)) ,allow_reconnect=True) # 集群多IP allow_reconnect 允许重连
    3150. # 增加 目录必须存在 # 目录: /v1/xuesong/
    3151. client.write('/v1/xuesong/10.10.10.10:8080', 'test')
    3152. # 获取指定路径的值
    3153. r = client.read('/v1/xuesong/10.10.10.10:8080' , recursive=True, sorted=True)
    3154. r.value
    3155. # 删除指定路径
    3156. client.delete('/v1/xuesong/10.10.10.10:8080')
    3157. # with ttl
    3158. client.write('/nodes/n2', 2, ttl=4) # sets the ttl to 4 seconds
    3159. # create only
    3160. client.write('/nodes/n3', 'test', prevExist=False)
    3161. # Compare and swap values atomically
    3162. client.write('/nodes/n3', 'test2', prevValue='test1') #this fails to write
    3163. client.write('/nodes/n3', 'test2', prevIndex=10) #this fails to write
    3164. # mkdir
    3165. client.write('/nodes/queue', None, dir=True)
    3166. # Append a value to a queue dir
    3167. client.write('/nodes/queue', 'test', append=True) #will write i.e. /nodes/queue/11
    3168. client.write('/nodes/queue', 'test2', append=True) #will write i.e. /nodes/queue/12
    3169. client.read('/nodes/n2').value # 获取单个键值
    3170. r = client.read('/nodes', recursive=True, sorted=True) # 递归查询目录
    3171. for i in r.children:
    3172. if not i.dir:
    3173. print("%s: %s" % (child.key,child.value))
    3174. client.read('/nodes/n2', wait=True) #Waits for a change in value in the key before returning.
    3175. client.read('/nodes/n2', wait=True, waitIndex=10)
    3176. try:
    3177. client.read('/invalid/path')
    3178. except etcd.EtcdKeyNotFound:
    3179. print "error"
    3180. client.delete('/nodes/n1')
    3181. client.delete('/nodes', dir=True) #spits an error if dir is not empty
    3182. client.delete('/nodes', recursive=True) #this works recursively
    3183. client.watch('/nodes/n1', recursive=True,timeout=0) # 递归获取改变值 阻塞直到有改变
    3184. # watch只会阻塞监视之后的一次改动,所以必须先递归read下所有路径,然后根据每次的watch进行更改
    3185. # 第一次read的时候,需要记录 etcd_index+1作为下一次watch的索引
    3186. index = client.read('/nodes/n1', recursive=True).etcd_index
    3187. while 1:
    3188. # watch后的索引是 modifiedIndex+1传给下一次的watch
    3189. index = client.watch('/nodes/n1', recursive=True, timeout=0, index=index+1).modifiedIndex
    3190. python操作zookeeper
    3191. https://kazoo.readthedocs.io/en/latest/basic_usage.html
    3192. pip install kazoo
    3193. from kazoo.client import KazooClient
    3194. zk = KazooClient(hosts='127.0.0.1:2181', read_only=True)
    3195. zk.start()
    3196. zk.get_children('/')
    3197. zk.stop()
    3198. python操作elasticsearch
    3199. http://elasticsearch-py.readthedocs.io/en/master/
    3200. from datetime import datetime
    3201. from elasticsearch import Elasticsearch
    3202. es = Elasticsearch(["host1", "host2"])
    3203. doc = {
    3204. 'author': 'kimchy',
    3205. 'text': 'Elasticsearch: cool. bonsai cool.',
    3206. 'timestamp': datetime.now(),
    3207. }
    3208. res = es.index(index="live-", doc_type='tweet', id=1, body=doc)
    3209. print(res['created'])
    3210. res = es.get(index="live-", doc_type='tweet', id=1)
    3211. print(res['_source'])
    3212. es.indices.refresh(index="live-")
    3213. res = es.search(index="live-", body={"query": {"match_all": {}}})
    3214. 7 http客户端
    3215. urllib2 [网络资源访问]
    3216. import urllib2
    3217. response = urllib2.urlopen('http://baidu.com')
    3218. print response.geturl() # url
    3219. headers = response.info()
    3220. print headers # web页面头部信息
    3221. print headers['date'] # 头部信息中的时间
    3222. date = response.read() # 返回页面所有信息[字符串]
    3223. # date = response.readlines() # 返回页面所有信息[列表]
    3224. for i in urllib2.urlopen('http://qq.com'): # 可直接迭代
    3225. print i,
    3226. 下载文件
    3227. #!/usr/bin/env python
    3228. #encoding:utf8
    3229. import urllib2
    3230. url = 'http://www.01happy.com/wp-content/uploads/2012/09/bg.png'
    3231. file("./pic/%04d.png" % i, "wb").write(urllib2.urlopen(url).read())
    3232. 抓取网页解析指定内容
    3233. #!/usr/bin/env python
    3234. #encoding:utf8
    3235. import urllib2
    3236. import urllib
    3237. import random
    3238. from bs4 import BeautifulSoup
    3239. url='http://www.aaammm.com/aaa/'
    3240. ua=["Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)",
    3241. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",
    3242. "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C; .NET4.0E)",
    3243. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36",
    3244. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36",
    3245. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36",
    3246. "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36",
    3247. "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36",
    3248. "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36",
    3249. "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36",
    3250. "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36",
    3251. "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0",
    3252. "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36"]
    3253. browser = random.choice(ua)
    3254. req_header = {'User-Agent':browser,
    3255. 'Accept':'text/html;q=0.9,*/*;q=0.8',
    3256. 'Cookie':'BAIDUID=4C8274B52CFB79DEB4FBA9A7EC76A1BC:FG=1; BDUSS=1dCdU1WNFdxUll0R09XcnBZTkRrVVVNbWVnSkRKSVRPeVljOUswclBoLUNzVEpVQVFBQUFBJCQAAAAAAAAAAAEAAADEuZ8BcXVhbnpob3U3MjIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIIIkC1SCJAtUY; BD_UPN=123143; BD_HOME=1', # 添真实登陆后的Cookie 谷歌浏览器[F12 Network Documents Headers]
    3257. 'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
    3258. 'Connection':'close',
    3259. }
    3260. #data = urllib.urlencode({'name':'xuesong','id':'30' }) # urllib 的处理参数的方法,可以再urllib2中使用
    3261. data = urllib2.quote("pgv_ref=im.perinfo.perinfo.icon&rrr=pppp")
    3262. req_timeout = 10
    3263. try:
    3264. req = urllib2.Request(url,data=data,headers=req_header) # data为None 则方法为get,有date为post方法
    3265. html = urllib2.urlopen(req,data=None,req_timeout).read()
    3266. except urllib2.HTTPError as err:
    3267. print str(err)
    3268. except:
    3269. print "timeout"
    3270. print(html)
    3271. # 百度带Cookie后查看自己的用户
    3272. #for i in html.split('\n'):
    3273. # if 'bds.comm.user=' in i:
    3274. # print i
    3275. soup = BeautifulSoup(html)
    3276. for i in soup.find_all(target="_blank",attrs={"class": "usr-pic"}): # 条件看情况选择
    3277. if i.img:
    3278. print(i.get('href'))
    3279. 模拟浏览器访问web页面 python3
    3280. #! /usr/bin/env python
    3281. # -*- coding=utf-8 -*-
    3282. import urllib.request
    3283. url = "http://www.baidu.com"
    3284. # AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11
    3285. headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1)',
    3286. 'Accept':'text/html;q=0.9,*/*;q=0.8',
    3287. 'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
    3288. 'Connection':'close',
    3289. 'Referer':None #注意如果依然不能抓取的话,这里可以设置抓取网站的host
    3290. }
    3291. opener = urllib.request.build_opener()
    3292. opener.addheaders = [headers]
    3293. data = opener.open(url).read()
    3294. print(data)
    3295. requests [替代urllib2]
    3296. # Requests是一个Python的HTTP客户端库
    3297. # 官方中文文档 http://cn.python-requests.org/zh_CN/latest/user/quickstart.html#id2
    3298. # 安装: sudo pip install requests
    3299. import requests
    3300. # 使用 logging 库时忽略 requests 库的日志
    3301. logging.getLogger("requests").setLevel(logging.WARNING)
    3302. logging.getLogger("urllib3").setLevel(logging.WARNING)
    3303. # get方法提交表单
    3304. url = r'http://dict.youdao.com/search?le=eng&q={0}'.format(word.strip())
    3305. r = requests.get(url,timeout=2)
    3306. # get方法带参数 http://httpbin.org/get?key=val
    3307. payload = {'key1': 'value1', 'key2': 'value2'}
    3308. r = requests.get("http://httpbin.org/get", params=payload)
    3309. # post方法提交表单
    3310. QueryAdd='http://www.anti-spam.org.cn/Rbl/Query/Result'
    3311. r = requests.post(url=QueryAdd, data={'IP':'211.211.54.54'})
    3312. # 定制请求头post请求
    3313. payload = {'some': 'data'}
    3314. headers = {'content-type': 'application/json'}
    3315. r = requests.post(url, data=json.dumps(payload), headers=headers)
    3316. # https 需登录加auth
    3317. r = requests.get('https://baidu.com', auth=('user', 'pass'))
    3318. if r.ok: # 判断请求是否正常
    3319. print r.url # u'http://httpbin.org/get?key2=value2&key1=value1'
    3320. print r.status_code # 状态码
    3321. print r.content # 获取到的原始内容 可使用 BeautifulSoup4 解析处理判定结果
    3322. print r.text # 把原始内容转unicode编码
    3323. print r.headers # 响应头
    3324. print r.headers['content-type'] # 网页头信息 不存在为None
    3325. print r.cookies['example_cookie_name'] # 查看cookie
    3326. print r.history # 追踪重定向 [<Response [301]>] 开启重定向 allow_redirects=True
    3327. 获取JSON
    3328. r = requests.get('https://github.com/timeline.json')
    3329. r.json()
    3330. 获取图片
    3331. from PIL import Image
    3332. from StringIO import StringIO
    3333. i = Image.open(StringIO(r.content))
    3334. 发送cookies到服务器
    3335. url = 'http://httpbin.org/cookies'
    3336. cookies = dict(cookies_are='working')
    3337. r = requests.get(url, cookies=cookies)
    3338. r.text '{"cookies": {"cookies_are": "working"}}'
    3339. 在同一个Session实例发出的所有请求之间保持cookies
    3340. s = requests.Session()
    3341. s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
    3342. r = s.get("http://httpbin.org/cookies")
    3343. print r.text
    3344. 会话对象能够跨请求保持某些参数
    3345. s = requests.Session()
    3346. s.auth = ('user', 'pass')
    3347. s.headers.update({'x-test': 'true'})
    3348. s.get('http://httpbin.org/headers', headers={'x-test2': 'true'}) # both 'x-test' and 'x-test2' are sent
    3349. ssl证书验证
    3350. requests.get('https://github.com', verify=True)
    3351. requests.get('https://kennethreitz.com', verify=False) # 忽略证书验证
    3352. requests.get('https://kennethreitz.com', cert=('/path/server.crt', '/path/key')) # 本地指定一个证书 正确 <Response [200]> 错误 SSLError
    3353. 流式上传
    3354. with open('massive-body') as f:
    3355. requests.post('http://some.url/streamed', data=f)
    3356. 流式请求
    3357. import requests
    3358. import json
    3359. r = requests.post('https://stream.twitter.com/1/statuses/filter.json',
    3360. data={'track': 'requests'}, auth=('username', 'password'), stream=True)
    3361. for line in r.iter_lines():
    3362. if line: # filter out keep-alive new lines
    3363. print json.loads(line)
    3364. 自定义身份验证
    3365. from requests.auth import AuthBase
    3366. class PizzaAuth(AuthBase):
    3367. """Attaches HTTP Pizza Authentication to the given Request object."""
    3368. def __init__(self, username):
    3369. # setup any auth-related data here
    3370. self.username = username
    3371. def __call__(self, r):
    3372. # modify and return the request
    3373. r.headers['X-Pizza'] = self.username
    3374. return r
    3375. requests.get('http://pizzabin.org/admin', auth=PizzaAuth('kenneth'))
    3376. 基本身份认证
    3377. from requests.auth import HTTPBasicAuth
    3378. requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))
    3379. 摘要式身份认证
    3380. from requests.auth import HTTPDigestAuth
    3381. url = 'http://httpbin.org/digest-auth/auth/user/pass'
    3382. requests.get(url, auth=HTTPDigestAuth('user', 'pass'))
    3383. 代理
    3384. import requests
    3385. proxies = {
    3386. "http": "http://10.10.1.10:3128",
    3387. # "http": "http://user:pass@10.10.1.10:3128/", # 用户名密码
    3388. "https": "http://10.10.1.10:1080",
    3389. }
    3390. requests.get("http://example.org", proxies=proxies)
    3391. #也可以设置环境变量之间访问
    3392. export HTTP_PROXY="http://10.10.1.10:3128"
    3393. export HTTPS_PROXY="http://10.10.1.10:1080"
    3394. requests.session
    3395. import requests
    3396. import time
    3397. from bs4 import BeautifulSoup
    3398. session = requests.session()
    3399. login_url = "http://deploy.ixiaochuan.cn/login"
    3400. res_start = session.get(url=login_url)
    3401. bs = BeautifulSoup(res_start.text, "html.parser")
    3402. a = bs.select("#csrf_token")[0]
    3403. token = a.attrs.get("value")
    3404. login_data = {"username": (None, "weiqiang"), "password": (None, "Onei"), "submit": (None, "Login"),
    3405. "csrf_token": (None, token)}
    3406. res = session.post(url=login_url, files=login_data, allow_redirects=False)
    3407. print("login success")
    3408. BeautifulSoup [html\xml解析器]
    3409. # BeautifulSoup中文官方文档
    3410. # http://www.crummy.com/software/BeautifulSoup/bs3/documentation.zh.html
    3411. # http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html
    3412. # Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种: Tag , NavigableString , BeautifulSoup , Comment
    3413. 导入模块
    3414. from BeautifulSoup import BeautifulSoup # For processing HTML 版本3.0 已停止更新
    3415. from BeautifulSoup import BeautifulStoneSoup # For processing XML
    3416. import BeautifulSoup # To get everything
    3417. from bs4 import BeautifulSoup # 版本4.0 bs4 安装: pip install BeautifulSoup4
    3418. from bs4 import BeautifulSoup
    3419. soup = BeautifulSoup(html_doc) # 解析html文本 可以是 requests 提交返回的页面 results.content
    3420. print(soup.prettify()) # 输出解析后的结构
    3421. print(soup.title) # 指定标签内容
    3422. print(soup.title.name) # 标签名
    3423. print(soup.title.string) # 标签内容
    3424. print(soup.title.parent.name) # 上层标签名
    3425. print(soup.p) # <p class="title"><b>The Dormouse's story</b></p>
    3426. print(soup.p['class']) # u'title' class属性值
    3427. print(soup.a) # 找到第一个a标签的标签行
    3428. print(soup.find_all('a',limit=2)) # 找到a标签的行,最多为limit个
    3429. print(soup.find(id="link3")) # 标签内id为link3的标签行
    3430. print(soup.get_text()) # 从文档中获取所有文字内容
    3431. soup.find_all("a", text="Elsie") # 从文档中搜索关键字
    3432. soup.find(text=re.compile("sisters")) # 从文档中正则搜索关键字
    3433. soup.find_all("a", class_="sister") # 按CSS搜索
    3434. soup.find_all(id='link2',"table",attrs={"class": "status"},href=re.compile("elsie")) # 搜索方法
    3435. for i in soup.find_all('a',attrs={"class": "usr-pic"}): # 循环所有a标签的标签行
    3436. if i.a.img:
    3437. print(i.a.img.get("src")) # 取出当前a标签中的连接
    3438. Tag
    3439. # find_all 后循环的值是 Tag 不是字符串 不能直接截取
    3440. tag.text # 文本
    3441. tag.name
    3442. tag.name = "blockquote" # 查找name为 blockquote 的
    3443. tag['class']
    3444. tag.attrs # 按熟悉查找
    3445. tag['class'] = 'verybold'
    3446. del tag['class'] # 删除
    3447. print(tag.get('class')) # 打印属性值
    3448. print(i.get('href')) # 打印连接
    3449. cookielib [保留cookie登录页面]
    3450. ck = cookielib.CookieJar() # 通过 这个就可以实现请求带过去的COOKIE与发送回来的COOKIE值了。
    3451. opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(ck)) # 获取到COOKIE
    3452. urllib2.install_opener(opener) # 此句设置urllib2的全局opener
    3453. content = urllib2.urlopen(url).read()
    3454. 登录cacti取图片
    3455. #encoding:utf8
    3456. import urllib2
    3457. import urllib
    3458. import cookielib
    3459. def renrenBrower(url,user,password):
    3460. #查找form标签中的action提交地址
    3461. login_page = "http://10.10.10.19/cacti/index.php"
    3462. try:
    3463. #获得一个cookieJar实例
    3464. cj = cookielib.CookieJar()
    3465. #cookieJar作为参数,获得一个opener的实例
    3466. opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    3467. #伪装成一个正常的浏览器,避免有些web服务器拒绝访问
    3468. opener.addheaders = [('User-agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')]
    3469. #生成Post数据,含有登陆用户名密码,所有表单内的input中name值
    3470. data = urllib.urlencode({"action":"login","login_username":user,"login_password":password})
    3471. #以post的方法访问登陆页面,访问之后cookieJar会自定保存cookie
    3472. opener.open(login_page,data)
    3473. #以带cookie的方式访问页面
    3474. op=opener.open(url)
    3475. #读取页面源码
    3476. data=op.read()
    3477. #将图片写到本地
    3478. #file("1d.png" , "wb").write(data)
    3479. return data
    3480. except Exception,e:
    3481. print str(e)
    3482. print renrenBrower("http://10.10.10.19/cacti/graph_image.php?local_graph_id=1630&rra_id=0&view_type=tree&graph_start=1397525517&graph_end=1397611917","admin","admin")
    3483. 例子2
    3484. import urllib, urllib2, cookielib
    3485. import os, time
    3486. headers = []
    3487. def login():
    3488. cj = cookielib.CookieJar()
    3489. opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    3490. login_url = r'http://zhixing.bjtu.edu.cn/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1'
    3491. login_data = urllib.urlencode({'cookietime': '2592000', 'handlekey': 'ls', 'password': 'xxx',
    3492. 'quickforward': 'yes', 'username': 'GuoYuan'})
    3493. opener.addheaders = [('Host', 'zhixing.bjtu.edu.cn'),
    3494. ('User-Agent', 'Mozilla/5.0 (Ubuntu; X11; Linux i686; rv:8.0) Gecko/20100101 Firefox/8.0'),
    3495. ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
    3496. ('Accept-Language', 'en-us,en;q=0.5'),
    3497. ('Accept-Encoding', 'gzip, deflate'),
    3498. ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'),
    3499. ('Connection', 'keep-alive'),
    3500. ('Referer', 'http://zhixing.bjtu.edu.cn/forum.php'),]
    3501. opener.open(login_url, login_data)
    3502. return opener
    3503. if __name__ == '__main__':
    3504. opener = login()
    3505. url = r'http://zhixing.bjtu.edu.cn/forum.php?mod=topicadmin&action=moderate&optgroup=2&modsubmit=yes&infloat=yes&inajax=1'
    3506. data = {'fid': '601', 'formhash': '0cdd1596', 'frommodcp': '', 'handlekey': 'mods',
    3507. 'listextra': 'page%3D62', 'moderate[]': '496146', 'operations[]': 'type', 'reason': '...',
    3508. 'redirect': r'http://zhixing.bjtu.edu.cn/thread-496146-1-1.html', 'typeid': '779'}
    3509. data2 = [(k, v) for k,v in data.iteritems()]
    3510. cnt = 0
    3511. for tid in range(493022, 496146 + 1):
    3512. cnt += 1
    3513. if cnt % 20 == 0: print
    3514. print tid,
    3515. data2.append(('moderate[]', str(tid)))
    3516. if cnt % 40 == 0 or cnt == 496146:
    3517. request = urllib2.Request(url=url, data=urllib.urlencode(data2))
    3518. print opener.open(request).read()
    3519. data2 = [(k, v) for k,v in data.iteritems()]
    3520. httplib [http协议的客户端]
    3521. import httplib
    3522. conn3 = httplib.HTTPConnection('www.baidu.com',80,True,10)
    3523. aiohttp [检索网页的客户端]
    3524. # 需要python3.3+
    3525. # http://aiohttp.readthedocs.org/en/v0.12.0/
    3526. import aiohttp
    3527. def get_body(url):
    3528. response = yield from aiohttp.request('GET', url)
    3529. return (yield from response.read())
    3530. response = yield from aiohttp.request('GET', 'http://python.org')
    3531. body = yield from response.read()
    3532. print(body)
    3533. # 用 asyncio 配合协程抓取页面
    3534. yield from asyncio.wait_for(request('GET', url), 10)
    3535. http_server
    3536. import asyncio
    3537. from aiohttp import web
    3538. @asyncio.coroutine
    3539. def handle(request):
    3540. name = request.match_info.get('name', "Anonymous")
    3541. text = "Hello, " + name
    3542. return web.Response(body=text.encode('utf-8'))
    3543. @asyncio.coroutine
    3544. def init(loop):
    3545. app = web.Application(loop=loop)
    3546. app.router.add_route('GET', '/{name}', handle)
    3547. srv = yield from loop.create_server(app.make_handler(),
    3548. '127.0.0.1', 8080)
    3549. print("Server started at http://127.0.0.1:8080")
    3550. return srv
    3551. loop = asyncio.get_event_loop()
    3552. loop.run_until_complete(init(loop))
    3553. loop.run_forever()
    3554. 查看网页图片尺寸类型
    3555. #将图片读入内存
    3556. #!/usr/bin/env python
    3557. #encoding=utf-8
    3558. import cStringIO, urllib2, Image
    3559. url = 'http://www.01happy.com/wp-content/uploads/2012/09/bg.png'
    3560. file = urllib2.urlopen(url)
    3561. tmpIm = cStringIO.StringIO(file.read())
    3562. im = Image.open(tmpIm)
    3563. print im.format, im.size, im.mode
    3564. 爬虫
    3565. #!/usr/bin/env python
    3566. #encoding:utf-8
    3567. #sudo pip install BeautifulSoup
    3568. import requests
    3569. from BeautifulSoup import BeautifulSoup
    3570. import re
    3571. baseurl = 'http://blog.sina.com.cn/s/articlelist_1191258123_0_1.html'
    3572. r = requests.get(baseurl)
    3573. for url in re.findall('<a.*?</a>', r.content, re.S):
    3574. if url.startswith('<a title='):
    3575. with open(r'd:/final.txt', 'ab') as f:
    3576. f.write(url + '\n')
    3577. linkfile = open(r'd:/final.txt', 'rb')
    3578. soup = BeautifulSoup(linkfile)
    3579. for link in soup.findAll('a'):
    3580. #print link.get('title') + ': ' + link.get('href')
    3581. ss = requests.get(link.get('href'))
    3582. for content in re.findall('<div id="sina_keyword_ad_area2" class="articalContent ">.*?</div>', ss.content, re.S):
    3583. with open(r'd:/myftp/%s.txt'%link.get('title').strip('<>'), 'wb') as f:
    3584. f.write(content)
    3585. print '%s has been copied.' % link.get('title')
    3586. 反垃圾邮件提交申诉
    3587. #很遗憾,反垃圾邮件联盟改版后加了验证码
    3588. #!/usr/bin/env python
    3589. #encoding:utf-8
    3590. import requests
    3591. import re
    3592. IpList=['113.212.91.25','113.212.91.23']
    3593. QueryAdd='http://www.anti-spam.org.cn/Rbl/Query/Result'
    3594. ComplaintAdd='http://www.anti-spam.org.cn/Rbl/Getout/Submit'
    3595. data = {
    3596. 'CONTENT':'''我们是一家正规的XXX。xxxxxxx。恳请将我们的发送服务器IP移出黑名单。谢谢!
    3597. 处理措施:
    3598. 1.XXXX。
    3599. 2.XXXX。''',
    3600. 'CORP':'abc.com',
    3601. 'WWW':'www.abc.cm',
    3602. 'NAME':'def',
    3603. 'MAIL':'def@163.com.cn',
    3604. 'TEL':'010-50000000',
    3605. 'LEVEL':'0',
    3606. }
    3607. for Ip in IpList:
    3608. query = requests.post(url=QueryAdd, data={'IP':Ip}) # 黑名单查询
    3609. if query.ok:
    3610. if re.findall(u'\u7533\u8bc9\u8131\u79bb', query.text, re.S): # 查找关键字 申诉脱离 既表明在黑名单中
    3611. data['IP']=Ip
    3612. complaint = requests.post(url=ComplaintAdd, data=data) # 提交申诉
    3613. if complaint.ok:
    3614. if re.findall(u'\u60a8\u7684\u9ed1\u540d\u5355\u8131\u79bb\u7533\u8bf7\u5df2\u63d0\u4ea4', complaint.text, re.S):
    3615. status='申请提交'
    3616. elif re.findall(u'\u8131\u79bb\u7533\u8bf7\u5df2\u88ab\u4ed6\u4eba\u63d0\u4ea4', complaint.text, re.S):
    3617. status='重复提交'
    3618. elif re.findall(u'\u7533\u8bf7\u7531\u4e8e\u8fd1\u671f\u5185\u6709\u88ab\u62d2\u7edd\u7684\u8bb0\u5f55', complaint.text, re.S):
    3619. status='近期拒绝'
    3620. else:
    3621. status='异常'
    3622. else:
    3623. status='正常'
    3624. print '%s %s' %(Ip,status)
    3625. 有道词典
    3626. #!/usr/bin/env python
    3627. import requests
    3628. from bs4 import BeautifulSoup
    3629. # bs4安装: pip install BeautifulSoup4
    3630. def youdao(word):
    3631. url = r'http://dict.youdao.com/search?le=eng&q={0}'.format(word.strip())
    3632. r = requests.get(url)
    3633. if r.ok:
    3634. soup = BeautifulSoup(r.content)
    3635. div = soup.find_all('div', class_='trans-container')[:1] # find_all是bs4的方法
    3636. ul = BeautifulSoup(str(div[0]))
    3637. li = ul.find_all('li')
    3638. for mean in li:
    3639. print mean.text
    3640. def query():
    3641. print('Created by @littlepy, QQ:185635687')
    3642. while True:
    3643. word = raw_input('>>>')
    3644. youdao(word)
    3645. if __name__ == '__main__':
    3646. query()
    3647. python启动http服务提供访问或下载
    3648. python -m SimpleHTTPServer 9900
    3649. 8 并发
    3650. #线程安全/竞争条件,锁/死锁检测,线程池,生产消费模型,伪并发,微线程,协程
    3651. #Stackless Python 是Python编程语言的一个增强版本,它使程序员从基于线程的编程方式中获得好处,并避免传统线程所带来的性能与复杂度问题。Stackless为 Python带来的微线程扩展,是一种低开销、轻量级的便利工具
    3652. Queue队列
    3653. import Queue
    3654. q = = Queue.Queue(3)
    3655. q.put('a', True, 5) # True等待超时时间, False不等待
    3656. if q.full(): # 队列满了返回True,反之False
    3657. q.qsize() # 队列长度
    3658. workQueue.queue.clear() # 清空队列
    3659. q.get(True,5) # True等待超时时间, False不等待
    3660. threading多线程
    3661. thread
    3662. start_new_thread(function,args kwargs=None) # 产生一个新的线程
    3663. allocate_lock() # 分配一个LockType类型的锁对象
    3664. exit() # 让线程退出
    3665. acquire(wait=None) # 尝试获取锁对象
    3666. locked() # 如果获取了锁对象返回True
    3667. release() # 释放锁
    3668. thread例子
    3669. #!/usr/bin/env python
    3670. #thread_test.py
    3671. #不支持守护进程
    3672. import thread
    3673. from time import sleep,ctime
    3674. loops = [4,2]
    3675. def loop(nloop,nsec,lock):
    3676. print 'start loop %s at:%s' % (nloop,ctime())
    3677. sleep(nsec)
    3678. print 'loop %s done at: %s' % (nloop, ctime())
    3679. lock.release() # 分配已获得的锁,操作结束后释放相应的锁通知主线程
    3680. def main():
    3681. print 'starting at:',ctime()
    3682. locks = []
    3683. nloops = range(len(loops))
    3684. for i in nloops:
    3685. lock = thread.allocate_lock() # 创建一个锁
    3686. lock.acquire() # 调用各个锁的acquire()函数获得锁
    3687. locks.append(lock) # 把锁放到锁列表locks中
    3688. for i in nloops:
    3689. thread.start_new_thread(loop,(i,loops[i],locks[i])) # 创建线程
    3690. for i in nloops:
    3691. while locks[i].locked():pass # 等待全部解锁才继续运行
    3692. print 'all DONE at:',ctime()
    3693. if __name__ == '__main__':
    3694. main()
    3695. thread例子1
    3696. #coding=utf-8
    3697. import thread,time,os
    3698. def f(name):
    3699. i =3
    3700. while i:
    3701. time.sleep(1)
    3702. print name
    3703. i -= 1
    3704. # os._exit() 会把整个进程关闭
    3705. os._exit(22)
    3706. if __name__ == '__main__':
    3707. thread.start_new_thread(f,("th1",))
    3708. while 1:
    3709. pass
    3710. os._exit(0)
    3711. threading
    3712. Thread # 表示一个线程的执行的对象
    3713. start() # 开始线程的执行
    3714. run() # 定义线程的功能的函数(一般会被子类重写)
    3715. join(timeout=None) # 允许主线程等待线程结束,程序挂起,直到线程结束;如果给了timeout,则最多等待timeout秒.
    3716. getName() # 返回线程的名字
    3717. setName(name) # 设置线程的名字
    3718. isAlive() # 布尔标志,表示这个线程是否还在运行中
    3719. isDaemon() # 返回线程的daemon标志
    3720. setDaemon(daemonic) # 后台线程,把线程的daemon标志设置为daemonic(一定要在调用start()函数前调用)
    3721. # 默认主线程在退出时会等待所有子线程的结束。如果希望主线程不等待子线程,而是在退出时自动结束所有的子线程,就需要设置子线程为后台线程(daemon)
    3722. Lock # 锁原语对象
    3723. Rlock # 可重入锁对象.使单线程可以在此获得已获得了的锁(递归锁定)
    3724. Condition # 条件变量对象能让一个线程停下来,等待其他线程满足了某个条件.如状态改变或值的改变
    3725. Event # 通用的条件变量.多个线程可以等待某个事件的发生,在事件发生后,所有的线程都会被激活
    3726. Semaphore # 为等待锁的线程提供一个类似等候室的结构
    3727. BoundedSemaphore # 与Semaphore类似,只是不允许超过初始值
    3728. Time # 与Thread相似,只是他要等待一段时间后才开始运行
    3729. activeCount() # 当前活动的线程对象的数量
    3730. currentThread() # 返回当前线程对象
    3731. enumerate() # 返回当前活动线程的列表
    3732. settrace(func) # 为所有线程设置一个跟踪函数
    3733. setprofile(func) # 为所有线程设置一个profile函数
    3734. threading例子1
    3735. #!/usr/bin/env python
    3736. #encoding:utf8
    3737. import threading
    3738. from Queue import Queue
    3739. from time import sleep,ctime
    3740. class ThreadFunc(object):
    3741. def __init__(self,func,args,name=''):
    3742. self.name=name
    3743. self.func=func # loop
    3744. self.args=args # (i,iplist[i],queue)
    3745. def __call__(self):
    3746. apply(self.func,self.args) # 函数apply() 执行loop函数并传递元组参数
    3747. def loop(nloop,ip,queue):
    3748. print 'start',nloop,'at:',ctime()
    3749. queue.put(ip)
    3750. sleep(2)
    3751. print 'loop',nloop,'done at:',ctime()
    3752. if __name__ == '__main__':
    3753. threads = []
    3754. queue = Queue()
    3755. iplist = ['192.168.1.2','192.168.1.3','192.168.1.4','192.168.1.5','192.168.1.6','192.168.1.7','192.168.1.8']
    3756. nloops = range(len(iplist))
    3757. for i in nloops:
    3758. t = threading.Thread(target=ThreadFunc(loop,(i,iplist[i],queue),loop.__name__))
    3759. threads.append(t)
    3760. for i in nloops:
    3761. threads[i].start()
    3762. for i in nloops:
    3763. threads[i].join()
    3764. for i in nloops:
    3765. print queue.get()
    3766. threading例子2
    3767. #!/usr/bin/env python
    3768. #encoding:utf8
    3769. from Queue import Queue
    3770. import random,time,threading
    3771. class Producer(threading.Thread):
    3772. def __init__(self, t_name, queue):
    3773. threading.Thread.__init__(self, name=t_name)
    3774. self.data=queue
    3775. def run(self):
    3776. for i in range(5):
    3777. print "%s: %s is producing %d to the queue!\n" %(time.ctime(), self.getName(), i)
    3778. self.data.put(i)
    3779. self.data.put(i*i)
    3780. time.sleep(2)
    3781. print "%s: %s finished!" %(time.ctime(), self.getName())
    3782. class Consumer(threading.Thread):
    3783. def __init__(self, t_name, queue):
    3784. threading.Thread.__init__(self, name=t_name)
    3785. self.data=queue
    3786. def run(self):
    3787. for i in range(10):
    3788. val = self.data.get()
    3789. print "%s: %s is consuming. %d in the queue is consumed!\n" %(time.ctime(), self.getName(), val)
    3790. print "%s: %s finished!" %(time.ctime(), self.getName())
    3791. if __name__ == '__main__':
    3792. queue = Queue()
    3793. producer = Producer('Pro.', queue)
    3794. consumer = Consumer('Con.', queue)
    3795. producer.start()
    3796. consumer.start()
    3797. producer.join()
    3798. consumer.join()
    3799. threading例子3
    3800. # 启动线程后自动执行 run函数其他不可以
    3801. import threading
    3802. import time
    3803. class Th(threading.Thread):
    3804. def __init__(self,name):
    3805. threading.Thread.__init__(self)
    3806. self.t_name=name
    3807. self.daemon = True # 默认为false,让主线程等待处理完成
    3808. def run(self):
    3809. time.sleep(1)
    3810. print "this is " + self.t_name
    3811. if __name__ == '__main__':
    3812. thread1 = Th("Th_1")
    3813. thread1.start()
    3814. threading例子4
    3815. import threading
    3816. import time
    3817. class Th(threading.Thread):
    3818. def __init__(self,thread_name):
    3819. threading.Thread.__init__(self)
    3820. self.setName(thread_name)
    3821. def run(self):
    3822. threadLock.acquire()
    3823. print self.getName()
    3824. for i in range(3):
    3825. time.sleep(1)
    3826. print str(i)
    3827. print self.getName() + " is over"
    3828. threadLock.release()
    3829. if __name__ == '__main__':
    3830. threadLock = threading.Lock()
    3831. thread1 = Th("Th_1")
    3832. thread2 = Th("Th_2")
    3833. thread1.start()
    3834. thread2.start()
    3835. 后台线程
    3836. import threading
    3837. import time,random
    3838. class MyThread(threading.Thread):
    3839. def run(self):
    3840. wait_time=random.randrange(1,10)
    3841. print "%s will wait %d seconds" % (self.name, wait_time)
    3842. time.sleep(wait_time)
    3843. print "%s finished!" % self.name
    3844. if __name__=="__main__":
    3845. for i in range(5):
    3846. t = MyThread()
    3847. t.setDaemon(True) # 设置为后台线程,主线程完成时不等待子线程完成就结束
    3848. t.start()
    3849. threading控制最大并发_查询日志中IP信息
    3850. #!/usr/bin/env python
    3851. #coding:utf-8
    3852. import urllib2
    3853. import json
    3854. import threading
    3855. import time
    3856. '''
    3857. by:某大牛
    3858. QQ:185635687
    3859. 这个是多线程并发控制. 如果要改成多进程,只需把threading 换成 mulitprocessing.Process , 对, 就是换个名字而已.
    3860. '''
    3861. #获取ip 及其出现次数
    3862. def ip_dic(file_obj, dic):
    3863. for i in file_obj:
    3864. if i:
    3865. ip=i.split('-')[0].strip()
    3866. if ip in dic.keys():
    3867. dic[ip]=dic[ip] + 1
    3868. else:
    3869. dic[ip]=1
    3870. return dic.iteritems()
    3871. #目标函数
    3872. def get_data(url, ipcounts):
    3873. data=urllib2.urlopen(url).read()
    3874. datadict=json.loads(data)
    3875. fdata = u"ip:%s---%s,%s,%s,%s,%s" %(datadict["data"]["ip"],ipcounts,datadict["data"]["country"],datadict["data"]["region"],datadict["data"]["city"],datadict["data"]["isp"])
    3876. print fdata
    3877. #多线程
    3878. def threads(iters):
    3879. thread_pool = []
    3880. for k in iters:
    3881. url = "http://ip.taobao.com/service/getIpInfo.php?ip="
    3882. ipcounts = k[1]
    3883. url = (url + k[0]).strip()
    3884. t = threading.Thread(target=get_data, args=(url, ipcounts))
    3885. thread_pool.append(t)
    3886. return thread_pool
    3887. #控制多线程
    3888. def startt(t_list, max,second):
    3889. l = len(t_list)
    3890. n = max
    3891. while l > 0:
    3892. if l > max:
    3893. nl = t_list[:max]
    3894. t_list = t_list[max:]
    3895. for t in nl:
    3896. t.start()
    3897. time.sleep(second)
    3898. for t in nl:
    3899. t.join()
    3900. print '*'*15, str(n)+ ' ip has been queried'+'*'*15
    3901. n += max
    3902. l = len(t_list)
    3903. continue
    3904. elif l <= max:
    3905. nl = t_list
    3906. for t in nl:
    3907. t.start()
    3908. for t in nl:
    3909. t.join()
    3910. print '>>> Totally ' + str(n+l ) + ' ip has been queried'
    3911. l = 0
    3912. if __name__ =="__main__":
    3913. dic={}
    3914. with open('access.log') as file_obj:
    3915. it = ip_dic(file_obj, dic)
    3916. t_list= threads(it)
    3917. startt(t_list, 15, 1)
    3918. 多线程取队列
    3919. #!/usr/bin/python
    3920. import Queue
    3921. import threading
    3922. import time
    3923. exitFlag = 0
    3924. class myThread(threading.Thread):
    3925. def __init__(self, threadID, name, q):
    3926. threading.Thread.__init__(self)
    3927. self.threadID = threadID
    3928. self.name = name
    3929. self.q = q
    3930. def run(self):
    3931. print "Starting " + self.name
    3932. process_data(self.name, self.q)
    3933. print "Exiting " + self.name
    3934. def process_data(threadName, q):
    3935. while not exitFlag: # 死循环等待
    3936. queueLock.acquire()
    3937. if not q.empty(): # 判断队列是否为空
    3938. data = q.get()
    3939. print "%s processing %s" % (threadName, data)
    3940. queueLock.release()
    3941. time.sleep(1)
    3942. nameList = ["One", "Two", "Three", "Four", "Five"]
    3943. queueLock = threading.Lock() # 锁与队列并无任何关联,其他线程也进行取锁操作的时候就会检查是否有被占用,有就阻塞等待解锁为止
    3944. workQueue = Queue.Queue(10)
    3945. threads = []
    3946. threadID = 1
    3947. # Create new threads
    3948. for threadID in range(100):
    3949. thread = myThread(threadID, 'tName%s' % threadID, workQueue)
    3950. thread.start()
    3951. threads.append(thread)
    3952. threadID += 1
    3953. # Fill the queue
    3954. queueLock.acquire()
    3955. for word in nameList:
    3956. workQueue.put(word)
    3957. queueLock.release()
    3958. # Wait for queue to empty
    3959. while not workQueue.empty(): # 死循环判断队列被处理完毕
    3960. pass
    3961. # Notify threads it's time to exit
    3962. exitFlag = 1
    3963. # Wait for all threads to complete
    3964. for t in threads:
    3965. t.join()
    3966. print "Exiting Main Thread"
    3967. Queue通用队列
    3968. q=Queue(size) # 创建大小size的Queue对象
    3969. qsize() # 返回队列的大小(返回时候,可能被其他进程修改,近似值)
    3970. empty() # 如果队列为空返回True,否则False
    3971. full() # 如果队列已满返回True,否则False
    3972. put(item,block0) # 把item放到队列中,如果给了block(不为0),函数会一直阻塞到队列中有空间为止
    3973. get(block=0) # 从队列中取一个对象,如果给了block(不为0),函数会一直阻塞到队列中有对象为止
    3974. get_nowait # 默认get阻塞,这个不阻塞
    3975. multiprocessing [多进程并发]
    3976. 线程池
    3977. import urllib2
    3978. from multiprocessing.dummy import Pool as ThreadPool
    3979. urls=['http://www.baidu.com','http://www.sohu.com']
    3980. pool=ThreadPool(4) # 线程池
    3981. results=pool.map(urllib2.urlopen,urls)
    3982. pool.close()
    3983. pool.join()
    3984. 进程并发
    3985. #!/usr/bin/env python
    3986. #encoding:utf8
    3987. from multiprocessing import Process
    3988. import time,os
    3989. def f(name):
    3990. time.sleep(1)
    3991. print 'hello ',name
    3992. print os.getppid() # 取得父进程ID
    3993. print os.getpid() # 取得进程ID
    3994. process_list = []
    3995. for i in range(10):
    3996. p = Process(target=f,args=(i,))
    3997. p.start()
    3998. process_list.append(p)
    3999. for j in process_list:
    4000. j.join()
    4001. 进程池
    4002. #!/usr/bin/env python
    4003. #encoding:utf8
    4004. from multiprocessing import Pool
    4005. import time,os
    4006. def f(name):
    4007. time.sleep(1)
    4008. print 'hello ',name
    4009. print os.getppid()
    4010. print os.getpid()
    4011. process_list = []
    4012. pool = Pool(4)
    4013. res = pool.map(f, range(1,10))
    4014. pool.close()
    4015. pool.join()
    4016. Queue进程间通信
    4017. from multiprocessing import Process,Queue
    4018. import time
    4019. def f(name):
    4020. time.sleep(1)
    4021. q.put(['hello'+str(name)])
    4022. process_list = []
    4023. q = Queue()
    4024. if __name__ == '__main__':
    4025. for i in range(10):
    4026. p = Process(target=f,args=(i,))
    4027. p.start()
    4028. process_list.append(p)
    4029. for j in process_list:
    4030. j.join()
    4031. for i in range(10):
    4032. print q.get()
    4033. Pipe管道 # 单项通信
    4034. from multiprocessing import Process,Pipe
    4035. import time
    4036. import os
    4037. def f(conn,name):
    4038. time.sleep(1)
    4039. conn.send(['hello'+str(name)])
    4040. print os.getppid(),'-----------',os.getpid()
    4041. process_list = []
    4042. parent_conn,child_conn = Pipe()
    4043. if __name__ == '__main__':
    4044. for i in range(10):
    4045. p = Process(target=f,args=(child_conn,i))
    4046. p.start()
    4047. process_list.append(p)
    4048. for j in process_list:
    4049. j.join()
    4050. for p in range(10):
    4051. print parent_conn.recv()
    4052. 进程间同步
    4053. #加锁,使某一时刻只有一个进程,其他在调用同一个锁就会被阻塞
    4054. from multiprocessing import Process,Lock
    4055. import time
    4056. import os
    4057. def f(name):
    4058. lock.acquire()
    4059. time.sleep(1)
    4060. print 'hello--'+str(name)
    4061. print os.getppid(),'-----------',os.getpid()
    4062. lock.release()
    4063. process_list = []
    4064. lock = Lock()
    4065. if __name__ == '__main__':
    4066. for i in range(10):
    4067. p = Process(target=f,args=(i,))
    4068. p.start()
    4069. process_list.append(p)
    4070. for j in process_list:
    4071. j.join()
    4072. 共享内存 # 双向通信
    4073. # 通过使用Value或者Array把数据存储在一个共享的内存表中
    4074. # 'd'和'i'参数是num和arr用来设置类型,d表示一个双精浮点类型,i表示一个带符号的整型。
    4075. from multiprocessing import Process,Value,Array
    4076. import time
    4077. import os
    4078. def f(n,a,name):
    4079. time.sleep(1)
    4080. n.value = name * name
    4081. for i in range(len(a)):
    4082. a[i] = -i
    4083. process_list = []
    4084. if __name__ == '__main__':
    4085. num = Value('d',0.0)
    4086. arr = Array('i',range(10))
    4087. for i in range(10):
    4088. p = Process(target=f,args=(num,arr,i))
    4089. p.start()
    4090. process_list.append(p)
    4091. for j in process_list:
    4092. j.join()
    4093. print num.value
    4094. print arr[:]
    4095. manager
    4096. # 比共享内存灵活,但缓慢
    4097. # 支持list,dict,Namespace,Lock,Semaphore,BoundedSemaphore,Condition,Event,Queue,Value,Array
    4098. from multiprocessing import Process,Manager
    4099. import time
    4100. import os
    4101. def f(d,name):
    4102. time.sleep(1)
    4103. d[name] = name * name
    4104. print d
    4105. process_list = []
    4106. if __name__ == '__main__':
    4107. manager = Manager()
    4108. d = manager.dict()
    4109. for i in range(10):
    4110. p = Process(target=f,args=(d,i))
    4111. p.start()
    4112. process_list.append(p)
    4113. for j in process_list:
    4114. j.join()
    4115. print d
    4116. 最大并发数
    4117. import multiprocessing
    4118. import time,os
    4119. result = []
    4120. def run(h):
    4121. print 'threading:' ,h,os.getpid()
    4122. p = multiprocessing.Pool(processes=20)
    4123. for i in range(100):
    4124. result.append(p.apply_async(run,(i,)))
    4125. p.close()
    4126. for res in result:
    4127. res.get(timeout=5)
    4128. gevent [轻量级协程]
    4129. # 在gevent中用到的主要模式是Greenlet, 它是以C扩展模块形式接入Python的轻量级协程。 Greenlet全部运行在主程序操作系统进程的内部,但它们被协作式地调度。
    4130. # http://xlambda.com/gevent-tutorial/
    4131. 锁的使用
    4132. # 同时允许多个协程操作对象的锁,通过互斥访问,保证资源只在程序上下文被单次使用
    4133. from gevent import sleep
    4134. from gevent.pool import Pool
    4135. from gevent.coros import BoundedSemaphore
    4136. sem = BoundedSemaphore(2) # 超过2就会阻塞等待
    4137. def worker1(n):
    4138. sem.acquire()
    4139. print('Worker %i acquired semaphore' % n)
    4140. sleep(0)
    4141. sem.release()
    4142. print('Worker %i released semaphore' % n)
    4143. def worker2(n):
    4144. with sem:
    4145. print('Worker %i acquired semaphore' % n)
    4146. sleep(0)
    4147. print('Worker %i released semaphore' % n)
    4148. pool = Pool()
    4149. pool.map(worker1, xrange(0,2))
    4150. pool.map(worker2, xrange(3,6))
    4151. 事件
    4152. # Event 阻塞事件
    4153. import gevent
    4154. from gevent.event import Event
    4155. evt = Event()
    4156. def setter():
    4157. '''After 3 seconds, wake all threads waiting on the value of evt'''
    4158. print('A: Hey wait for me, I have to do something')
    4159. gevent.sleep(3)
    4160. print("Ok, I'm done")
    4161. evt.set() # 表示事件完成
    4162. def waiter():
    4163. '''After 3 seconds the get call will unblock'''
    4164. print("I'll wait for you")
    4165. evt.wait() # 阻塞等待事件完成
    4166. print("It's about time")
    4167. gevent.joinall([
    4168. gevent.spawn(setter),
    4169. gevent.spawn(waiter),
    4170. gevent.spawn(waiter),
    4171. gevent.spawn(waiter),
    4172. gevent.spawn(waiter),
    4173. gevent.spawn(waiter)
    4174. ])
    4175. # AsyncResult 可传值的事件
    4176. import gevent
    4177. from gevent.event import AsyncResult
    4178. a = AsyncResult()
    4179. def setter():
    4180. gevent.sleep(3)
    4181. a.set('Hello!') # 事件传值
    4182. def waiter():
    4183. """
    4184. After 3 seconds the get call will unblock after the setter
    4185. puts a value into the AsyncResult.
    4186. """
    4187. print(a.get()) # 获取时间值
    4188. gevent.joinall([
    4189. gevent.spawn(setter),
    4190. gevent.spawn(waiter),
    4191. ])
    4192. 队列
    4193. #/usr/local/python
    4194. #encoding:utf8
    4195. import gevent
    4196. from gevent.pool import Pool
    4197. from gevent.coros import BoundedSemaphore
    4198. from gevent.queue import Queue, Empty
    4199. import os
    4200. tasks = Queue(maxsize=30) # 队列 超过30引发 gevent.hub.LoopExit
    4201. tasks1 = Queue()
    4202. def boss():
    4203. print '放队列任务'
    4204. for i in xrange(1,25):
    4205. tasks.put(i)
    4206. def worker1(n):
    4207. print len(pool)
    4208. while not tasks.empty(): # 判断队列是否为空
    4209. task = tasks.get() # 获取队列内容
    4210. tasks1.put(os.popen('id').read())
    4211. print('Worker %s got task %s' % (n, task))
    4212. gevent.sleep(0) # 放弃当前任务
    4213. def worker2(name):
    4214. try:
    4215. while True:
    4216. task = tasks1.get(timeout=2)
    4217. print '获取后释放:%s' % task
    4218. gevent.sleep(0)
    4219. except Empty: # 等待超时报错完成
    4220. print('Quitting time!')
    4221. gevent.spawn(boss).join() # 执行单次协程任务
    4222. pool = Pool(5) # 协程池大小
    4223. pool.map(worker1, xrange(0,20)) # 通过map方法把多个任务分发给池中的5个协程
    4224. gevent.joinall([ # 同时执行多个协程任务
    4225. gevent.spawn(worker2, 'steve'),
    4226. gevent.spawn(worker2, 'john'),
    4227. gevent.spawn(worker2, 'nancy'),
    4228. ])
    4229. 9 框架
    4230. flask [微型网络开发框架]
    4231. # http://dormousehole.readthedocs.org/en/latest/
    4232. # http://www.pythonhosted.org/Flask-Bootstrap/basic-usage.html#templates
    4233. # html放在 ./templates/ js放在 ./static/
    4234. #pip install Flask-Login
    4235. #pip install Flask-OpenID
    4236. #pip install Flask-WTF
    4237. #pip install flask-bootstrap
    4238. #pip install flask-sqlalchemy
    4239. #pip install flask-script
    4240. #pip install flask-migrate
    4241. request.args.get('page', 1) # 获取参数 ?page=1
    4242. request.json # 获取传递的整个json数据
    4243. request.form.get("host",'127') # 获取表单值
    4244. request.form.getlist('client') # 获取表单列表
    4245. 简单实例 # 接收数据和展示
    4246. import MySQLdb as mysql
    4247. from flask import Flask, request
    4248. app = Flask(__name__)
    4249. db.autocommit(True)
    4250. c = db.cursor()
    4251. """
    4252. CREATE TABLE `statusinfo` (
    4253. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
    4254. `hostname` varchar(32) NOT NULL,
    4255. `load` float(10) NOT NULL DEFAULT 0.00,
    4256. `time` int(15) NOT NULL,
    4257. `memtotal` int(15) NOT NULL,
    4258. `memusage` int(15) NOT NULL,
    4259. `memfree` int(15) NOT NULL,
    4260. PRIMARY KEY (`id`)
    4261. ) ENGINE=InnoDB AUTO_INCREMENT=161 DEFAULT CHARSET=utf8;
    4262. """
    4263. @app.route("/collect", methods=["GET", "POST"])
    4264. def collect():
    4265. sql = ""
    4266. if request.method == "POST":
    4267. data = request.json # 获取传递的json
    4268. hostname = data["Host"]
    4269. load = data["LoadAvg"]
    4270. time = data["Time"]
    4271. memtotal = data["MemTotal"]
    4272. memusage = data["MemUsage"]
    4273. memfree = data["MemFree"]
    4274. try:
    4275. sql = "INSERT INTO `statusinfo` (`hostname`,`load`,`time`,`memtotal`,`memusage`,`memfree`) VALUES('%s', %s, %s, %s, %s, %s);" % (hostname, load,time,memtotal,memusage,memfree)
    4276. ret = c.execute(sql)
    4277. return 'ok'
    4278. except mysql.IntegrityError:
    4279. return 'errer'
    4280. @app.route("/show", methods=["GET", "POST"])
    4281. def show():
    4282. try:
    4283. hostname = request.form.get("hostname") # 获取表单方式的变量值
    4284. sql = "SELECT `load` FROM `statusinfo` WHERE hostname = '%s';" % (hostname)
    4285. c.execute(sql)
    4286. ones = c.fetchall()
    4287. return render_template("sysstatus.html", data=ones, sql = sql)
    4288. except:
    4289. print 'hostname null'
    4290. from flask import render_template
    4291. @app.route("/xxx/<name>")
    4292. def hello_xx(name):
    4293. return render_template("sysstatus.html", name='teach')
    4294. if __name__ == "__main__":
    4295. app.run(host="0.0.0.0", port=50000, debug=True)
    4296. Flask-SQLAlchemy
    4297. http://www.pythondoc.com/flask-sqlalchemy/queries.html#id2
    4298. http://docs.jinkan.org/docs/flask-sqlalchemy/models.html#id2
    4299. https://www.cnblogs.com/mosson/p/6257147.html
    4300. db.create_all() # 创建表
    4301. 增加
    4302. admin = User('admin', 'admin@example.com')
    4303. db.session.add(admin)
    4304. db.session.add(guest)
    4305. db.session.commit()
    4306. 查询
    4307. # 返回数组
    4308. users = User.query.all()
    4309. # 条件过滤 返回一个对象 不存在返回 返回none 像python传参数
    4310. peter = User.query.filter_by(username = 'peter').first()
    4311. # 条件过滤 像sql 可使用 ><
    4312. peter = User.query.filter(username == 'peter').first()
    4313. # 获取指定列的值
    4314. print peter.username
    4315. # 复杂查询 返回列表对象
    4316. User.query.filter(User.email.endswith('@example.com')).all()
    4317. # 对查询结果按指定列排序
    4318. User.query.order_by(User.username)
    4319. # 取前面的指定条数
    4320. User.query.limit(1).all()
    4321. # 通过主键来获取对象
    4322. User.query.get(1)
    4323. # 通配查询 ilike 忽略大小写
    4324. User.query.filter(User.username.ilike('online_%')).all()
    4325. User.query.filter(User.username.notilike('online_%')).all()
    4326. 删除
    4327. user = User.query.get(id)
    4328. db.session.delete(user)
    4329. db.session.commit()
    4330. User.query.filter_by(id=123).delete()
    4331. User.query.filter(User.id == 123).delete()
    4332. db.session.query(Users).filter(Users.id > 2).update({"name" : "099"})
    4333. db.session.commit()
    4334. q = db.session.query(Toner)
    4335. q = q.filter(Toner.toner_id==1)
    4336. record = q.one()
    4337. record.toner_color = 'Azure Radiance'
    4338. db.session.flush()
    4339. 连表
    4340. ret = session.query(Users, Favor).filter(Users.id == Favor.nid).all()
    4341. ret = session.query(Person).join(Favor).all()
    4342. ret = session.query(Person).join(Favor, isouter=True).all()
    4343. 通配符
    4344. ret = session.query(Users).filter(Users.name.like('e%')).all()
    4345. ret = session.query(Users).filter(~Users.name.like('e%')).all()
    4346. 排序
    4347. ret = session.query(Users).order_by(Users.name).all() # 正序
    4348. ret = session.query(Users).order_by(Users.name.desc()).all() # 倒序
    4349. ret = session.query(Users).order_by(Users.name.desc(), Users.id.asc()).all()
    4350. twisted [非阻塞异步服务器框架]
    4351. # 较老 推荐使用 协程框架 或 微线程框架
    4352. # 用来进行网络服务和应用程序的编程。虽然 Twisted Matrix 中有大量松散耦合的模块化组件,但该框架的中心概念还是非阻塞异步服务器这一思想。对于习惯于线程技术或分叉服务器的开发人员来说,这是一种新颖的编程风格,但它却能在繁重负载的情况下带来极高的效率。
    4353. pip install twisted
    4354. from twisted.internet import protocol, reactor, endpoints
    4355. class Echo(protocol.Protocol):
    4356. def dataReceived(self, data):
    4357. self.transport.write(data)
    4358. class EchoFactory(protocol.Factory):
    4359. dDescribeInstanceStatusef buildProtocol(self, addr):
    4360. return Echo()
    4361. endpoints.serverFromString(reactor, "tcp:1234").listen(EchoFactory())
    4362. reactor.run()
    4363. 服务端
    4364. #!/usr/bin/env python
    4365. from twisted.application import service, internet
    4366. from txjsonrpc.netstring import jsonrpc
    4367. class Example(jsonrpc.JSONRPC):
    4368. """An example object to be published."""
    4369. def jsonrpc_echo(self, x):
    4370. """Return all passed args."""
    4371. return x
    4372. def jsonrpc_add(self, a, b):
    4373. """Return sum of arguments."""
    4374. print "add", a, b
    4375. return a + b
    4376. factory = jsonrpc.RPCFactory(Example())
    4377. application = service.Application("Example JSON-RPC Server")
    4378. jsonrpcServer = internet.TCPServer(7080, factory)
    4379. jsonrpcServer.setServiceParent(application)
    4380. 客户端
    4381. #!/usr/bin/env python
    4382. import os
    4383. import sys
    4384. sys.path.insert(0, os.getcwd())
    4385. from twisted.internet import reactor
    4386. from txjsonrpc.netstring.jsonrpc import Proxy
    4387. def printValue(value):
    4388. print "Result: %s" % str(value)
    4389. reactor.stop()
    4390. def printError(error):
    4391. print 'error', error
    4392. reactor.stop()
    4393. proxy = Proxy('127.0.0.1', 7080)
    4394. proxy.callRemote('add', 3, 5).addCallbacks(printValue, printError)
    4395. reactor.run()
    4396. Celery [分布式任务队列]
    4397. # http://docs.jinkan.org/docs/celery/getting-started/introduction.html
    4398. pip install -U Celery
    4399. tornado [极轻量级Web服务器框架]
    4400. # 高可伸缩性和epoll非阻塞IO,响应快速,可处理数千并发连接,特别适用用于实时的Web服务 底层是gevent协程
    4401. # http://www.tornadoweb.cn/documentation
    4402. # http://old.sebug.net/paper/books/tornado/#_2
    4403. # http://demo.pythoner.com/itt2zh/ch5.html
    4404. # 非阻塞方式生成子进程
    4405. # https://github.com/vukasin/tornado-subprocess
    4406. pip install tornado
    4407. self.get_argument() # 方法来获取查询字符串参数,以及解析 POST 的内容
    4408. self.request.arguments # 所有的 GET 或 POST 的参数
    4409. self.request.files # 所有通过 multipart/form-data POST 请求上传的文件
    4410. self.request.path # 请求的路径( ? 之前的所有内容)
    4411. self.request.headers # 请求的开头信息
    4412. callback # 执行完成后执行回调函数
    4413. @tornado.web.asynchronous # 非阻塞异步装饰器
    4414. self.finish() # 使用非阻塞异步 必须调用 self.finish() 已完成 HTTTP 请求
    4415. # 异步 HTTP 客户端 两种模式 默认 SimpleAsyncHTTPClient 如果要修改为 CurlAsyncHTTPClient
    4416. AsyncHTTPClient.configure('tornado.curl_httpclient.CurlAsyncHTTPClient')
    4417. import tornado.ioloop
    4418. import tornado.web
    4419. import tornado.httpclient
    4420. import json
    4421. class MainHandler(tornado.web.RequestHandler):
    4422. def get(self):
    4423. self.write("Hello, world")
    4424. def post(self):
    4425. self.set_header("Content-Type", "text/plain")
    4426. self.write("You wrote " + self.get_argument("message"))
    4427. class Template(tornado.web.RequestHandler):
    4428. def get(self):
    4429. items = ["Item 1", "Item 2", "Item 3"]
    4430. self.render("template.html", title="My title", items=items)
    4431. class urlhttp(tornado.web.RequestHandler):
    4432. @tornado.web.asynchronous
    4433. def get(self):
    4434. http = tornado.httpclient.AsyncHTTPClient()
    4435. http.fetch("http://friendfeed-api.com/v2/feed/bret", callback=self.on_response)
    4436. def on_response(self, response):
    4437. if response.error: raise tornado.web.HTTPError(500)
    4438. jsondata = tornado.escape.json_decode(response.body)
    4439. print type(jsondata)
    4440. self.write(json.dumps(jsondata))
    4441. self.finish()
    4442. class StoryHandler(tornado.web.RequestHandler):
    4443. def get(self, story_id):
    4444. self.write("You requested the story " + story_id)
    4445. def make_app():
    4446. return tornado.web.Application([
    4447. (r"/", MainHandler),
    4448. (r"/template", Template),
    4449. (r"/story/([0-9]+)", StoryHandler),
    4450. (r"/tapi", urlhttp),
    4451. ])
    4452. if __name__ == "__main__":
    4453. app = make_app()
    4454. app.listen(8888)
    4455. tornado.ioloop.IOLoop.current().start()
    4456. Scrapy [web抓取框架]
    4457. # Python开发的一个快速,高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据。Scrapy用途广泛,可以用于数据挖掘、监测和自动化测试。
    4458. pip install scrapy
    4459. from scrapy import Spider, Item, Field
    4460. class Post(Item):
    4461. title = Field()
    4462. class BlogSpider(Spider):
    4463. name, start_urls = 'blogspider', ['http://blog.scrapinghub.com']
    4464. def parse(self, response):
    4465. return [Post(title=e.extract()) for e in response.css("h2 a::text")]
    4466. scrapy runspider myspider.py
    4467. django [重量级web框架]
    4468. bottle [轻量级的Web框架]
    4469. stackless [增强版python]
    4470. 微线程扩展,是一种低开销、轻量级的便利工具 避免传统线程所带来的性能与复杂度问题
    4471. greenlet [微线程/协程框架]
    4472. # 更加原始的微线程的概念,没有调度,或者叫做协程。这在你需要控制你的代码时很有用。你可以自己构造微线程的 调度器;也可以使用"greenlet"实现高级的控制流。例如可以重新创建构造器;不同于Python的构造器,我们的构造器可以嵌套的调用函数,而被嵌套的函数也可以 yield 一个值。
    4473. pip install greenlet
    4474. asyncio [异步I/O协同]
    4475. # https://docs.python.org/3/library/asyncio.html
    4476. 需要python3.4+
    4477. asyncio: 协同程序和事件循环。协同程序像是方法,但是它们可以在代码中的特定点暂停和继续。当在等待一个IO(比如一个HTTP请求),同时执行另一个请求的时候,可以用来暂停一个协同程序。我们使用关键字yield from来设定一个状态,表明我们需要一个协同程序的返回值。而事件循环则被用来安排协同程序的执行。
    4478. 10例子
    4479. 小算法
    4480. 斐波那契
    4481. #将函数结果作为列表可用于循环
    4482. def fab(max):
    4483. n, a, b = 0, 0, 1
    4484. while n < max:
    4485. yield b
    4486. a, b = b, a + b
    4487. n = n + 1
    4488. for n in fab(5):
    4489. print n
    4490. 乘法口诀
    4491. #!/usr/bin/python
    4492. for i in range(1,10):
    4493. for j in range(1,i+1):
    4494. print j,'*',i,'=',j*i,
    4495. else:
    4496. print ''
    4497. 最小公倍数
    4498. # 1-70的最小公倍数
    4499. def c(m,n):
    4500. a1=m
    4501. b1=n
    4502. r=n%m
    4503. while r!=0:
    4504. n=m
    4505. m=r
    4506. r=n%m
    4507. return (a1*b1)/m
    4508. d=1
    4509. for i in range(3,71,2):
    4510. d = c(d,i)
    4511. print d
    4512. 排序算法
    4513. 插入排序
    4514. def insertion_sort(sort_list):
    4515. iter_len = len(sort_list)
    4516. if iter_len < 2:
    4517. return sort_list
    4518. for i in range(1, iter_len):
    4519. key = sort_list[i]
    4520. j = i - 1
    4521. while j>=0 and sort_list[j]>key:
    4522. sort_list[j+1] = sort_list[j]
    4523. j -= 1
    4524. sort_list[j+1] = key
    4525. return sort_list
    4526. 选择排序
    4527. def selection_sort(sort_list):
    4528. iter_len = len(sort_list)
    4529. if iter_len < 2:
    4530. return sort_list
    4531. for i in range(iter_len-1):
    4532. smallest = sort_list[i]
    4533. location = i
    4534. for j in range(i, iter_len):
    4535. if sort_list[j] < smallest:
    4536. smallest = sort_list[j]
    4537. location = j
    4538. if i != location:
    4539. sort_list[i], sort_list[location] = sort_list[location], sort_list[i]
    4540. return sort_list
    4541. 冒泡排序
    4542. def bubblesort(numbers):
    4543. for j in range(len(numbers)-1,-1,-1):
    4544. for i in range(j):
    4545. if numbers[i]>numbers[i+1]:
    4546. numbers[i],numbers[i+1] = numbers[i+1],numbers[i]
    4547. print(i,j)
    4548. print(numbers)
    4549. 快速排序
    4550. # 先从数列中取出一个数作为基准数。
    4551. # 分区过程,将比这个数大的数全放到它的右边,小于或等于它的数全放到它的左边。
    4552. # 再对左右区间重复第二步,直到各区间只有一个数。
    4553. #!/usr/bin/python
    4554. # -*- coding: utf-8 -*-
    4555. def sub_sort(array,low,high):
    4556. key = array[low]
    4557. while low < high:
    4558. while low < high and array[high] >= key:
    4559. high -= 1
    4560. while low < high and array[high] < key:
    4561. array[low] = array[high]
    4562. low += 1
    4563. array[high] = array[low]
    4564. array[low] = key
    4565. return low
    4566. def quick_sort(array,low,high):
    4567. if low < high:
    4568. key_index = sub_sort(array,low,high)
    4569. quick_sort(array,low,key_index)
    4570. quick_sort(array,key_index+1,high)
    4571. if __name__ == '__main__':
    4572. array = [8,10,9,6,4,16,5,13,26,18,2,45,34,23,1,7,3]
    4573. print array
    4574. quick_sort(array,0,len(array)-1)
    4575. print array
    4576. 二分算法
    4577. #python 2f.py 123456789 4
    4578. # list('123456789') = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
    4579. #!/usr/bin/env python
    4580. import sys
    4581. def search2(a,m):
    4582. low = 0
    4583. high = len(a) - 1
    4584. while(low <= high):
    4585. mid = (low + high)/2
    4586. midval = a[mid]
    4587. if midval < m:
    4588. low = mid + 1
    4589. elif midval > m:
    4590. high = mid - 1
    4591. else:
    4592. print mid
    4593. return mid
    4594. print -1
    4595. return -1
    4596. if __name__ == "__main__":
    4597. a = [int(i) for i in list(sys.argv[1])]
    4598. m = int(sys.argv[2])
    4599. search2(a,m)
    4600. 全排序
    4601. def Mideng(li):
    4602. if(type(li)!=list):
    4603. return
    4604. if(len(li)==1):
    4605. return [li]
    4606. result=[]
    4607. for i in range(0,len(li[:])):
    4608. bak=li[:]
    4609. head=bak.pop(i)
    4610. for j in Mideng(bak):
    4611. j.insert(0,head)
    4612. result.append(j)
    4613. return result
    4614. def MM(n):
    4615. if(type(n)!=int or n<2):
    4616. return
    4617. return Mideng(list(range(1,n)))
    4618. MM(6)
    4619. 嵌套复杂排序
    4620. 字典排序
    4621. 按照键值(value)排序
    4622. # a = {'a': 'China', 'c': 'USA', 'b': 'Russia', 'd': 'Canada'}
    4623. b = sorted(a.items(), key=lambda x: x[1], reverse=True)
    4624. #[('c', 'USA'), ('b', 'Russia'), ('a', 'China'), ('d', 'Canada')]
    4625. 按照键名(key)排序
    4626. #a = {'a': 'China', 'c': 'USA', 'b': 'Russia', 'd': 'Canada'}
    4627. b = sorted(a.items(), key=lambda x: x[0], reverse=True)
    4628. #[('d', 'Canada'), ('c', 'USA'), ('b', 'Russia'), ('a', 'China')]
    4629. 嵌套字典, 按照字典键名(key)排序
    4630. #a = {'a': {'b': 'China'}, 'c': {'d': 'USA'}, 'b': {'c': 'Russia'}, 'd': {'a': 'Canada'}}
    4631. b = sorted(a.items(), key=lambda x: x[1], reverse=True)
    4632. #[('c', {'d': 'USA'}), ('b', {'c': 'Russia'}), ('a', {'b': 'China'}), ('d', {'a': 'Canada'})]
    4633. 嵌套列表, 针对列表第一个元素排序( 其实直接写 x: x[1] 就是按照第一个值排序. )
    4634. #a = {'a': [1, 3], 'c': [3, 4], 'b': [0, 2], 'd': [2, 1]}
    4635. b = sorted(a.items(), key=lambda x: x[1][0], reverse=True)
    4636. #[('c', [3, 4]), ('d', [2, 1]), ('a', [1, 3]), ('b', [0, 2])]
    4637. 嵌套列表, 按照列表其他元素排序 只需要修改列表对应的下标
    4638. # a = {'a': [1, 3], 'c': [3, 4], 'b': [0, 2], 'd': [2, 1]}
    4639. b = sorted(a.items(), key=lambda x: x[1][1], reverse=True)
    4640. # [('c', [3, 4]), ('a', [1, 3]), ('b', [0, 2]), ('d', [2, 1])]
    4641. # 总结: 此处使用lambda方法, x: x[1][1] 就可以看做是在访问字典的值, 想要按照哪个数值排序, 用相应的坐标对应即可, 但当字典过于复杂后, 应该选择用元组存储, 简化排序过程.
    4642. 列表排序
    4643. 1: 按照字母排序
    4644. # a = ['USA', 'China', 'Canada', 'Russia']
    4645. a.sort(reverse=True)
    4646. # ['USA', 'Russia', 'China', 'Canada']
    4647. 2: 嵌套列表的排序, 按照子列表的其他值排序雷系, 修改x[0] 这里的下标即可
    4648. # a = [['USA', 'b'], ['China', 'c'], ['Canada', 'd'], ['Russia', 'a']]
    4649. a.sort(key=lambda x: x[0], reverse=True)
    4650. # [['USA', 'b'], ['Russia', 'a'], ['China', 'c'], ['Canada', 'd']]
    4651. 3: 嵌套字典, 按照字典值(value) 排序
    4652. # a = [{'letter': 'b'}, {'letter': 'c'}, {'letter': 'd'}, {'letter': 'a'}]
    4653. a.sort(key=lambda x: x['letter'], reverse=True)
    4654. # [{'letter': 'd'}, {'letter': 'c'}, {'letter': 'b'}, {'letter': 'a'}]
    4655. 4: 当字典值也是字典时, 这时候会优先按照键名排序, 再按照键值排序. 例子如下
    4656. # a = [{'letter': {'a': 'b'}}, {'letter': {'a': 'c'}}, {'letter': {'a': 'd'}}, {'letter': {'a': 'a'}}]
    4657. a.sort(key=lambda x: x['letter'], reverse=True)
    4658. # [{'letter': {'a': 'd'}}, {'letter': {'a': 'c'}}, {'letter': {'a': 'b'}}, {'letter': {'a': 'a'}}]
    4659. 方法2:
    4660. # a = [{'letter': {'a': 'b'}}, {'letter': {'b': 'c'}}, {'letter': {'c': 'd'}}, {'letter': {'d': 'a'}}]
    4661. a.sort(key=lambda x: x['letter'], reverse=True)
    4662. #[{'letter': {'d': 'a'}}, {'letter': {'c': 'd'}}, {'letter': {'b': 'c'}}, {'letter': {'a': 'b'}}]
    4663. 1000以内是3或者是5的倍数的值的和
    4664. sum([ num for num in range(1, 1000) if num % 3 == 0 or num % 5 == 0 ])
    4665. 打印如下列表
    4666. 1
    4667. 2 1
    4668. 3 2 1
    4669. 4 3 2 1
    4670. 5 4 3 2 1
    4671. 6 5 4 3 2 1
    4672. #!/usr/local/python
    4673. i=1
    4674. while i < 7:
    4675. a = ""
    4676. n=1
    4677. while n <= i:
    4678. a = "%s %s" %(n, a)
    4679. n = n + 1
    4680. print a
    4681. i = i + 1
    4682. 将字典中所有time去掉
    4683. a={'version01': {'nba': {'timenba': 'valuesasdfasdf', 'nbanbac': 'vtimefasdf', 'userasdf': 'vtimasdf'}}}
    4684. eval(str(a).replace("time",""))
    4685. 阿里云oss
    4686. https://help.aliyun.com/document_detail/32027.html?spm=5176.doc32026.6.674.AXf7Lw
    4687. pip install oss2
    4688. # -*- coding: utf-8 -*-
    4689. import oss2
    4690. auth = oss2.Auth('AccessKeyId', 'AccessKeySecret')
    4691. # 注意内外网域名 不带bucket
    4692. service = oss2.Service(auth, 'oss-cn-shanghai-internal.aliyuncs.com')
    4693. print([b.name for b in oss2.BucketIterator(service)]) # 查看存在的bucket
    4694. bucket = oss2.Bucket(auth, 'http://oss-cn-shanghai-internal.aliyuncs.com', 'ec-share')
    4695. # bucket.create_bucket(oss2.models.BUCKET_ACL_PRIVATE) # 创建bucket
    4696. bucket.put_object_from_file('remote.txt','/tmp/local.txt') # 上传文件
    4697. bucket.get_object_to_file('remote.txt', 'local-backup.txt') # 下载文件
    4698. bucket.delete_object('remote.txt') # 删除文件
    4699. 阿里云ecs
    4700. https://help.aliyun.com/document_detail/67117.html?spm=a2c4g.11186623.6.543.390360e41Cfpqm
    4701. pip install aliyun-python-sdk-core # 安装阿里云SDK核心库
    4702. pip install aliyun-python-sdk-ecs # 安装管理ECS的库
    4703. from aliyunsdkcore.client import AcsClient
    4704. from aliyunsdkcore.acs_exception.exceptions import ClientException
    4705. from aliyunsdkcore.acs_exception.exceptions import ServerException
    4706. from aliyunsdkecs.request.v20140526 import DescribeInstancesRequest
    4707. from aliyunsdkecs.request.v20140526 import StopInstanceRequest
    4708. client = AcsClient(
    4709. "your-access-key-id",
    4710. "your-access-key-secret",
    4711. "your-region-id"
    4712. );
    4713. request = DescribeInstancesRequest.DescribeInstancesRequest()
    4714. request.set_PageSize(10)
    4715. try:
    4716. response = client.do_action_with_exception(request)
    4717. print response
    4718. except ServerException as e:
    4719. print e
    4720. except ClientException as e:
    4721. print e
    4722. # 使用CommonRequest的方式调用ECS的 DescribeInstanceStatus 接口
    4723. from aliyunsdkcore.client import AcsClient
    4724. from aliyunsdkcore.request import CommonRequest
    4725. client = AcsClient('your_access_key_id', 'your_access_key_secret', 'your_region_id')
    4726. request = CommonRequest()
    4727. request.set_domain('ecs.aliyuncs.com')
    4728. request.set_version('2014-05-26')
    4729. request.set_action_name('DescribeInstanceStatus')
    4730. request.add_query_param('PageNumber', '1')
    4731. request.add_query_param('PageSize', '30')
    4732. request.add_query_param('ZoneId', 'cn-shanghai-d')
    4733. response = client.do_action_with_exception(request)
    4734. # 接口列表
    4735. https://help.aliyun.com/document_detail/25506.html?spm=a2c4g.11186623.6.1084.2f672eafMskx7S
    4736. # 调用DescribeInstances查询一台或多台实例的详细信息
    4737. DescribeInstances
    4738. # 调用CreateInstance创建一台ECS实例
    4739. CreateInstance
    4740. # 调用StartInstance启动一台实例
    4741. StartInstance
    4742. # 调用StopInstance停止运行一台实例
    4743. StopInstance
    4744. # 调用DescribeInstanceStatus获取一台或多台ECS实例的状态信息
    4745. DescribeInstanceStatus
    4746. # 创建ecs, CreateInstance, stop状态
    4747. # 参数列表
    4748. # https://help.aliyun.com/document_detail/25499.html?spm=a2c4g.11186623.6.1095.4347431djUtw2v
    4749. from aliyunsdkcore.client import AcsClient
    4750. from aliyunsdkcore.request import CommonRequest
    4751. client = AcsClient('LTAIzeBZre', 'fLJOBweE8qHKxrEOnc2FIF', 'cn-shanghai')
    4752. request = CommonRequest()
    4753. request.set_domain('ecs.aliyuncs.com')
    4754. request.set_version('2014-05-26')
    4755. request.set_action_name('CreateInstance')
    4756. request.add_query_param('ImageId', 'm-uf67jei1pul0xpfsfpfv')
    4757. request.add_query_param('InstanceType', 'ecs.c5.large')
    4758. request.add_query_param('RegionId', 'cn-shanghai')
    4759. request.add_query_param('ZoneId', 'cn-shanghai-f')
    4760. request.add_query_param('SecurityGroupId', 'sg-uf6i53pjsi11yuyrwyqs')
    4761. request.add_query_param('VSwitchId', 'vsw-uf630eqh0edoe9n3ig7lz')
    4762. request.add_query_param('Period', '1')
    4763. request.add_query_param('InstanceChargeType', 'PrePaid')
    4764. request.add_query_param('AutoRenew', 'true')
    4765. request.add_query_param('AutoRenewPeriod', '1')
    4766. request.add_query_param('InstanceName', 'xuesong-test1')
    4767. request.add_query_param('HostName', 'xuesong-test1')
    4768. request.add_query_param('Password', 'azuDa9nee6aiHaey')
    4769. request.add_query_param('SystemDisk.Size', '200')
    4770. request.add_query_param('SystemDisk.Category', 'cloud_efficiency')
    4771. request.add_query_param('SystemDisk.DiskName', 'xuesong-test1')
    4772. response = client.do_action_with_exception(request)
    4773. # InstanceId # 实例ID,是访问实例的唯一标识
    4774. # RequestId # 无论调用接口成功与否,都会返回请求ID
    4775. # 启动ecs StartInstance
    4776. from aliyunsdkcore.client import AcsClient
    4777. from aliyunsdkcore.request import CommonRequest
    4778. client = AcsClient('LTAIzeBZre', 'fLJOBweE8qHKxrEOnc2FIF', 'cn-shanghai')
    4779. request = CommonRequest()
    4780. request.set_domain('ecs.aliyuncs.com')
    4781. request.set_version('2014-05-26')
    4782. request.set_action_name('StartInstance')
    4783. request.add_query_param('InstanceId', 'i-uf69e821lkybxke6yyno')
    4784. response = client.do_action_with_exception(request)
    4785. # 查询ecs信息 DescribeInstances
    4786. from aliyunsdkcore.client import AcsClient
    4787. from aliyunsdkcore.request import CommonRequest
    4788. import json
    4789. client = AcsClient('LTAIzeBZre', 'fLJOBweE8qHKxrEOnc2FIF', 'cn-shanghai')
    4790. request = CommonRequest()
    4791. request.set_domain('ecs.aliyuncs.com')
    4792. request.set_version('2014-05-26')
    4793. request.set_action_name('DescribeInstances')
    4794. request.add_query_param('InstanceIds', ['i-uf69e821lkybxke6yyno'])
    4795. response = client.do_action_with_exception(request)
    4796. jresponse = json.loads(response)
    4797. ip = jresponse['Instances']['Instance'][0]['NetworkInterfaces']['NetworkInterface'][0]['PrimaryIpAddress']
    4798. status = jresponse['Instances']['Instance'][0]['Status']
    4799. # Stopped Stopping Starting Running
    4800. # 停止ecs StopInstance
    4801. from aliyunsdkcore.client import AcsClient
    4802. from aliyunsdkcore.request import CommonRequest
    4803. client = AcsClient('LTAIzeBZre', 'fLJOBweE8qHKxrEOnc2FIF', 'cn-shanghai')
    4804. request = CommonRequest()
    4805. request.set_domain('ecs.aliyuncs.com')
    4806. request.set_version('2014-05-26')
    4807. request.set_action_name('StopInstance')
    4808. request.add_query_param('InstanceId', 'i-uf69e821lkybxke6yyno')
    4809. response = client.do_action_with_exception(request)
    4810. # 删除ecs DeleteInstance 释放一台按量付费实例或者到期的预付费(包年包月)实例
    4811. from aliyunsdkcore.client import AcsClient
    4812. from aliyunsdkcore.request import CommonRequest
    4813. client = AcsClient('LTAIzeBZre', 'fLJOBweE8qHKxrEOnc2FIF', 'cn-shanghai')
    4814. request = CommonRequest()
    4815. request.set_domain('ecs.aliyuncs.com')
    4816. request.set_version('2014-05-26')
    4817. request.set_action_name('DeleteInstance')
    4818. request.add_query_param('InstanceId', 'i-uf69e821lkybxke6yyno')
    4819. request.add_query_param('Force', 'true')
    4820. response = client.do_action_with_exception(request)
    4821. PIL图像处理
    4822. import Image
    4823. im = Image.open("j.jpg") # 打开图片
    4824. print im.format, im.size, im.mode # 打印图像格式、像素宽和高、模式
    4825. # JPEG (440, 330) RGB
    4826. im.show() # 显示最新加载图像
    4827. box = (100, 100, 200, 200)
    4828. region = im.crop(box) # 从图像中提取出某个矩形大小的图像
    4829. 图片等比缩小
    4830. # -*- coding: cp936 -*-
    4831. import Image
    4832. import glob, os
    4833. #图片批处理
    4834. def timage():
    4835. for files in glob.glob('D:\\1\\*.JPG'):
    4836. filepath,filename = os.path.split(files)
    4837. filterame,exts = os.path.splitext(filename)
    4838. #输出路径
    4839. opfile = r'D:\\22\\'
    4840. #判断opfile是否存在,不存在则创建
    4841. if (os.path.isdir(opfile)==False):
    4842. os.mkdir(opfile)
    4843. im = Image.open(files)
    4844. w,h = im.size
    4845. #im_ss = im.resize((400,400))
    4846. #im_ss = im.convert('P')
    4847. im_ss = im.resize((int(w*0.12), int(h*0.12)))
    4848. im_ss.save(opfile+filterame+'.jpg')
    4849. if __name__=='__main__':
    4850. timage()
    4851. 取系统返回值赋给序列
    4852. cmd = os.popen("df -Ph|awk 'NR!=1{print $5}'").readlines();
    4853. cmd = os.popen('df -h').read().split('\n')
    4854. cmd = os.popen('lo 2>&1').read()
    4855. #取磁盘使用空间
    4856. import commands
    4857. df = commands.getoutput("df -hP")
    4858. [ x.split()[4] for x in df.split("\n") ]
    4859. [ (x.split()[0],x.split()[4]) for x in df.split("\n") if x.split()[4].endswith("%") ]
    4860. 切片获取星星
    4861. def getRating(rating):
    4862. return '★★★★★☆☆☆☆☆'.decode('utf8')[5-rating:10-rating]
    4863. print getRating(1)
    4864. print getRating(3)
    4865. 打印表格
    4866. map = [["a","b","c"],
    4867. ["d","e","f"],
    4868. ["g","h","i"]]
    4869. def print_board():
    4870. for i in range(0,3):
    4871. for j in range(0,3):
    4872. print "|",map[i][j],
    4873. #if j != 2:
    4874. print '|'
    4875. 井字游戏
    4876. #!/usr/bin/python
    4877. # http://www.admin10000.com/document/2506.html
    4878. def print_board():
    4879. for i in range(0,3):
    4880. for j in range(0,3):
    4881. print map[2-i][j],
    4882. if j != 2:
    4883. print "|",
    4884. print ""
    4885. def check_done():
    4886. for i in range(0,3):
    4887. if map[i][0] == map[i][1] == map[i][2] != " " \
    4888. or map[0][i] == map[1][i] == map[2][i] != " ":
    4889. print turn, "won!!!"
    4890. return True
    4891. if map[0][0] == map[1][1] == map[2][2] != " " \
    4892. or map[0][2] == map[1][1] == map[2][0] != " ":
    4893. print turn, "won!!!"
    4894. return True
    4895. if " " not in map[0] and " " not in map[1] and " " not in map[2]:
    4896. print "Draw"
    4897. return True
    4898. return False
    4899. turn = "X"
    4900. map = [[" "," "," "],
    4901. [" "," "," "],
    4902. [" "," "," "]]
    4903. done = False
    4904. while done != True:
    4905. print_board()
    4906. print turn, "'s turn"
    4907. print
    4908. moved = False
    4909. while moved != True:
    4910. print "Please select position by typing in a number between 1 and 9, see below for which number that is which position..."
    4911. print "7|8|9"
    4912. print "4|5|6"
    4913. print "1|2|3"
    4914. print
    4915. try:
    4916. pos = input("Select: ")
    4917. if pos <=9 and pos >=1:
    4918. Y = pos/3
    4919. X = pos%3
    4920. if X != 0:
    4921. X -=1
    4922. else:
    4923. X = 2
    4924. Y -=1
    4925. if map[Y][X] == " ":
    4926. map[Y][X] = turn
    4927. moved = True
    4928. done = check_done()
    4929. if done == False:
    4930. if turn == "X":
    4931. turn = "O"
    4932. else:
    4933. turn = "X"
    4934. except:
    4935. print "You need to add a numeric value"
    4936. 网段划分
    4937. 题目
    4938. 192.168.1
    4939. 192.168.3
    4940. 192.168.2
    4941. 172.16.3
    4942. 192.16.1
    4943. 192.16.2
    4944. 192.16.3
    4945. 10.0.4
    4946. 输出结果:
    4947. 192.16.1-192.16.3
    4948. 192.168.1-192.168.3
    4949. 172.16.3
    4950. 10.0.4
    4951. 答案
    4952. #!/usr/bin/python
    4953. f = file('a.txt')
    4954. c = f.readlines()
    4955. dic={}
    4956. for i in c:
    4957. a=i.strip().split('.')
    4958. if a[0]+'.'+a[1] in dic.keys():
    4959. key=dic["%s.%s" %(a[0],a[1])]
    4960. else:
    4961. key=[]
    4962. key.append(a[2])
    4963. dic[a[0]+'.'+a[1]]=sorted(key)
    4964. for x,y in dic.items():
    4965. if y[0] == y[-1]:
    4966. print '%s.%s' %(x,y[0])
    4967. else:
    4968. print '%s.%s-%s.%s' %(x,y[0],x,y[-1])
    4969. 统计日志IP
    4970. # 打印出独立IP,并统计独立IP数
    4971. 219.140.190.130 - - [23/May/2006:08:57:59 +0800] "GET /fg172.exe HTTP/1.1" 200 2350253
    4972. 221.228.143.52 - - [23/May/2006:08:58:08 +0800] "GET /fg172.exe HTTP/1.1" 206 719996
    4973. 221.228.143.52 - - [23/May/2006:08:58:08 +0800] "GET /fg172.exe HTTP/1.1" 206 713242
    4974. #!/usr/bin/python
    4975. dic={}
    4976. a=open("a").readlines()
    4977. for i in a:
    4978. ip=i.strip().split()[0]
    4979. if ip in dic.keys():
    4980. dic[ip] = dic[ip] + 1
    4981. else:
    4982. dic[ip] = 1
    4983. for x,y in dic.items():
    4984. print x," ",y
    4985. 多线程下载http
    4986. # 先从文件头中或取content-length的值,即文件大小,在用header中指定Range范围来下载文件中一段字符串
    4987. # 'Range':'bytes=0-499' # 表示头500个字节
    4988. # 'Range':'bytes=-500' # 表示最后500个字节
    4989. # 'Range':'bytes=500-' # 表示500字节以后的范围
    4990. # 'Range':'bytes=0-0,-1' # 第一个和最后一个字节
    4991. # 'Range':'bytes=50-60,61-99' # 同时指定几个范围
    4992. #!/usr/bin/env python
    4993. #encoding:utf8
    4994. import urllib2
    4995. import threading
    4996. class myThread(threading.Thread):
    4997. def __init__(self, url_file, scope, url):
    4998. threading.Thread.__init__(self)
    4999. self.url_file = url_file
    5000. self.scope = scope
    5001. self.url = url
    5002. def run(self):
    5003. req_header = {'User-Agent':"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)",
    5004. 'Accept':'text/html;q=0.9,*/*;q=0.8',
    5005. 'Range':'bytes=%s' % self.scope,
    5006. 'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
    5007. 'Connection':'close',
    5008. }
    5009. req = urllib2.Request(self.url, headers=req_header)
    5010. data = urllib2.urlopen(req, data=None).read()
    5011. start_value = int(self.scope.split('-')[0])
    5012. threadLock.acquire()
    5013. self.url_file.seek(start_value)
    5014. self.url_file.write(data)
    5015. self.url_file.flush()
    5016. threadLock.release()
    5017. if __name__ == '__main__':
    5018. url = 'http://dldir1.qq.com/qqfile/qq/QQ7.1/14522/QQ7.1.exe'
    5019. size=int(urllib2.urlopen(url).info()['content-length'])
    5020. print size
    5021. threadnum = 4
    5022. len = size / threadnum
    5023. current = 0
    5024. url_file = file(url.split('/')[-1],'wb+')
    5025. threadLock = threading.Lock()
    5026. threads = []
    5027. for tName in range(1, threadnum + 1):
    5028. if tName < threadnum:
    5029. scope = "%d-%d" %(current,len * tName - 1)
    5030. current = len * tName
    5031. elif tName == threadnum:
    5032. scope = "%d-" %(current)
    5033. print scope
    5034. thread = myThread(url_file, scope, url)
    5035. thread.start()
    5036. threads.append(thread)
    5037. for t in threads:
    5038. t.join()
    5039. url_file.flush()
    5040. url_file.close()
    5041. 获取网卡流量
    5042. #!/usr/bin/env python
    5043. net = []
    5044. f = open("/proc/net/dev")
    5045. lines = f.readlines()
    5046. f.close()
    5047. for line in lines[3:]:
    5048. con = line.split()
    5049. intf = dict(
    5050. zip(
    5051. ( 'interface', 'ReceiveBytes', 'ReceivePackets', 'TransmitBytes', 'TransmitPackets',),
    5052. ( con[0].split(":")[0], con[0].split(":")[1], int(con[1]), int(con[8]), int(con[9]),)
    5053. )
    5054. )
    5055. net.append(intf)
    5056. print net
    5057. 阿里云sdk接口
    5058. # 阿里云接口列表
    5059. https://develop.aliyun.com/tools/sdk?#/python
    5060. # python sdk模块
    5061. https://help.aliyun.com/document_detail/30003.html?spm=5176.doc29995.2.1.htCtSa
    5062. # 接口参数详解
    5063. https://help.aliyun.com/document_detail/25500.html?spm=5176.doc25499.6.691.lWwhc0
    5064. pip install aliyun-python-sdk-core aliyun-python-sdk-ecs
    5065. dir(aliyunsdkecs.request)
    5066. v20140526
    5067. aliyunsdkecs.request.v20140526
    5068. #!/usr/bin/env python
    5069. from aliyunsdkcore import client
    5070. from aliyunsdkecs.request.v20140526 import DescribeRegionsRequest
    5071. clt = client.AcsClient('SFAW************','Nc2nZ6dQoiqck0*************',
    5072. 'cn-hangzhou')
    5073. request=DescribeRegionsRequest.DescribeRegionsRequest()
    5074. print dir(request)
    5075. request.set_accept_format('json')
    5076. request.set_action_name("CreateInstance")
    5077. print(clt.do_action(request))
    5078. 获取系统监控信息
    5079. #!/usr/bin/env python
    5080. import inspect
    5081. import os,time,socket
    5082. class mon:
    5083. def __init__(self):
    5084. self.data = {}
    5085. def getLoadAvg(self):
    5086. with open('/proc/loadavg') as load_open:
    5087. a = load_open.read().split()[:3]
    5088. #return "%s %s %s" % (a[0],a[1],a[2])
    5089. return float(a[0])
    5090. def getMemTotal(self):
    5091. with open('/proc/meminfo') as mem_open:
    5092. a = int(mem_open.readline().split()[1])
    5093. return a / 1024
    5094. def getMemUsage(self, noBufferCache=True):
    5095. if noBufferCache:
    5096. with open('/proc/meminfo') as mem_open:
    5097. T = int(mem_open.readline().split()[1]) #Total
    5098. F = int(mem_open.readline().split()[1]) #Free
    5099. B = int(mem_open.readline().split()[1]) #Buffer
    5100. C = int(mem_open.readline().split()[1]) #Cache
    5101. return (T-F-B-C)/1024
    5102. else:
    5103. with open('/proc/meminfo') as mem_open:
    5104. a = int(mem_open.readline().split()[1]) - int(mem_open.readline().split()[1])
    5105. return a / 1024
    5106. def getMemFree(self, noBufferCache=True):
    5107. if noBufferCache:
    5108. with open('/proc/meminfo') as mem_open:
    5109. T = int(mem_open.readline().split()[1])
    5110. F = int(mem_open.readline().split()[1])
    5111. B = int(mem_open.readline().split()[1])
    5112. C = int(mem_open.readline().split()[1])
    5113. return (F+B+C)/1024
    5114. else:
    5115. with open('/proc/meminfo') as mem_open:
    5116. mem_open.readline()
    5117. a = int(mem_open.readline().split()[1])
    5118. return a / 1024
    5119. def getDiskTotal(self):
    5120. disk = os.statvfs("/")
    5121. Total = disk.f_bsize * disk.f_blocks / 1024 / 1024
    5122. return Total
    5123. def getDiskFree(self):
    5124. disk = os.statvfs("/")
    5125. Free = disk.f_bsize * disk.f_bavail / 1024 / 1024
    5126. return Free
    5127. def getTraffic(self):
    5128. traffic = {}
    5129. f = open("/proc/net/dev")
    5130. lines = f.readlines()
    5131. f.close()
    5132. for line in lines[3:]:
    5133. con = line.split()
    5134. intf = dict(
    5135. zip(
    5136. ('ReceiveBytes', 'TransmitBytes',),
    5137. (con[0].split(":")[1], int(con[8]),)
    5138. )
    5139. )
    5140. traffic[con[0].split(":")[0]] = intf
    5141. return traffic
    5142. def getHost(self):
    5143. #return ['host1', 'host2', 'host3', 'host4', 'host5'][int(time.time() * 1000.0) % 5]
    5144. return socket.gethostname()
    5145. def getTime(self):
    5146. return int(time.time())
    5147. def runAllGet(self):
    5148. for fun in inspect.getmembers(self, predicate=inspect.ismethod):
    5149. if fun[0][:3] == 'get':
    5150. self.data[fun[0][3:]] = fun[1]()
    5151. return self.data
    5152. if __name__ == "__main__":
    5153. print mon().runAllGet()
    5154. nginx_5xx钉钉报警
    5155. import os
    5156. import sys
    5157. import datetime
    5158. import time
    5159. import requests
    5160. import json
    5161. mtime = (datetime.datetime.now()-datetime.timedelta(minutes=1)).strftime("%Y-%m-%dT%H:%M")
    5162. num = int(os.popen('''tail -n 100000 /app/nginx/logs/*_access.log | grep %s |grep 'status": 5' |wc -l ''' % mtime ).read().strip())
    5163. print num
    5164. if num > 20:
    5165. print 'baojing'
    5166. Robot = 'https://oapi.dingtalk.com/robot/send?access_token=e80aa431d237d97217827524'
    5167. headers = {'content-type': 'application/json'}
    5168. content = "lite nginx dmz01 5XX: %s" % num
    5169. dingdata = {
    5170. "msgtype": "text",
    5171. "text": {
    5172. "content": content
    5173. }
    5174. }
    5175. try:
    5176. r = requests.post(url=Robot, data=json.dumps(dingdata), headers=headers, timeout=2).json()
    5177. except Exception as err:
    5178. print 'ERROR: notice dingding api error'
    5179. print str(err)
    5180. 获取主机名
    5181. #!/usr/bin/env python
    5182. # -*- coding: utf8 -*-
    5183. #python network.py --host
    5184. import os
    5185. import socket
    5186. """
    5187. copy from:
    5188. http://stackoverflow.com/questions/11735821/python-get-localhost-ip
    5189. """
    5190. if os.name != "nt":
    5191. import fcntl
    5192. import struct
    5193. def get_interface_ip(ifname):
    5194. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    5195. return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))[20:24])
    5196. def lan_ip():
    5197. ip = socket.gethostbyname(socket.gethostname())
    5198. if ip.startswith("127.") and os.name != "nt":
    5199. interfaces = [
    5200. "eth0",
    5201. "eth1",
    5202. "eth2",
    5203. "wlan0",
    5204. "wlan1",
    5205. "wifi0",
    5206. "ath0",
    5207. "ath1",
    5208. "ppp0",
    5209. ]
    5210. for ifname in interfaces:
    5211. try:
    5212. ip = get_interface_ip(ifname)
    5213. break
    5214. except IOError:
    5215. pass
    5216. return ip
    5217. if __name__ == '__main__':
    5218. import sys
    5219. if len(sys.argv) > 1:
    5220. print socket.gethostname()
    5221. sys.exit(0)
    5222. print lan_ip()
    5223. LazyManage并发批量操作(判断非root交互到root操作)
    5224. #!/usr/bin/python
    5225. #encoding:utf8
    5226. # LzayManage.py
    5227. # config file: serverlist.conf
    5228. import paramiko
    5229. import multiprocessing
    5230. import sys,os,time,socket,re
    5231. def Ssh_Cmd(host_ip,Cmd,user_name,user_pwd,port=22):
    5232. s = paramiko.SSHClient()
    5233. s.load_system_host_keys()
    5234. s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    5235. s.connect(hostname=host_ip,port=port,username=user_name,password=user_pwd)
    5236. stdin,stdout,stderr = s.exec_command(Cmd)
    5237. Result = '%s%s' %(stdout.read(),stderr.read())
    5238. q.put('successful')
    5239. s.close()
    5240. return Result.strip()
    5241. def Ssh_Su_Cmd(host_ip,Cmd,user_name,user_pwd,root_name,root_pwd,port=22):
    5242. s = paramiko.SSHClient()
    5243. s.load_system_host_keys()
    5244. s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    5245. s.connect(hostname=host_ip,port=port,username=user_name,password=user_pwd)
    5246. ssh = s.invoke_shell()
    5247. time.sleep(0.1)
    5248. ssh.send('su - %s\n' %(root_name))
    5249. buff = ''
    5250. while not buff.endswith('Password: '):
    5251. resp = ssh.recv(9999)
    5252. buff +=resp
    5253. ssh.send('%s\n' %(root_pwd))
    5254. buff = ''
    5255. while True:
    5256. resp = ssh.recv(9999)
    5257. buff +=resp
    5258. if ': incorrect password' in buff:
    5259. su_correct='passwd_error'
    5260. break
    5261. elif buff.endswith('# '):
    5262. su_correct='passwd_correct'
    5263. break
    5264. if su_correct == 'passwd_correct':
    5265. ssh.send('%s\n' %(Cmd))
    5266. buff = ''
    5267. while True:
    5268. resp = ssh.recv(9999)
    5269. if resp.endswith('# '):
    5270. buff +=re.sub('\[.*@.*\]# $','',resp)
    5271. break
    5272. buff +=resp
    5273. Result = buff.lstrip('%s' %(Cmd))
    5274. q.put('successful')
    5275. elif su_correct == 'passwd_error':
    5276. Result = "\033[31mroot密码错误\033[m"
    5277. s.close()
    5278. return Result.strip()
    5279. def Send_File(host_ip,PathList,user_name,user_pwd,Remote='/tmp',port=22):
    5280. s=paramiko.Transport((host_ip,port))
    5281. s.connect(username=user_name,password=user_pwd)
    5282. sftp=paramiko.SFTPClient.from_transport(s)
    5283. for InputPath in PathList:
    5284. LocalPath = re.sub('^\./','',InputPath.rstrip('/'))
    5285. RemotePath = '%s/%s' %( Remote , os.path.basename( LocalPath ))
    5286. try:
    5287. sftp.rmdir(RemotePath)
    5288. except:
    5289. pass
    5290. try:
    5291. sftp.remove(RemotePath)
    5292. except:
    5293. pass
    5294. if os.path.isdir(LocalPath):
    5295. sftp.mkdir(RemotePath)
    5296. for path,dirs,files in os.walk(LocalPath):
    5297. for dir in dirs:
    5298. dir_path = os.path.join(path,dir)
    5299. sftp.mkdir('%s/%s' %(RemotePath,re.sub('^%s/' %LocalPath,'',dir_path)))
    5300. for file in files:
    5301. file_path = os.path.join(path,file)
    5302. sftp.put( file_path,'%s/%s' %(RemotePath,re.sub('^%s/' %LocalPath,'',file_path)))
    5303. else:
    5304. sftp.put(LocalPath,RemotePath)
    5305. q.put('successful')
    5306. sftp.close()
    5307. s.close()
    5308. Result = '%s \033[32m传送完成\033[m' % PathList
    5309. return Result
    5310. def Ssh(host_ip,Operation,user_name,user_pwd,root_name,root_pwd,Cmd=None,PathList=None,port=22):
    5311. msg = "\033[32m-----------Result:%s----------\033[m" % host_ip
    5312. try:
    5313. if Operation == 'Ssh_Cmd':
    5314. Result = Ssh_Cmd(host_ip=host_ip,Cmd=Cmd,user_name=user_name,user_pwd=user_pwd,port=port)
    5315. elif Operation == 'Ssh_Su_Cmd':
    5316. Result = Ssh_Su_Cmd(host_ip=host_ip,Cmd=Cmd,user_name=user_name,user_pwd=user_pwd,root_name=root_name,root_pwd=root_pwd,port=port)
    5317. elif Operation == 'Ssh_Script':
    5318. Send_File(host_ip=host_ip,PathList=PathList,user_name=user_name,user_pwd=user_pwd,port=port)
    5319. Script_Head = open(PathList[0]).readline().strip()
    5320. LocalPath = re.sub('^\./','',PathList[0].rstrip('/'))
    5321. Cmd = '%s /tmp/%s' %( re.sub('^#!','',Script_Head), os.path.basename( LocalPath ))
    5322. Result = Ssh_Cmd(host_ip=host_ip,Cmd=Cmd,user_name=user_name,user_pwd=user_pwd,port=port)
    5323. elif Operation == 'Ssh_Su_Script':
    5324. Send_File(host_ip=host_ip,PathList=PathList,user_name=user_name,user_pwd=user_pwd,port=port)
    5325. Script_Head = open(PathList[0]).readline().strip()
    5326. LocalPath = re.sub('^\./','',PathList[0].rstrip('/'))
    5327. Cmd = '%s /tmp/%s' %( re.sub('^#!','',Script_Head), os.path.basename( LocalPath ))
    5328. Result = Ssh_Su_Cmd(host_ip=host_ip,Cmd=Cmd,user_name=user_name,user_pwd=user_pwd,root_name=root_name,root_pwd=root_pwd,port=port)
    5329. elif Operation == 'Send_File':
    5330. Result = Send_File(host_ip=host_ip,PathList=PathList,user_name=user_name,user_pwd=user_pwd,port=port)
    5331. else:
    5332. Result = '操作不存在'
    5333. except socket.error:
    5334. Result = '\033[31m主机或端口错误\033[m'
    5335. except paramiko.AuthenticationException:
    5336. Result = '\033[31m用户名或密码错误\033[m'
    5337. except paramiko.BadHostKeyException:
    5338. Result = '\033[31mBad host key\033[m['
    5339. except IOError:
    5340. Result = '\033[31m远程主机已存在非空目录或没有写权限\033[m'
    5341. except:
    5342. Result = '\033[31m未知错误\033[m'
    5343. r.put('%s\n%s\n' %(msg,Result))
    5344. def Concurrent(Conf,Operation,user_name,user_pwd,root_name,root_pwd,Cmd=None,PathList=None,port=22):
    5345. # 读取配置文件
    5346. f=open(Conf)
    5347. list = f.readlines()
    5348. f.close()
    5349. # 执行总计
    5350. total = 0
    5351. # 并发执行
    5352. for host_info in list:
    5353. # 判断配置文件中注释行跳过
    5354. if host_info.startswith('#'):
    5355. continue
    5356. # 取变量,其中任意变量未取到就跳过执行
    5357. try:
    5358. host_ip=host_info.split()[0]
    5359. #user_name=host_info.split()[1]
    5360. #user_pwd=host_info.split()[2]
    5361. except:
    5362. print('Profile error: %s' %(host_info) )
    5363. continue
    5364. try:
    5365. port=int(host_info.split()[3])
    5366. except:
    5367. port=22
    5368. total +=1
    5369. p = multiprocessing.Process(target=Ssh,args=(host_ip,Operation,user_name,user_pwd,root_name,root_pwd,Cmd,PathList,port))
    5370. p.start()
    5371. # 打印执行结果
    5372. for j in range(total):
    5373. print(r.get() )
    5374. if Operation == 'Ssh_Script' or Operation == 'Ssh_Su_Script':
    5375. successful = q.qsize() / 2
    5376. else:
    5377. successful = q.qsize()
    5378. print('\033[32m执行完毕[总执行:%s 成功:%s 失败:%s]\033[m' %(total,successful,total - successful) )
    5379. q.close()
    5380. r.close()
    5381. def Help():
    5382. print(''' 1.执行命令
    5383. 2.执行脚本 \033[32m[位置1脚本(必须带脚本头),后可带执行脚本所需要的包\文件\文件夹路径,空格分隔]\033[m
    5384. 3.发送文件 \033[32m[传送的包\文件\文件夹路径,空格分隔]\033[m
    5385. 退出: 0\exit\quit
    5386. 帮助: help\h\?
    5387. 注意: 发送文件默认为/tmp下,如已存在同名文件会被强制覆盖,非空目录则中断操作.执行脚本先将本地脚本及包发送远程主机上,发送规则同发送文件
    5388. ''')
    5389. if __name__=='__main__':
    5390. # 定义root账号信息
    5391. root_name = 'root'
    5392. root_pwd = 'peterli'
    5393. user_name='peterli'
    5394. user_pwd='<++(3Ie'
    5395. # 配置文件
    5396. Conf='serverlist.conf'
    5397. if not os.path.isfile(Conf):
    5398. print('\033[33m配置文件 %s 不存在\033[m' %(Conf) )
    5399. sys.exit()
    5400. Help()
    5401. while True:
    5402. i = raw_input("\033[35m[请选择操作]: \033[m").strip()
    5403. q = multiprocessing.Queue()
    5404. r = multiprocessing.Queue()
    5405. if i == '1':
    5406. if user_name == root_name:
    5407. Operation = 'Ssh_Cmd'
    5408. else:
    5409. Operation = 'Ssh_Su_Cmd'
    5410. Cmd = raw_input('CMD: ').strip()
    5411. if len(Cmd) == 0:
    5412. print('\033[33m命令为空\033[m')
    5413. continue
    5414. Concurrent(Conf=Conf,Operation=Operation,user_name=user_name,user_pwd=user_pwd,root_name=root_name,root_pwd=root_pwd,Cmd=Cmd)
    5415. elif i == '2':
    5416. if user_name == root_name:
    5417. Operation = 'Ssh_Script'
    5418. else:
    5419. Operation = 'Ssh_Su_Script'
    5420. PathList = raw_input('\033[36m本地脚本路径: \033[m').strip().split()
    5421. if len(PathList) == 0:
    5422. print('\033[33m路径为空\033[m')
    5423. continue
    5424. if not os.path.isfile(PathList[0]):
    5425. print('\033[33m本地路径 %s 不存在或不是文件\033[m' %(PathList[0]) )
    5426. continue
    5427. for LocalPath in PathList[1:]:
    5428. if not os.path.exists(LocalPath):
    5429. print('\033[33m本地路径 %s 不存在\033[m' %(LocalPath) )
    5430. break
    5431. else:
    5432. Concurrent(Conf=Conf,Operation=Operation,user_name=user_name,user_pwd=user_pwd,root_name=root_name,root_pwd=root_pwd,PathList=PathList)
    5433. elif i == '3':
    5434. Operation = 'Send_File'
    5435. PathList = raw_input('\033[36m本地路径: \033[m').strip().split()
    5436. if len(PathList) == 0:
    5437. print('\033[33m路径为空\033[m')
    5438. continue
    5439. for LocalPath in PathList:
    5440. if not os.path.exists(LocalPath):
    5441. print('\033[33m本地路径 %s 不存在\033[m' %(LocalPath) )
    5442. break
    5443. else:
    5444. Concurrent(Conf=Conf,Operation=Operation,user_name=user_name,user_pwd=user_pwd,root_name=root_name,root_pwd=root_pwd,PathList=PathList)
    5445. elif i == '0' or i == 'exit' or i == 'quit':
    5446. print("\033[34m退出LazyManage脚本\033[m")
    5447. sys.exit()
    5448. elif i == 'help' or i == 'h' or i == '?':
    5449. Help()
    5450. epoll非阻塞长链接
    5451. server
    5452. #!/usr/bin/python
    5453. #-*- coding:utf-8 -*-
    5454. import socket, select, logging, errno
    5455. import os, sys, json
    5456. def cmdRunner(input):
    5457. import commands
    5458. cmd_ret = commands.getstatusoutput(input)
    5459. return json.dumps({'ret':cmd_ret[0], 'out':cmd_ret[1]}, separators=(',', ':'))
    5460. class _State:
    5461. def __init__(self):
    5462. self.state = "read"
    5463. self.have_read = 0
    5464. self.need_read = 10
    5465. self.have_write = 0
    5466. self.need_write = 0
    5467. self.data = ""
    5468. __all__ = ['nbNet']
    5469. class nbNet:
    5470. def __init__(self, host, port, logic):
    5471. self.host = host
    5472. self.port = port
    5473. self.logic = logic
    5474. self.sm = {
    5475. "read":self.aread,
    5476. "write":self.awrite,
    5477. "process":self.aprocess,
    5478. "closing":self.aclose,
    5479. }
    5480. def run(self):
    5481. try:
    5482. self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    5483. except socket.error, msg:
    5484. print("create socket failed")
    5485. try:
    5486. self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    5487. except socket.error, msg:
    5488. print("setsocketopt SO_REUSEADDR failed")
    5489. try:
    5490. self.sock.bind((self.host, self.port))
    5491. except socket.error, msg:
    5492. print("bind failed")
    5493. try:
    5494. self.sock.listen(10)
    5495. except socket.error, msg:
    5496. print(msg)
    5497. try:
    5498. self.epoll_fd = select.epoll()
    5499. # 向 epoll 句柄中注册 新来socket链接,监听可读事件
    5500. self.epoll_fd.register(self.sock.fileno(), select.EPOLLIN )
    5501. except select.error, msg:
    5502. print(msg)
    5503. self.STATE = {}
    5504. while True:
    5505. print self.STATE
    5506. # epoll 等待事件回调收发数据
    5507. epoll_list = self.epoll_fd.poll()
    5508. for fd, events in epoll_list:
    5509. if select.EPOLLHUP & events:
    5510. print 'EPOLLHUP'
    5511. self.STATE[fd][2].state = "closing"
    5512. elif select.EPOLLERR & events:
    5513. print 'EPOLLERR'
    5514. self.STATE[fd][2].state = "closing"
    5515. self.state_machine(fd)
    5516. def state_machine(self, fd):
    5517. if fd == self.sock.fileno():
    5518. print "state_machine fd %s accept" % fd
    5519. # fd与初始监听的fd一致,新创建一个连接
    5520. conn, addr = self.sock.accept()
    5521. # 设置为非阻塞
    5522. conn.setblocking(0)
    5523. self.STATE[conn.fileno()] = [conn, addr, _State()]
    5524. # 将新建立的链接注册在epoll句柄中,监听可读事件,并设置为EPOLLET高速边缘触发,即触发后不会再次触发直到新接收数据
    5525. self.epoll_fd.register(conn.fileno(), select.EPOLLET | select.EPOLLIN )
    5526. else:
    5527. # 否则为历史已存在的fd,调用对应的状态方法
    5528. print "state_machine fd %s %s" % (fd,self.STATE[fd][2].state)
    5529. stat = self.STATE[fd][2].state
    5530. self.sm[stat](fd)
    5531. def aread(self, fd):
    5532. try:
    5533. # 接收当前fd的可读事件中的数据
    5534. one_read = self.STATE[fd][0].recv(self.STATE[fd][2].need_read)
    5535. if len(one_read) == 0:
    5536. # 接收错误改变状态为关闭
    5537. self.STATE[fd][2].state = "closing"
    5538. self.state_machine(fd)
    5539. return
    5540. # 将历史接收的数据叠加
    5541. self.STATE[fd][2].data += one_read
    5542. self.STATE[fd][2].have_read += len(one_read)
    5543. self.STATE[fd][2].need_read -= len(one_read)
    5544. # 接收协议的10个字符
    5545. if self.STATE[fd][2].have_read == 10:
    5546. # 通过10个字符得知下次应该具体接收多少字节,存入状态字典中
    5547. self.STATE[fd][2].need_read += int(self.STATE[fd][2].data)
    5548. self.STATE[fd][2].data = ''
    5549. # 调用状态机重新处理
    5550. self.state_machine(fd)
    5551. elif self.STATE[fd][2].need_read == 0:
    5552. # 当接全部收完毕,改变状态,去执行具体服务
    5553. self.STATE[fd][2].state = 'process'
    5554. self.state_machine(fd)
    5555. except socket.error, msg:
    5556. self.STATE[fd][2].state = "closing"
    5557. print(msg)
    5558. self.state_machine(fd)
    5559. return
    5560. def aprocess(self, fd):
    5561. # 执行具体执行方法 cmdRunner 得到符合传输协议的返回结果
    5562. response = self.logic(self.STATE[fd][2].data)
    5563. self.STATE[fd][2].data = "%010d%s"%(len(response), response)
    5564. self.STATE[fd][2].need_write = len(self.STATE[fd][2].data)
    5565. # 改变为写的状态
    5566. self.STATE[fd][2].state = 'write'
    5567. # 改变监听事件为写
    5568. self.epoll_fd.modify(fd, select.EPOLLET | select.EPOLLOUT)
    5569. self.state_machine(fd)
    5570. def awrite(self, fd):
    5571. try:
    5572. last_have_send = self.STATE[fd][2].have_write
    5573. # 发送返回给客户端的数据
    5574. have_send = self.STATE[fd][0].send(self.STATE[fd][2].data[last_have_send:])
    5575. self.STATE[fd][2].have_write += have_send
    5576. self.STATE[fd][2].need_write -= have_send
    5577. if self.STATE[fd][2].need_write == 0 and self.STATE[fd][2].have_write != 0:
    5578. # 发送完成,重新初始化状态,并将监听写事件改回读事件
    5579. self.STATE[fd][2] = _State()
    5580. self.epoll_fd.modify(fd, select.EPOLLET | select.EPOLLIN)
    5581. except socket.error, msg:
    5582. self.STATE[fd][2].state = "closing"
    5583. self.state_machine(fd)
    5584. print(msg)
    5585. return
    5586. def aclose(self, fd):
    5587. try:
    5588. print 'Error: %s:%d' %(self.STATE[fd][1][0] ,self.STATE[fd][1][1])
    5589. # 取消fd的事件监听
    5590. self.epoll_fd.unregister(fd)
    5591. # 关闭异常链接
    5592. self.STATE[fd][0].close()
    5593. # 删除fd的状态信息
    5594. self.STATE.pop(fd)
    5595. except:
    5596. print 'Close the abnormal'
    5597. if __name__ == "__main__":
    5598. HOST = '0.0.0.0'
    5599. PORT = 50005
    5600. nb = nbNet(HOST, PORT, cmdRunner)
    5601. nb.run()
    5602. client
    5603. #!/usr/bin/env python
    5604. import socket, sys, os
    5605. HOST = '0.0.0.0'
    5606. PORT = 50005
    5607. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    5608. s.connect((HOST, PORT))
    5609. cmd = sys.argv[1]
    5610. while True:
    5611. s.sendall("%010d%s"%(len(cmd), cmd))
    5612. print cmd
    5613. count = s.recv(10)
    5614. if not count:
    5615. print '-----------'
    5616. print count
    5617. sys.exit()
    5618. count = int(count)
    5619. buf = s.recv(count)
    5620. print buf
    5621. 不定期更新下载地址:
    5622. https://github.com/liquanzhou/ops_doc
    5623. 请勿删除信息, 植入广告, 抵制不道德行为