{"id":599,"date":"2019-10-23T14:19:40","date_gmt":"2019-10-23T06:19:40","guid":{"rendered":"https:\/\/www.linuxdevops.cn\/?p=599"},"modified":"2023-04-18T15:57:32","modified_gmt":"2023-04-18T07:57:32","slug":"general-collection-of-python-common-modules-sorting","status":"publish","type":"post","link":"https:\/\/www.linuxdevops.cn\/2019\/10\/general-collection-of-python-common-modules-sorting\/","title":{"rendered":"Python \u5e38\u7528\u6a21\u5757\u5927\u5168\uff08\u6574\u7406\uff09"},"content":{"rendered":"\n
\n<\/em>#os\u6a21\u5757\u5c31\u662f\u5bf9\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u64cd\u4f5c\uff0c\u4f7f\u7528\u8be5\u6a21\u5757\u5fc5\u987b\u5148\u5bfc\u5165\u6a21\u5757\uff1a\nimport os\n\n#getcwd() \u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55(\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u9ed8\u8ba4\u90fd\u662f\u5f53\u524d\u6587\u4ef6\u6240\u5728\u7684\u6587\u4ef6\u5939)\nresult = os.getcwd()\nprint(result)\n\n#chdir()\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\nos.chdir('\/home\/sy')\nresult = os.getcwd()\nprint(result)\n\nopen('02.txt','w')\n\n#\u64cd\u4f5c\u65f6\u5982\u679c\u4e66\u5199\u5b8c\u6574\u7684\u8def\u5f84\u5219\u4e0d\u9700\u8981\u8003\u8651\u9ed8\u8ba4\u5de5\u4f5c\u76ee\u5f55\u7684\u95ee\u9898,\u6309\u7167\u5b9e\u9645\u4e66\u5199\u8def\u5f84\u64cd\u4f5c\nopen('\/home\/sy\/\u4e0b\u8f7d\/02.txt','w')\n\n#listdir() \u83b7\u53d6\u6307\u5b9a\u6587\u4ef6\u5939\u4e2d\u6240\u6709\u5185\u5bb9\u7684\u540d\u79f0\u5217\u8868\nresult = os.listdir('\/home\/sy')\nprint(result)\n\n#mkdir() \u521b\u5efa\u6587\u4ef6\u5939\n#os.mkdir('girls')\n#os.mkdir('boys',0o777)\n\n#makedirs() \u9012\u5f52\u521b\u5efa\u6587\u4ef6\u5939\n#os.makedirs('\/home\/sy\/a\/b\/c\/d')\n\n#rmdir() \u5220\u9664\u7a7a\u76ee\u5f55\n#os.rmdir('girls')\n\n#removedirs \u9012\u5f52\u5220\u9664\u6587\u4ef6\u5939 \u5fc5\u987b\u90fd\u662f\u7a7a\u76ee\u5f55\n#os.removedirs('\/home\/sy\/a\/b\/c\/d')\n\n#rename() \u6587\u4ef6\u6216\u6587\u4ef6\u5939\u91cd\u547d\u540d\n#os.rename('\/home\/sy\/a','\/home\/sy\/alibaba'\n#os.rename('02.txt','002.txt')\n\n#stat() \u83b7\u53d6\u6587\u4ef6\u6216\u8005\u6587\u4ef6\u5939\u7684\u4fe1\u606f\n#result = os.stat('\/home\/sy\/PycharmProject\/Python3\/10.27\/01.py)\n#print(result)\n\n#system() \u6267\u884c\u7cfb\u7edf\u547d\u4ee4(\u5371\u9669\u51fd\u6570)\n#result = os.system('ls -al') #\u83b7\u53d6\u9690\u85cf\u6587\u4ef6\n#print(result)\n\n#\u73af\u5883\u53d8\u91cf\n'''\n\u73af\u5883\u53d8\u91cf\u5c31\u662f\u4e00\u4e9b\u547d\u4ee4\u7684\u96c6\u5408\n\u64cd\u4f5c\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u5c31\u662f\u64cd\u4f5c\u7cfb\u7edf\u5728\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u65f6\u641c\u7d22\u547d\u4ee4\u7684\u76ee\u5f55\u7684\u96c6\u5408\n'''\n#getenv() \u83b7\u53d6\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\nresult = os.getenv('PATH')\nprint(result.split(':'))\n\n#putenv() \u5c06\u4e00\u4e2a\u76ee\u5f55\u6dfb\u52a0\u5230\u73af\u5883\u53d8\u91cf\u4e2d(\u4e34\u65f6\u589e\u52a0\u4ec5\u5bf9\u5f53\u524d\u811a\u672c\u6709\u6548)\n#os.putenv('PATH','\/home\/sy\/\u4e0b\u8f7d')\n#os.system('syls')\n\n#exit() \u9000\u51fa\u7ec8\u7aef\u7684\u547d\u4ee4\n\n#os\u6a21\u5757\u4e2d\u7684\u5e38\u7528\u503c\n#curdir \u8868\u793a\u5f53\u524d\u6587\u4ef6\u5939 .\u8868\u793a\u5f53\u524d\u6587\u4ef6\u5939 \u4e00\u822c\u60c5\u51b5\u4e0b\u53ef\u4ee5\u7701\u7565\nprint(os.curdir)\n\n#pardir \u8868\u793a\u4e0a\u4e00\u5c42\u6587\u4ef6\u5939 ..\u8868\u793a\u4e0a\u4e00\u5c42\u6587\u4ef6\u5939 \u4e0d\u53ef\u7701\u7565!\nprint(os.pardir)\n\n#os.mkdir('..\/..\/..\/man')#\u76f8\u5bf9\u8def\u5f84 \u4ece\u5f53\u524d\u76ee\u5f55\u5f00\u59cb\u67e5\u627e\n#os.mkdir('\/home\/sy\/man1')#\u7edd\u5bf9\u8def\u5f84 \u4ece\u6839\u76ee\u5f55\u5f00\u59cb\u67e5\u627e\n\n#name \u83b7\u53d6\u4ee3\u8868\u64cd\u4f5c\u7cfb\u7edf\u7684\u540d\u79f0\u5b57\u7b26\u4e32\nprint(os.name) #posix -> linux\u6216\u8005unix\u7cfb\u7edf nt -> window\u7cfb\u7edf\n\n#sep \u83b7\u53d6\u7cfb\u7edf\u8def\u5f84\u95f4\u9694\u7b26\u53f7 window ->\\ linux ->\/\nprint(os.sep)\n\n#extsep \u83b7\u53d6\u6587\u4ef6\u540d\u79f0\u548c\u540e\u7f00\u4e4b\u95f4\u7684\u95f4\u9694\u7b26\u53f7 window & linux -> .\nprint(os.extsep)\n\n#linesep \u83b7\u53d6\u64cd\u4f5c\u7cfb\u7edf\u7684\u6362\u884c\u7b26\u53f7 window -> \\r\\n linux\/unix -> \\n\nprint(repr(os.linesep))\n\n#\u4ee5\u4e0b\u5185\u5bb9\u90fd\u662fos.path\u5b50\u6a21\u5757\u4e2d\u7684\u5185\u5bb9\n\n#abspath() \u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u5316\u4e3a\u7edd\u5bf9\u8def\u5f84\npath = '.\/boys'#\u76f8\u5bf9\nresult = os.path.abspath(path)\nprint(result)\n\n#dirname() \u83b7\u53d6\u5b8c\u6574\u8def\u5f84\u5f53\u4e2d\u7684\u76ee\u5f55\u90e8\u5206 & basename()\u83b7\u53d6\u5b8c\u6574\u8def\u5f84\u5f53\u4e2d\u7684\u4e3b\u4f53\u90e8\u5206\npath = '\/home\/sy\/boys'\nresult = os.path.dirname(path)\nprint(result)\n\nresult = os.path.basename(path)\nprint(result)\n\n#split() \u5c06\u4e00\u4e2a\u5b8c\u6574\u7684\u8def\u5f84\u5207\u5272\u6210\u76ee\u5f55\u90e8\u5206\u548c\u4e3b\u4f53\u90e8\u5206\npath = '\/home\/sy\/boys'\nresult = os.path.split(path)\nprint(result)\n\n#join() \u5c062\u4e2a\u8def\u5f84\u5408\u5e76\u6210\u4e00\u4e2a\nvar1 = '\/home\/sy'\nvar2 = '000.py'\nresult = os.path.join(var1,var2)\nprint(result)\n\n#splitext() \u5c06\u4e00\u4e2a\u8def\u5f84\u5207\u5272\u6210\u6587\u4ef6\u540e\u7f00\u548c\u5176\u4ed6\u4e24\u4e2a\u90e8\u5206,\u4e3b\u8981\u7528\u4e8e\u83b7\u53d6\u6587\u4ef6\u7684\u540e\u7f00\npath = '\/home\/sy\/000.py'\nresult = os.path.splitext(path)\nprint(result)\n\n#getsize() \u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f\n#path = '\/home\/sy\/000.py'\n#result = os.path.getsize(path)\n#print(result)\n\n#isfile() \u68c0\u6d4b\u662f\u5426\u662f\u6587\u4ef6\npath = '\/home\/sy\/000.py'\nresult = os.path.isfile(path)\nprint(result)\n\n#isdir() \u68c0\u6d4b\u662f\u5426\u662f\u6587\u4ef6\u5939\nresult = os.path.isdir(path)\nprint(result)\n\n#islink() \u68c0\u6d4b\u662f\u5426\u662f\u94fe\u63a5\npath = '\/initrd.img.old'\nresult = os.path.islink(path)\nprint(result)\n\n#getctime() \u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4 get create time\n#getmtime() \u83b7\u53d6\u6587\u4ef6\u7684\u4fee\u6539\u65f6\u95f4 get modify time\n#getatime() \u83b7\u53d6\u6587\u4ef6\u7684\u8bbf\u95ee\u65f6\u95f4 get active time\n\nimport time\n\nfilepath = '\/home\/sy\/\u4e0b\u8f7d\/chls'\n\nresult = os.path.getctime(filepath)\nprint(time.ctime(result))\n\nresult = os.path.getmtime(filepath)\nprint(time.ctime(result))\n\nresult = os.path.getatime(filepath)\nprint(time.ctime(result))\n\n#exists() \u68c0\u6d4b\u67d0\u4e2a\u8def\u5f84\u662f\u5426\u771f\u5b9e\u5b58\u5728\nfilepath = '\/home\/sy\/\u4e0b\u8f7d\/chls'\nresult = os.path.exists(filepath)\nprint(result)\n\n#isabs() \u68c0\u6d4b\u4e00\u4e2a\u8def\u5f84\u662f\u5426\u662f\u7edd\u5bf9\u8def\u5f84\npath = '\/boys'\nresult = os.path.isabs(path)\nprint(result)\n\n#samefile() \u68c0\u6d4b2\u4e2a\u8def\u5f84\u662f\u5426\u662f\u540c\u4e00\u4e2a\u6587\u4ef6\npath1 = '\/home\/sy\/\u4e0b\u8f7d\/001'\npath2 = '..\/..\/..\/\u4e0b\u8f7d\/001'\nresult = os.path.samefile(path1,path2)\nprint(result)\n\n#os.environ \u7528\u4e8e\u83b7\u53d6\u548c\u8bbe\u7f6e\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u7684\u5185\u7f6e\u503c\nimport os\n#\u83b7\u53d6\u7cfb\u7edf\u73af\u5883\u53d8\u91cf getenv() \u6548\u679c\nprint(os.environ['PATH'])\n\n#\u8bbe\u7f6e\u7cfb\u7edf\u73af\u5883\u53d8\u91cf putenv()\nos.environ['PATH'] += ':\/home\/sy\/\u4e0b\u8f7d'\nos.system('chls')<\/pre>\n\n\n\nos \u5e38\u7528\u65b9\u6cd5<\/h4>\n\n\n\n
- os.remove(\u2018path\/filename\u2019) \u5220\u9664\u6587\u4ef6<\/li>
- os.rename(oldname, newname) \u91cd\u547d\u540d\u6587\u4ef6<\/li>
- os.walk() \u751f\u6210\u76ee\u5f55\u6811\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u540d<\/li>
- os.chdir('dirname') \u6539\u53d8\u76ee\u5f55<\/li>
- os.mkdir\/makedirs('dirname')\u521b\u5efa\u76ee\u5f55\/\u591a\u5c42\u76ee\u5f55<\/li>
- os.rmdir\/removedirs('dirname') \u5220\u9664\u76ee\u5f55\/\u591a\u5c42\u76ee\u5f55<\/li>
- os.listdir('dirname') \u5217\u51fa\u6307\u5b9a\u76ee\u5f55\u7684\u6587\u4ef6<\/li>
- os.getcwd() \u53d6\u5f97\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/li>
- os.chmod() \u6539\u53d8\u76ee\u5f55\u6743\u9650<\/li>
- os.path.basename(\u2018path\/filename\u2019) \u53bb\u6389\u76ee\u5f55\u8def\u5f84\uff0c\u8fd4\u56de\u6587\u4ef6\u540d<\/li>
- os.path.dirname(\u2018path\/filename\u2019) \u53bb\u6389\u6587\u4ef6\u540d\uff0c\u8fd4\u56de\u76ee\u5f55\u8def\u5f84<\/li>
- os.path.join(path1[,path2[,...]]) \u5c06\u5206\u79bb\u7684\u5404\u90e8\u5206\u7ec4\u5408\u6210\u4e00\u4e2a\u8def\u5f84\u540d<\/li>
- os.path.split('path') \u8fd4\u56de( dirname(), basename())\u5143\u7ec4<\/li>
- os.path.splitext() \u8fd4\u56de (filename, extension) \u5143\u7ec4<\/li>
- os.path.getatime\\ctime\\mtime \u5206\u522b\u8fd4\u56de\u6700\u8fd1\u8bbf\u95ee\u3001\u521b\u5efa\u3001\u4fee\u6539\u65f6\u95f4<\/li>
- os.path.getsize() \u8fd4\u56de\u6587\u4ef6\u5927\u5c0f<\/li>
- os.path.exists() \u662f\u5426\u5b58\u5728<\/li>
- os.path.isabs() \u662f\u5426\u4e3a\u7edd\u5bf9\u8def\u5f84<\/li>
- os.path.isdir() \u662f\u5426\u4e3a\u76ee\u5f55<\/li>
- os.path.isfile() \u662f\u5426\u4e3a\u6587\u4ef6<\/li><\/ul>\n\n\n\n
sys \u6a21\u5757
<\/strong><\/h2>\n\n\n\n
- sys.argv \u547d\u4ee4\u884c\u53c2\u6570List\uff0c\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u7a0b\u5e8f\u672c\u8eab\u8def\u5f84<\/li>
- sys.modules.keys() \u8fd4\u56de\u6240\u6709\u5df2\u7ecf\u5bfc\u5165\u7684\u6a21\u5757\u5217\u8868<\/li>
- sys.exc_info() \u83b7\u53d6\u5f53\u524d\u6b63\u5728\u5904\u7406\u7684\u5f02\u5e38\u7c7b,exc_type\u3001exc_value\u3001exc_traceback\u5f53\u524d\u5904\u7406\u7684\u5f02\u5e38\u8be6\u7ec6\u4fe1\u606f<\/li>
- sys.exit(n) \u9000\u51fa\u7a0b\u5e8f\uff0c\u6b63\u5e38\u9000\u51fa\u65f6exit(0)<\/li>
- sys.hexversion \u83b7\u53d6Python\u89e3\u91ca\u7a0b\u5e8f\u7684\u7248\u672c\u503c\uff0c16\u8fdb\u5236\u683c\u5f0f\u5982\uff1a0x020403F0<\/li>
- sys.version \u83b7\u53d6Python\u89e3\u91ca\u7a0b\u5e8f\u7684\u7248\u672c\u4fe1\u606f<\/li>
- sys.maxint \u6700\u5927\u7684Int\u503c<\/li>
- sys.maxunicode \u6700\u5927\u7684Unicode\u503c<\/li>
- sys.modules \u8fd4\u56de\u7cfb\u7edf\u5bfc\u5165\u7684\u6a21\u5757\u5b57\u6bb5\uff0ckey\u662f\u6a21\u5757\u540d\uff0cvalue\u662f\u6a21\u5757<\/li>
- sys.path \u8fd4\u56de\u6a21\u5757\u7684\u641c\u7d22\u8def\u5f84\uff0c\u521d\u59cb\u5316\u65f6\u4f7f\u7528PYTHONPATH\u73af\u5883\u53d8\u91cf\u7684\u503c<\/li>
- sys.platform \u8fd4\u56de\u64cd\u4f5c\u7cfb\u7edf\u5e73\u53f0\u540d\u79f0<\/li>
- sys.stdout \u6807\u51c6\u8f93\u51fa<\/li>
- sys.stdin \u6807\u51c6\u8f93\u5165<\/li>
- sys.stderr \u9519\u8bef\u8f93\u51fa<\/li>
- sys.exc_clear() \u7528\u6765\u6e05\u9664\u5f53\u524d\u7ebf\u7a0b\u6240\u51fa\u73b0\u7684\u5f53\u524d\u7684\u6216\u6700\u8fd1\u7684\u9519\u8bef\u4fe1\u606f<\/li>
- sys.exec_prefix \u8fd4\u56de\u5e73\u53f0\u72ec\u7acb\u7684python\u6587\u4ef6\u5b89\u88c5\u7684\u4f4d\u7f6e<\/li>
- sys.byteorder \u672c\u5730\u5b57\u8282\u89c4\u5219\u7684\u6307\u793a\u5668\uff0cbig-endian\u5e73\u53f0\u7684\u503c\u662f'big',little-endian\u5e73\u53f0\u7684\u503c\u662f'little'<\/li>
- sys.copyright \u8bb0\u5f55python\u7248\u6743\u76f8\u5173\u7684\u4e1c\u897f<\/li>
- sys.api_version \u89e3\u91ca\u5668\u7684C\u7684API\u7248\u672c<\/li><\/ul>\n\n\n\n
sys.stdin,sys.stdout,sys.stderr,stdin , stdout , \u4ee5\u53castderr \u53d8\u91cf\u5305\u542b\u4e0e\u6807\u51c6I\/O \u6d41\u5bf9\u5e94\u7684\u6d41\u5bf9\u8c61. \u5982\u679c\u9700\u8981\u66f4\u597d\u5730\u63a7\u5236\u8f93\u51fa,\u800cprint \u4e0d\u80fd\u6ee1\u8db3\u4f60\u7684\u8981\u6c42, \u5b83\u4eec\u5c31\u662f\u4f60\u6240\u9700\u8981\u7684. \u4f60\u4e5f\u53ef\u4ee5\u66ff\u6362\u5b83\u4eec, \u8fd9\u65f6\u5019\u4f60\u5c31\u53ef\u4ee5\u91cd\u5b9a\u5411\u8f93\u51fa\u548c\u8f93\u5165\u5230\u5176\u5b83\u8bbe\u5907( device ), \u6216\u8005\u4ee5\u975e\u6807\u51c6\u7684\u65b9\u5f0f\u5904\u7406\u5b83\u4eec
\u6211\u4eec\u5e38\u7528print\u548craw_input\u6765\u8fdb\u884c\u8f93\u5165\u548c\u6253\u5370\uff0c\u90a3\u4e48<\/code>print \u548c raw_input\u662f\u5982\u4f55\u4e0e\u6807\u51c6\u8f93\u5165\/\u8f93\u51fa\u6d41\u5efa\u7acb\u5173\u7cfb\u7684\u5462\uff1f
\u5176\u5b9ePython\u7a0b\u5e8f\u7684\u6807\u51c6\u8f93\u5165\/\u8f93\u51fa\/\u51fa\u9519\u6d41\u5b9a\u4e49\u5728sys\u6a21\u5757\u4e2d\uff0c\u5206\u522b \u4e3a\uff1a sys.stdin,sys.stdout, sys.stderr
\u4e0b\u5217\u7684\u7a0b\u5e8f\u4e5f\u53ef\u4ee5\u7528\u6765\u8f93\u5165\u548c\u8f93\u51fa\u662f\u4e00\u6837\u7684\uff1a
<\/p><\/blockquote>\n\n\n\n<\/p>\n\n\n\n
sys.stdout.write('HelloWorld!')<\/code><\/p>\n\n\n\n
print 'Please enter yourname:',<\/code>
name=sys.stdin.readline()[:-1]<\/code>
print 'Hi, %s!' % name<\/code><\/p>\n\n\n\n
\u90a3\u4e48sys.stdin, sys.stdout, stderr\u5230\u5e95\u662f\u4ec0\u4e48\u5462\uff1f\u6211\u4eec\u5728Python\u8fd0\u884c\u73af\u5883\u4e2d\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n\n\n\n
import sys<\/code>
for f in (sys.stdin,sys.stdout, sys.stderr): print f<\/code>
\u8f93\u51fa\u4e3a\uff1a<\/code>
<open file'<stdin>', mode 'r' at 892210><\/code>
<open file'<stdout>', mode 'w' at 892270><\/code>
<open file'<stderr>', mode 'w at 8922d0><\/code><\/p>\n\n\n\n
\u7531\u6b64\u53ef\u4ee5\u770b\u51fastdin, stdout, stderr\u5728Python\u4e2d\u65e0\u975e\u90fd\u662f\u6587\u4ef6\u5c5e\u6027\u7684\u5bf9\u8c61\uff0c\u4ed6\u4eec\u5728Python\u542f\u52a8\u65f6\u81ea\u52a8\u4e0eShell \u73af\u5883\u4e2d\u7684\u6807\u51c6\u8f93\u5165\uff0c\u8f93\u51fa\uff0c\u51fa\u9519\u5173\u8054\u3002
\u800cPython\u7a0b\u5e8f\u7684\u5728Shell\u4e2d\u7684I\/O\u91cd\u5b9a\u5411\u4e0e\u672c\u6587\u5f00\u59cb\u65f6\u4e3e\u7684DOS\u547d\u4ee4\u7684\u91cd\u5b9a\u5411\u5b8c\u5168\u76f8\u540c\uff0c\u5176\u5b9e\u8fd9\u79cd\u91cd\u5b9a\u5411\u662f\u7531Shell\u6765\u63d0\u4f9b\u7684\uff0c\u4e0ePython \u672c\u8eab\u5e76\u65e0\u5173\u7cfb\u3002\u90a3\u4e48\u6211\u4eec\u662f\u5426\u53ef\u4ee5\u5728Python\u7a0b\u5e8f\u5185\u90e8\u5c06stdin,stdout,stderr\u8bfb\u5199\u64cd\u4f5c\u91cd\u5b9a\u5411\u5230\u4e00\u4e2a\u5185\u90e8\u5bf9\u8c61\u5462\uff1f\u7b54\u6848\u662f\u80af\u5b9a\u7684\u3002
Python\u63d0\u4f9b\u4e86\u4e00\u4e2aStringIO\u6a21\u5757\u6765\u5b8c\u6210\u8fd9\u4e2a\u8bbe\u60f3\uff0c\u6bd4\u5982\uff1a
from StringIO import StringIO
import sys
buff =StringIO()<\/p>\n\n\n\ntemp =sys.stdout #\u4fdd\u5b58\u6807\u51c6I\/O\u6d41
sys.stdout =buff #\u5c06\u6807\u51c6I\/O\u6d41\u91cd\u5b9a\u5411\u5230buff\u5bf9\u8c61
print 42, 'hello', 0.001<\/p>\n\n\n\nsys.stdout=temp #\u6062\u590d\u6807\u51c6I\/O\u6d41
print buff.getvalue() <\/p>\n\n\n\n\n\ntime\u6a21\u5757<\/h2>\n\n\n\n
time\u6a21\u5757\u4e2d\u65f6\u95f4\u8868\u73b0\u7684\u683c\u5f0f\u4e3b\u8981\u6709\u4e09\u79cd\uff1a<\/p>\n\n\n\n
\u3000\u3000a\u3001timestamp\u65f6\u95f4\u6233\uff0c\u65f6\u95f4\u6233\u8868\u793a\u7684\u662f\u4ece1970\u5e741\u67081\u65e500:00:00\u5f00\u59cb\u6309\u79d2\u8ba1\u7b97\u7684\u504f\u79fb\u91cf<\/p>\n\n\n\n
\u3000\u3000b\u3001struct_time\u65f6\u95f4\u5143\u7ec4\uff0c\u5171\u6709\u4e5d\u4e2a\u5143\u7d20\u7ec4\u3002<\/p>\n\n\n\n
\u3000\u3000c\u3001format time \u683c\u5f0f\u5316\u65f6\u95f4\uff0c\u5df2\u683c\u5f0f\u5316\u7684\u7ed3\u6784\u4f7f\u65f6\u95f4\u66f4\u5177\u53ef\u8bfb\u6027\u3002\u5305\u62ec\u81ea\u5b9a\u4e49\u683c\u5f0f\u548c\u56fa\u5b9a\u683c\u5f0f\u3002<\/p>\n\n\n\n
\u4e3b\u8981time\u751f\u6210\u65b9\u6cd5\u548ctime\u683c\u5f0f\u8f6c\u6362\u65b9\u6cd5\u5b9e\u4f8b\uff1a<\/strong><\/h3>\n\n\n\n
#! \/usr\/bin\/env python\n# -*- coding:utf-8 -*-\n# __author__ = \"TKQ\"\n\nimport time\n\n# \u751f\u6210timestamp<\/strong>\ntime.time()\n# 1477471508.05
#struct_time to timestamp
time.mktime(time.localtime())<\/pre>\n\n\n\n#\u751f\u6210struct_time<\/strong>\n# timestamp to struct_time \u672c\u5730\u65f6\u95f4\ntime.localtime()\ntime.localtime(time.time())\n# time.struct_time(tm_year=2016, tm_mon=10, tm_mday=26, tm_hour=16, tm_min=45, tm_sec=8, tm_wday=2, tm_yday=300, tm_isdst=0)\n\n# timestamp to struct_time \u683c\u6797\u5a01\u6cbb\u65f6\u95f4\ntime.gmtime()\ntime.gmtime(time.time())\n# time.struct_time(tm_year=2016, tm_mon=10, tm_mday=26, tm_hour=8, tm_min=45, tm_sec=8, tm_wday=2, tm_yday=300, tm_isdst=0)\n\n#format_time to struct_time\ntime.strptime('2011-05-05 16:37:06', '%Y-%m-%d %X')\n# time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6, tm_wday=3, tm_yday=125, tm_isdst=-1)\n\n#\u751f\u6210format_time<\/strong>\n#struct_time to format_time\ntime.strftime(\"%Y-%m-%d %X\")\ntime.strftime(\"%Y-%m-%d %X\",time.localtime())\n# 2016-10-26 16:48:41\n\n\n#\u751f\u6210\u56fa\u5b9a\u683c\u5f0f\u7684\u65f6\u95f4\u8868\u793a\u683c\u5f0f<\/strong>\ntime.asctime(time.localtime())\ntime.ctime(time.time())\n# Wed Oct 26 16:45:08 2016<\/pre>\n\n\n\nstruct_time\u5143\u7ec4\u5143\u7d20\u7ed3\u6784<\/strong><\/h3>\n\n\n\n
\u5c5e\u6027 \u503c\ntm_year\uff08\u5e74\uff09 \u6bd4\u59822011 \ntm_mon\uff08\u6708\uff09 1 - 12\ntm_mday\uff08\u65e5\uff09 1 - 31\ntm_hour\uff08\u65f6\uff09 0 - 23\ntm_min\uff08\u5206\uff09 0 - 59\ntm_sec\uff08\u79d2\uff09 0 - 61\ntm_wday\uff08weekday\uff09 0 - 6\uff080\u8868\u793a\u5468\u65e5\uff09\ntm_yday\uff08\u4e00\u5e74\u4e2d\u7684\u7b2c\u51e0\u5929\uff09 1 - 366\ntm_isdst\uff08\u662f\u5426\u662f\u590f\u4ee4\u65f6\uff09 \u9ed8\u8ba4\u4e3a-1<\/pre>\n\n\n\nformat time\u7ed3\u6784\u5316\u8868\u793a<\/strong><\/h3>\n\n\n\n
\u683c\u5f0f<\/strong><\/td> \u542b\u4e49<\/strong><\/td><\/tr> %a<\/td> \u672c\u5730\uff08locale\uff09\u7b80\u5316\u661f\u671f\u540d\u79f0<\/td><\/tr> %A<\/td> \u672c\u5730\u5b8c\u6574\u661f\u671f\u540d\u79f0<\/td><\/tr> %b<\/td> \u672c\u5730\u7b80\u5316\u6708\u4efd\u540d\u79f0<\/td><\/tr> %B<\/td> \u672c\u5730\u5b8c\u6574\u6708\u4efd\u540d\u79f0<\/td><\/tr> %c<\/td> \u672c\u5730\u76f8\u5e94\u7684\u65e5\u671f\u548c\u65f6\u95f4\u8868\u793a<\/td><\/tr> %d<\/td> \u4e00\u4e2a\u6708\u4e2d\u7684\u7b2c\u51e0\u5929\uff0801 - 31\uff09<\/td><\/tr> %H<\/td> \u4e00\u5929\u4e2d\u7684\u7b2c\u51e0\u4e2a\u5c0f\u65f6\uff0824\u5c0f\u65f6\u5236\uff0c00 - 23\uff09<\/td><\/tr> %I<\/td> \u7b2c\u51e0\u4e2a\u5c0f\u65f6\uff0812\u5c0f\u65f6\u5236\uff0c01 - 12\uff09<\/td><\/tr> %j<\/td> \u4e00\u5e74\u4e2d\u7684\u7b2c\u51e0\u5929\uff08001 - 366\uff09<\/td><\/tr> %m<\/td> \u6708\u4efd\uff0801 - 12\uff09<\/td><\/tr> %M<\/td> \u5206\u949f\u6570\uff0800 - 59\uff09<\/td><\/tr> %p<\/td> \u672c\u5730am\u6216\u8005pm\u7684\u76f8\u5e94\u7b26<\/td><\/tr> %S<\/td> \u79d2\uff0801 - 61\uff09<\/td><\/tr> %U<\/td> \u4e00\u5e74\u4e2d\u7684\u661f\u671f\u6570\u3002\uff0800 - 53\u661f\u671f\u5929\u662f\u4e00\u4e2a\u661f\u671f\u7684\u5f00\u59cb\u3002\uff09\u7b2c\u4e00\u4e2a\u661f\u671f\u5929\u4e4b\u524d\u7684\u6240\u6709\u5929\u6570\u90fd\u653e\u5728\u7b2c0\u5468\u3002<\/td><\/tr> %w<\/td> \u4e00\u4e2a\u661f\u671f\u4e2d\u7684\u7b2c\u51e0\u5929\uff080 - 6\uff0c0\u662f\u661f\u671f\u5929\uff09<\/td><\/tr> %W<\/td> \u548c%U\u57fa\u672c\u76f8\u540c\uff0c\u4e0d\u540c\u7684\u662f%W\u4ee5\u661f\u671f\u4e00\u4e3a\u4e00\u4e2a\u661f\u671f\u7684\u5f00\u59cb\u3002<\/td><\/tr> %x<\/td> \u672c\u5730\u76f8\u5e94\u65e5\u671f<\/td><\/tr> %X<\/td> \u672c\u5730\u76f8\u5e94\u65f6\u95f4<\/td><\/tr> %y<\/td> \u53bb\u6389\u4e16\u7eaa\u7684\u5e74\u4efd\uff0800 - 99\uff09<\/td><\/tr> %Y<\/td> \u5b8c\u6574\u7684\u5e74\u4efd<\/td><\/tr> %Z<\/td> \u65f6\u533a\u7684\u540d\u5b57\uff08\u5982\u679c\u4e0d\u5b58\u5728\u4e3a\u7a7a\u5b57\u7b26\uff09<\/td><\/tr> %%<\/td> \u2018%\u2019\u5b57\u7b26<\/td><\/tr><\/tbody><\/table>\n\n\n\n \u5e38\u89c1\u7ed3\u6784\u5316\u65f6\u95f4\u7ec4\u5408\uff1a<\/p>\n\n\n\n
print time.strftime(\"%Y-%m-%d %X\")\n#2016-10-26 20:50:13<\/pre>\n\n\n\ntime\u52a0\u51cf<\/h3>\n\n\n\n
#timestamp\u52a0\u51cf\u5355\u4f4d\u4ee5\u79d2\u4e3a\u5355\u4f4d\nimport time\nt1 = time.time()\nt2=t1+10\n\nprint time.ctime(t1)#Wed Oct 26 21:15:30 2016\nprint time.ctime(t2)#Wed Oct 26 21:15:40 2016<\/pre>\n\n\n\ndatetime\u6a21\u5757<\/h2>\n\n\n\n
datatime\u6a21\u5757\u91cd\u65b0\u5c01\u88c5\u4e86time\u6a21\u5757\uff0c\u63d0\u4f9b\u66f4\u591a\u63a5\u53e3\uff0c\u63d0\u4f9b\u7684\u7c7b\u6709\uff1adate,time,datetime,timedelta,tzinfo\u3002<\/p>\n\n\n\n
date\u7c7b<\/strong><\/h3>\n\n\n\n
datetime.date(year, month, day)<\/p>\n\n\n\n
\u9759\u6001\u65b9\u6cd5\u548c\u5b57\u6bb5<\/p>\n\n\n\n
date.max\u3001date.min\uff1adate\u5bf9\u8c61\u6240\u80fd\u8868\u793a\u7684\u6700\u5927\u3001\u6700\u5c0f\u65e5\u671f\uff1b\ndate.resolution\uff1adate\u5bf9\u8c61\u8868\u793a\u65e5\u671f\u7684\u6700\u5c0f\u5355\u4f4d\u3002\u8fd9\u91cc\u662f\u5929\u3002\ndate.today()\uff1a\u8fd4\u56de\u4e00\u4e2a\u8868\u793a\u5f53\u524d\u672c\u5730\u65e5\u671f\u7684date\u5bf9\u8c61\uff1b\ndate.fromtimestamp(timestamp)\uff1a\u6839\u636e\u7ed9\u5b9a\u7684\u65f6\u95f4\u622e\uff0c\u8fd4\u56de\u4e00\u4e2adate\u5bf9\u8c61\uff1b<\/pre>\n\n\n\n\u65b9\u6cd5\u548c\u5c5e\u6027<\/p>\n\n\n\n
d1 = date(2011,06,03)#date\u5bf9\u8c61\nd1.year\u3001date.month\u3001date.day\uff1a\u5e74\u3001\u6708\u3001\u65e5\uff1b\nd1.replace(year, month, day)\uff1a\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u65e5\u671f\u5bf9\u8c61\uff0c\u7528\u53c2\u6570\u6307\u5b9a\u7684\u5e74\uff0c\u6708\uff0c\u65e5\u4ee3\u66ff\u539f\u6709\u5bf9\u8c61\u4e2d\u7684\u5c5e\u6027\u3002\uff08\u539f\u6709\u5bf9\u8c61\u4ecd\u4fdd\u6301\u4e0d\u53d8\uff09\nd1.timetuple()\uff1a\u8fd4\u56de\u65e5\u671f\u5bf9\u5e94\u7684time.struct_time\u5bf9\u8c61\uff1b\nd1.weekday()\uff1a\u8fd4\u56deweekday\uff0c\u5982\u679c\u662f\u661f\u671f\u4e00\uff0c\u8fd4\u56de0\uff1b\u5982\u679c\u662f\u661f\u671f2\uff0c\u8fd4\u56de1\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff1b\nd1.isoweekday()\uff1a\u8fd4\u56deweekday\uff0c\u5982\u679c\u662f\u661f\u671f\u4e00\uff0c\u8fd4\u56de1\uff1b\u5982\u679c\u662f\u661f\u671f2\uff0c\u8fd4\u56de2\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff1b\nd1.isocalendar()\uff1a\u8fd4\u56de\u683c\u5f0f\u5982(year\uff0cmonth\uff0cday)\u7684\u5143\u7ec4\uff1b\nd1.isoformat()\uff1a\u8fd4\u56de\u683c\u5f0f\u5982'YYYY-MM-DD\u2019\u7684\u5b57\u7b26\u4e32\uff1b\nd1.strftime(fmt)\uff1a\u548ctime\u6a21\u5757format\u76f8\u540c\u3002<\/pre>\n\n\n\ntime\u7c7b<\/strong><\/h3>\n\n\n\n
datetime.time(hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] ) <\/p>\n\n\n\n
\u9759\u6001\u65b9\u6cd5\u548c\u5b57\u6bb5<\/p>\n\n\n\n
time.min\u3001time.max\uff1atime\u7c7b\u6240\u80fd\u8868\u793a\u7684\u6700\u5c0f\u3001\u6700\u5927\u65f6\u95f4\u3002\u5176\u4e2d\uff0ctime.min = time(0, 0, 0, 0)\uff0c time.max = time(23, 59, 59, 999999)\uff1b\ntime.resolution\uff1a\u65f6\u95f4\u7684\u6700\u5c0f\u5355\u4f4d\uff0c\u8fd9\u91cc\u662f1\u5fae\u79d2\uff1b<\/pre>\n\n\n\n\u65b9\u6cd5\u548c\u5c5e\u6027<\/p>\n\n\n\n
t1 = datetime.time(10,23,15)#time\u5bf9\u8c61
t1.hour\u3001t1.minute\u3001t1.second\u3001t1.microsecond\uff1a\u65f6\u3001\u5206\u3001\u79d2\u3001\u5fae\u79d2\uff1b\nt1.tzinfo\uff1a\u65f6\u533a\u4fe1\u606f\uff1b\nt1.replace([ hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] ] )\uff1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u65f6\u95f4\u5bf9\u8c61\uff0c\u7528\u53c2\u6570\u6307\u5b9a\u7684\u65f6\u3001\u5206\u3001\u79d2\u3001\u5fae\u79d2\u4ee3\u66ff\u539f\u6709\u5bf9\u8c61\u4e2d\u7684\u5c5e\u6027\uff08\u539f\u6709\u5bf9\u8c61\u4ecd\u4fdd\u6301\u4e0d\u53d8\uff09\uff1b\nt1.isoformat()\uff1a\u8fd4\u56de\u578b\u5982\"HH:MM:SS\"\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff1b\nt1.strftime(fmt)\uff1a\u540ctime\u6a21\u5757\u4e2d\u7684format\uff1b<\/pre>\n\n\n\ndatetime\u7c7b<\/strong><\/h3>\n\n\n\n
datetime\u76f8\u5f53\u4e8edate\u548ctime\u7ed3\u5408\u8d77\u6765\u3002
datetime.datetime (year, month, day[ , hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] ] )<\/p>\n\n\n\n\u9759\u6001\u65b9\u6cd5\u548c\u5b57\u6bb5<\/p>\n\n\n\n
datetime.today()\uff1a\u8fd4\u56de\u4e00\u4e2a\u8868\u793a\u5f53\u524d\u672c\u5730\u65f6\u95f4\u7684datetime\u5bf9\u8c61\uff1b\ndatetime.now([tz])\uff1a\u8fd4\u56de\u4e00\u4e2a\u8868\u793a\u5f53\u524d\u672c\u5730\u65f6\u95f4\u7684datetime\u5bf9\u8c61\uff0c\u5982\u679c\u63d0\u4f9b\u4e86\u53c2\u6570tz\uff0c\u5219\u83b7\u53d6tz\u53c2\u6570\u6240\u6307\u65f6\u533a\u7684\u672c\u5730\u65f6\u95f4\uff1b\ndatetime.utcnow()\uff1a\u8fd4\u56de\u4e00\u4e2a\u5f53\u524dutc\u65f6\u95f4\u7684datetime\u5bf9\u8c61\uff1b#\u683c\u6797\u5a01\u6cbb\u65f6\u95f4\ndatetime.fromtimestamp(timestamp[, tz])\uff1a\u6839\u636e\u65f6\u95f4\u622e\u521b\u5efa\u4e00\u4e2adatetime\u5bf9\u8c61\uff0c\u53c2\u6570tz\u6307\u5b9a\u65f6\u533a\u4fe1\u606f\uff1b\ndatetime.utcfromtimestamp(timestamp)\uff1a\u6839\u636e\u65f6\u95f4\u622e\u521b\u5efa\u4e00\u4e2adatetime\u5bf9\u8c61\uff1b\ndatetime.combine(date, time)\uff1a\u6839\u636edate\u548ctime\uff0c\u521b\u5efa\u4e00\u4e2adatetime\u5bf9\u8c61\uff1b\ndatetime.strptime(date_string, format)\uff1a\u5c06\u683c\u5f0f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\uff1b<\/pre>\n\n\n\n\u65b9\u6cd5\u548c\u5c5e\u6027<\/p>\n\n\n\n
dt=datetime.now()#datetime\u5bf9\u8c61\ndt.year\u3001month\u3001day\u3001hour\u3001minute\u3001second\u3001microsecond\u3001tzinfo\uff1a\ndt.date()\uff1a\u83b7\u53d6date\u5bf9\u8c61\uff1b\ndt.time()\uff1a\u83b7\u53d6time\u5bf9\u8c61\uff1b\ndt. replace ([ year[ , month[ , day[ , hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] ] ] ] ])\uff1a\ndt. timetuple ()\ndt. utctimetuple ()\ndt. toordinal ()\ndt. weekday ()\ndt. isocalendar ()\ndt. isoformat ([ sep] )\ndt. ctime ()\uff1a\u8fd4\u56de\u4e00\u4e2a\u65e5\u671f\u65f6\u95f4\u7684C\u683c\u5f0f\u5b57\u7b26\u4e32\uff0c\u7b49\u6548\u4e8etime.ctime(time.mktime(dt.timetuple()))\uff1b\ndt. strftime (format)<\/pre>\n\n\n\ntimedelta\u7c7b\uff0c\u65f6\u95f4\u52a0\u51cf<\/strong><\/h3>\n\n\n\n
\u4f7f\u7528timedelta\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u5728\u65e5\u671f\u4e0a\u505a\u5929days\uff0c\u5c0f\u65f6hour\uff0c\u5206\u949f\uff0c\u79d2\uff0c\u6beb\u79d2\uff0c\u5fae\u5999\u7684\u65f6\u95f4\u8ba1\u7b97\uff0c\u5982\u679c\u8981\u8ba1\u7b97\u6708\u4efd\u5219\u9700\u8981\u53e6\u5916\u7684\u529e\u6cd5\u3002<\/p>\n\n\n\n
#coding:utf-8\nfrom datetime import *\n\ndt = datetime.now()\n#\u65e5\u671f\u51cf\u4e00\u5929\ndt1 = dt + timedelta(days=-1)#\u6628\u5929\ndt2 = dt - timedelta(days=1)#\u6628\u5929\ndt3 = dt + timedelta(days=1)#\u660e\u5929\ndelta_obj = dt3-dt\nprint type(delta_obj),delta_obj#<type 'datetime.timedelta'> 1 day, 0:00:00\nprint delta_obj.days ,delta_obj.total_seconds()#1 86400.0<\/pre>\n\n\n\ntzinfo\u65f6\u533a\u7c7b<\/strong><\/h3>\n\n\n\n
#! \/usr\/bin\/python\n# coding=utf-8\n\nfrom datetime import datetime, tzinfo,timedelta\n\n\"\"\"\ntzinfo\u662f\u5173\u4e8e\u65f6\u533a\u4fe1\u606f\u7684\u7c7b\ntzinfo\u662f\u4e00\u4e2a\u62bd\u8c61\u7c7b\uff0c\u6240\u4ee5\u4e0d\u80fd\u76f4\u63a5\u88ab\u5b9e\u4f8b\u5316\n\"\"\"\nclass UTC(tzinfo):\n \"\"\"UTC\"\"\"\n def __init__(self,offset = 0):\n self._offset = offset\n\n def utcoffset(self, dt):\n return timedelta(hours=self._offset)\n\n def tzname(self, dt):\n return \"UTC +%s\" % self._offset\n\n def dst(self, dt):\n return timedelta(hours=self._offset)\n\n#\u5317\u4eac\u65f6\u95f4\nbeijing = datetime(2011,11,11,0,0,0,tzinfo = UTC(8))\nprint \"beijing time:\",beijing\n#\u66fc\u8c37\u65f6\u95f4\nbangkok = datetime(2011,11,11,0,0,0,tzinfo = UTC(7))\nprint \"bangkok time\",bangkok\n#\u5317\u4eac\u65f6\u95f4\u8f6c\u6210\u66fc\u8c37\u65f6\u95f4\nprint \"beijing-time to bangkok-time:\",beijing.astimezone(UTC(7))\n\n#\u8ba1\u7b97\u65f6\u95f4\u5dee\u65f6\u4e5f\u4f1a\u8003\u8651\u65f6\u533a\u7684\u95ee\u9898\ntimespan = beijing - bangkok\nprint \"\u65f6\u5dee:\",timespan\n\n#Output==================\n# beijing time: 2011-11-11 00:00:00+08:00\n# bangkok time 2011-11-11 00:00:00+07:00\n# beijing-time to bangkok-time: 2011-11-10 23:00:00+07:00\n# \u65f6\u5dee: -1 day, 23:00:00<\/pre>\n\n\n\nhashlib\u52a0\u5bc6<\/h2>\n\n\n\n
hashlib\u4e3b\u8981\u63d0\u4f9b\u5b57\u7b26\u52a0\u5bc6\u529f\u80fd\uff0c\u5c06md5\u548csha\u6a21\u5757\u6574\u5408\u5230\u4e86\u4e00\u8d77\uff0c\u652f\u6301md5,sha1, sha224, sha256, sha384, sha512\u7b49\u7b97\u6cd5<\/p>\n\n\n\n
\u5177\u4f53\u5e94\u7528<\/h3>\n\n\n\n
#!\/usr\/bin\/env python\n# -*- coding: UTF-8 -*-\n#pyversion:python3.5\n#owner:fuzj\n\n\n\n\nimport hashlib\n\n# ######## md5 ########\n\nstring = \"beyongjie\"\n\n\nmd5 = hashlib.md5()\nmd5.update(string.encode('utf-8')) #\u6ce8\u610f\u8f6c\u7801\nres = md5.hexdigest()\nprint(\"md5\u52a0\u5bc6\u7ed3\u679c:\",res)\n\n# ######## sha1 ########\n\nsha1 = hashlib.sha1()\nsha1.update(string.encode('utf-8'))\nres = sha1.hexdigest()\nprint(\"sha1\u52a0\u5bc6\u7ed3\u679c:\",res)\n\n# ######## sha256 ########\n\nsha256 = hashlib.sha256()\nsha256.update(string.encode('utf-8'))\nres = sha256.hexdigest()\nprint(\"sha256\u52a0\u5bc6\u7ed3\u679c:\",res)\n\n\n# ######## sha384 ########\n\nsha384 = hashlib.sha384()\nsha384.update(string.encode('utf-8'))\nres = sha384.hexdigest()\nprint(\"sha384\u52a0\u5bc6\u7ed3\u679c:\",res)\n\n# ######## sha512 ########\n\nsha512= hashlib.sha512()\nsha512.update(string.encode('utf-8'))\nres = sha512.hexdigest()\nprint(\"sha512\u52a0\u5bc6\u7ed3\u679c:\",res)<\/pre>\n\n\n\n\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n
md5\u52a0\u5bc6\u7ed3\u679c: 0e725e477851ff4076f774dc312d4748\nsha1\u52a0\u5bc6\u7ed3\u679c: 458d32be8ea38b66300174970ab0a8c0b734252f\nsha256\u52a0\u5bc6\u7ed3\u679c: 1e62b55bfd02977943f885f6a0998af7cc9cfb95c8ac4a9f30ecccb7c05ec9f4\nsha384\u52a0\u5bc6\u7ed3\u679c: e91cdf0d2570de5c96ee84e8a12cddf16508685e7a03b3e811099cfcd54b7f52183e20197cff7c07f312157f0ba4875b\nsha512\u52a0\u5bc6\u7ed3\u679c: 3f0020a726e9c1cb5d22290c967f3dd1bcecb409a51a8088db520750c876aaec3f17a70d7981cd575ed4b89471f743f3f24a146a39d59f215ae3e208d0170073<\/pre>\n\n\n\n\u6ce8\u610f\uff1ahashlib \u52a0\u5bc6\u554a\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u4e3a\u4e8c\u8fdb\u5236\u7f16\u7801\uff0c\u76f4\u63a5\u52a0\u5bc6\u5b57\u7b26\u4e32\u4f1a\u62a5\u5982\u4e0b\u9519\u8bef\uff1a<\/p>\n\n\n\n
sha1 = hashlib.sha1()\nsha1.update(string)\nres = sha1.hexdigest()\nprint(\"sha1\u52a0\u5bc6\u7ed3\u679c:\",res)\n\n\nTypeError: Unicode-objects must be encoded before hashing<\/pre>\n\n\n\n\u53ef\u4ee5\u4f7f\u7528encode\u8fdb\u884c\u8f6c\u6362<\/p>\n\n\n\n
shaa1 = hashlib.sha1()\nshaa1.update(string.encode('utf-8'))\nres = shaa1.hexdigest()\nprint(\"sha1\u91c7\u7528encode\u8f6c\u6362\u52a0\u5bc6\u7ed3\u679c:\",res)<\/pre>\n\n\n\n\u6216\u8005\u4f7f\u7528byte\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236<\/p>\n\n\n\n
shab1 = hashlib.sha1()\nshab1.update(bytes(string,encoding='utf-8'))\nres = shab1.hexdigest()\nprint(\"sha1\u91c7\u7528byte\u8f6c\u6362\u7684\u7ed3\u679c:\",res)<\/pre>\n\n\n\n\u4ee5\u4e0a\u8f93\u51fa\uff1a<\/p>\n\n\n\n
sha1\u91c7\u7528encode\u8f6c\u6362\u52a0\u5bc6\u7ed3\u679c: 458d32be8ea38b66300174970ab0a8c0b734252f\nsha1\u91c7\u7528byte\u8f6c\u6362\u7684\u7ed3\u679c: 458d32be8ea38b66300174970ab0a8c0b734252f<\/pre>\n\n\n\n\u5e38\u7528\u65b9\u6cd5<\/h3>\n\n\n\n
- hash.update(arg) \u66f4\u65b0\u54c8\u5e0c\u5bf9\u8c61\u4ee5\u5b57\u7b26\u4e32\u53c2\u6570, \u6ce8\u610f\uff1a\u5982\u679c\u540c\u4e00\u4e2ahash\u5bf9\u8c61\u91cd\u590d\u8c03\u7528\u8be5\u65b9\u6cd5\uff0c\u5219m.update(a); m.update(b) \u7b49\u6548\u4e8e m.update(a+b)\uff0c\u770b\u4e0b\u9762\u4f8b\u5b50<\/li><\/ul>\n\n\n\n
- hash.digest() \u8fd4\u56de\u6458\u8981\uff0c\u4f5c\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e\u5b57\u7b26\u4e32\u503c,<\/li>
- hash.hexdigest() \u8fd4\u56de\u6458\u8981\uff0c\u4f5c\u4e3a\u5341\u516d\u8fdb\u5236\u6570\u636e\u5b57\u7b26\u4e32\u503c,<\/li>
- hash.copy() \u590d\u5236<\/li><\/ul>\n\n\n\n
\u9ad8\u7ea7\u52a0\u5bc6<\/h3>\n\n\n\n
\u4ee5\u4e0a\u52a0\u5bc6\u7b97\u6cd5\u867d\u7136\u4f9d\u7136\u975e\u5e38\u5389\u5bb3\uff0c\u4f46\u65f6\u5019\u5b58\u5728\u7f3a\u9677\uff0c\u5373\uff1a\u901a\u8fc7\u649e\u5e93\u53ef\u4ee5\u53cd\u89e3\u3002\u6240\u4ee5\uff0c\u6709\u5fc5\u8981\u5bf9\u52a0\u5bc6\u7b97\u6cd5\u4e2d\u6dfb\u52a0\u81ea\u5b9a\u4e49key\u518d\u6765\u505a\u52a0\u5bc6\u3002<\/p>\n\n\n\n
low = hashlib.md5()\nlow.update('ab'.encode('utf-8'))\nres = low.hexdigest()\nprint(\"\u666e\u901a\u52a0\u5bc6:\",res)\n\nhigh = hashlib.md5(b'beyondjie')\nhigh.update('ab'.encode('utf-8'))\nres = high.hexdigest()\nprint(\"\u91c7\u7528key\u52a0\u5bc6:\",res)\n\n\u8f93\u51fa\u7ed3\u679c\uff1a\n\u666e\u901a\u52a0\u5bc6: 187ef4436122d1cc2f40dc2b92f0eba0\n\u91c7\u7528key\u52a0\u5bc6: 1b073f6b8cffe609751e4c98537b7653<\/pre>\n\n\n\n\u9644\u52a0HMAC-SHA1\u5404\u8bed\u8a00\u7248\u672c\u5b9e\u73b0<\/h3>\n\n\n\n
\u5728\u5404\u5927\u5f00\u653e\u5e73\u53f0\u5927\u884c\u5176\u9053\u7684\u4e92\u8054\u7f51\u5f00\u53d1\u6f6e\u6d41\u4e2d\uff0c\u8c03\u7528\u5404\u5e73\u53f0\u7684API\u63a5\u53e3\u8fc7\u7a0b\u4e2d\uff0c\u65e0\u4e00\u4f8b\u5916\u90fd\u4f1a\u7528\u5230\u8ba1\u7b97\u7b7e\u540d\u503c(sig\u503c)\u3002\u800c\u5728\u5404\u79cd\u8ba1\u7b97\u7b7e\u540d\u7684\u65b9\u6cd5\u4e2d\uff0c\u7ecf\u5e38\u88ab\u91c7\u7528\u7684\u5c31\u662fHMAC-SHA1\uff0c\u73b0\u5bf9HMAC-SHA1\u505a\u4e00\u4e2a\u7b80\u5355\u7684\u4ecb\u7ecd\uff1a\n\n HMAC\uff0c\u6563\u5217\u6d88\u606f\u9274\u522b\u7801\uff0c\u57fa\u4e8e\u5bc6\u94a5\u7684Hash\u7b97\u6cd5\u8ba4\u8bc1\u534f\u8bae\u3002\u5b9e\u73b0\u539f\u7406\u4e3a\uff1a\u5229\u7528\u5df2\u7ecf\u516c\u5f00\u7684Hash\u51fd\u6570\u548c\u79c1\u6709\u7684\u5bc6\u94a5\uff0c\u6765\u751f\u6210\u56fa\u5b9a\u957f\u5ea6\u7684\u6d88\u606f\u9274\u522b\u7801\uff1b\n\n SHA1\u3001MD5\u7b49Hash\u7b97\u6cd5\u662f\u6bd4\u8f83\u5e38\u7528\u7684\u4e0d\u53ef\u9006Hash\u7b7e\u540d\u8ba1\u7b97\u65b9\u6cd5\uff1b\n\n BASE64\uff0c\u5c06\u4efb\u610f\u5e8f\u5217\u76848\u5b57\u8282\u5b57\u7b26\u8f6c\u6362\u4e3a\u4eba\u773c\u65e0\u6cd5\u76f4\u63a5\u8bc6\u522b\u7684\u7b26\u53f7\u7f16\u7801\u7684\u4e00\u79cd\u65b9\u6cd5\uff1b\n\n \u5404\u4e2a\u8bed\u8a00\u7248\u672c\u7684\u5b9e\u73b0\u4e3a\uff1a\n\n Python\u7248\uff1a\n\n import hmac\n\n import hashlib\n\n import base64\n\n hmac.new(Token,data,hashlib.sha1).digest().encode('base64').rstrip()\n\nToken\uff1a\u5373\u63a5\u53e3\u7684key\n\ndata\uff1a\u8981\u52a0\u5bc6\u7684\u6570\u636e\n\n PHP\u7248\uff1a\n base64_encode(hash_hmac(\"SHA1\",clientStr,Token , true))\n C++\u7248\uff08Openssl)\uff1a\n\n HMAC( EVP_sha1(),\n\n \/*key data*\/ strKey.data(),\n \/*key len*\/ strKey.size(),\n \/*data *\/(unsigned char*) strRandom.data(),\n \/*data len*\/ strRandom.size(), digest, &digest_len))\n Shell\u7248\uff1a\n echo -n '3f88a95c532bea70' | openssl dgst -hmac '123' -sha1 -binary | base64
<\/pre>\n\n\n\nlogging\u6a21\u5757\u7b80\u4ecb<\/h2>\n\n\n\n
logging\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u6807\u51c6\u6a21\u5757\uff0c\u4e3b\u8981\u7528\u4e8e\u8f93\u51fa\u8fd0\u884c\u65e5\u5fd7\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u8f93\u51fa\u65e5\u5fd7\u7684\u7b49\u7ea7\u3001\u65e5\u5fd7\u4fdd\u5b58\u8def\u5f84\u3001\u65e5\u5fd7\u6587\u4ef6\u56de\u6eda\u7b49\uff1b\u76f8\u6bd4print\uff0c\u5177\u5907\u5982\u4e0b\u4f18\u70b9\uff1a<\/p>\n\n\n\n
- \u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e0d\u540c\u7684\u65e5\u5fd7\u7b49\u7ea7\uff0c\u5728release\u7248\u672c\u4e2d\u53ea\u8f93\u51fa\u91cd\u8981\u4fe1\u606f\uff0c\u800c\u4e0d\u5fc5\u663e\u793a\u5927\u91cf\u7684\u8c03\u8bd5\u4fe1\u606f\uff1b<\/li>
- print\u5c06\u6240\u6709\u4fe1\u606f\u90fd\u8f93\u51fa\u5230\u6807\u51c6\u8f93\u51fa\u4e2d\uff0c\u4e25\u91cd\u5f71\u54cd\u5f00\u53d1\u8005\u4ece\u6807\u51c6\u8f93\u51fa\u4e2d\u67e5\u770b\u5176\u5b83\u6570\u636e\uff1blogging\u5219\u53ef\u4ee5\u7531\u5f00\u53d1\u8005\u51b3\u5b9a\u5c06\u4fe1\u606f\u8f93\u51fa\u5230\u4ec0\u4e48\u5730\u65b9\uff0c\u4ee5\u53ca\u600e\u4e48\u8f93\u51fa\uff1b<\/li><\/ol>\n\n\n\n
logging\u6a21\u5757\u4f7f\u7528<\/h2>\n\n\n\n
\u57fa\u672c\u4f7f\u7528<\/h4>\n\n\n\n
\u914d\u7f6elogging\u57fa\u672c\u7684\u8bbe\u7f6e\uff0c\u7136\u540e\u5728\u63a7\u5236\u53f0\u8f93\u51fa\u65e5\u5fd7\uff0c<\/p>\n\n\n\n
import logging\nlogging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')\nlogger = logging.getLogger(__name__)\n\nlogger.info(\"Start print log\")\nlogger.debug(\"Do something\")\nlogger.warning(\"Something maybe fail.\")\nlogger.info(\"Finish\")<\/pre>\n\n\n\n\u8fd0\u884c\u65f6\uff0c\u63a7\u5236\u53f0\u8f93\u51fa\uff0c<\/p>\n\n\n\n
2016-10-09 19:11:19,434 - __main__ - INFO - Start print log\n2016-10-09 19:11:19,434 - __main__ - WARNING - Something maybe fail.\n2016-10-09 19:11:19,434 - __main__ - INFO - Finish<\/code><\/pre>\n\n\n\n
logging\u4e2d\u53ef\u4ee5\u9009\u62e9\u5f88\u591a\u6d88\u606f\u7ea7\u522b\uff0c\u5982debug\u3001info\u3001warning\u3001error\u4ee5\u53cacritical\u3002\u901a\u8fc7\u8d4b\u4e88logger\u6216\u8005handler\u4e0d\u540c\u7684\u7ea7\u522b\uff0c\u5f00\u53d1\u8005\u5c31\u53ef\u4ee5\u53ea\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u5230\u7279\u5b9a\u7684\u8bb0\u5f55\u6587\u4ef6\uff0c\u6216\u8005\u5728\u8c03\u8bd5\u65f6\u53ea\u8bb0\u5f55\u8c03\u8bd5\u4fe1\u606f\u3002<\/p>\n\n\n\n
\u4f8b\u5982\uff0c\u6211\u4eec\u5c06logger\u7684\u7ea7\u522b\u6539\u4e3aDEBUG\uff0c\u518d\u89c2\u5bdf\u4e00\u4e0b\u8f93\u51fa\u7ed3\u679c\uff0c<\/p>\n\n\n\n
logging.basicConfig(level = logging.DEBUG,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')<\/pre>\n\n\n\n\u63a7\u5236\u53f0\u8f93\u51fa\uff0c\u53ef\u4ee5\u53d1\u73b0\uff0c\u8f93\u51fa\u4e86debug\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n
2016-10-09 19:12:08,289 - __main__ - INFO - Start print log\n2016-10-09 19:12:08,289 - __main__ - DEBUG - Do something\n2016-10-09 19:12:08,289 - __main__ - WARNING - Something maybe fail.\n2016-10-09 19:12:08,289 - __main__ - INFO - Finish<\/code><\/pre>\n\n\n\n
logging.basicConfig\u51fd\u6570\u5404\u53c2\u6570\uff1a<\/p>\n\n\n\n
filename\uff1a\u6307\u5b9a\u65e5\u5fd7\u6587\u4ef6\u540d\uff1b<\/p>\n\n\n\n
filemode\uff1a\u548cfile\u51fd\u6570\u610f\u4e49\u76f8\u540c\uff0c\u6307\u5b9a\u65e5\u5fd7\u6587\u4ef6\u7684\u6253\u5f00\u6a21\u5f0f\uff0c'w'\u6216\u8005'a'\uff1b<\/p>\n\n\n\n
format\uff1a\u6307\u5b9a\u8f93\u51fa\u7684\u683c\u5f0f\u548c\u5185\u5bb9\uff0cformat\u53ef\u4ee5\u8f93\u51fa\u5f88\u591a\u6709\u7528\u7684\u4fe1\u606f\uff0c<\/p>\n\n\n\n
\u53c2\u6570\uff1a\u4f5c\u7528\n\n%(levelno)s\uff1a\u6253\u5370\u65e5\u5fd7\u7ea7\u522b\u7684\u6570\u503c\n%(levelname)s\uff1a\u6253\u5370\u65e5\u5fd7\u7ea7\u522b\u7684\u540d\u79f0\n%(pathname)s\uff1a\u6253\u5370\u5f53\u524d\u6267\u884c\u7a0b\u5e8f\u7684\u8def\u5f84\uff0c\u5176\u5b9e\u5c31\u662fsys.argv[0]\n%(filename)s\uff1a\u6253\u5370\u5f53\u524d\u6267\u884c\u7a0b\u5e8f\u540d\n%(funcName)s\uff1a\u6253\u5370\u65e5\u5fd7\u7684\u5f53\u524d\u51fd\u6570\n%(lineno)d\uff1a\u6253\u5370\u65e5\u5fd7\u7684\u5f53\u524d\u884c\u53f7\n%(asctime)s\uff1a\u6253\u5370\u65e5\u5fd7\u7684\u65f6\u95f4\n%(thread)d\uff1a\u6253\u5370\u7ebf\u7a0bID\n%(threadName)s\uff1a\u6253\u5370\u7ebf\u7a0b\u540d\u79f0\n%(process)d\uff1a\u6253\u5370\u8fdb\u7a0bID\n%(message)s\uff1a\u6253\u5370\u65e5\u5fd7\u4fe1\u606f<\/code><\/pre>\n\n\n\n
datefmt\uff1a\u6307\u5b9a\u65f6\u95f4\u683c\u5f0f\uff0c\u540ctime.strftime()\uff1b<\/p>\n\n\n\n
level\uff1a\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\uff0c\u9ed8\u8ba4\u4e3alogging.WARNNING\uff1b<\/p>\n\n\n\n
stream\uff1a\u6307\u5b9a\u5c06\u65e5\u5fd7\u7684\u8f93\u51fa\u6d41\uff0c\u53ef\u4ee5\u6307\u5b9a\u8f93\u51fa\u5230sys.stderr\uff0csys.stdout\u6216\u8005\u6587\u4ef6\uff0c\u9ed8\u8ba4\u8f93\u51fa\u5230sys.stderr\uff0c\u5f53stream\u548cfilename\u540c\u65f6\u6307\u5b9a\u65f6\uff0cstream\u88ab\u5ffd\u7565\uff1b<\/p>\n\n\n\n
\u5c06\u65e5\u5fd7\u5199\u5165\u5230\u6587\u4ef6<\/h4>\n\n\n\n
<\/p>\n\n\n\n
\u8bbe\u7f6elogging\uff0c\u521b\u5efa\u4e00\u4e2aFileHandler\uff0c\u5e76\u5bf9\u8f93\u51fa\u6d88\u606f\u7684\u683c\u5f0f\u8fdb\u884c\u8bbe\u7f6e\uff0c\u5c06\u5176\u6dfb\u52a0\u5230logger\uff0c\u7136\u540e\u5c06\u65e5\u5fd7\u5199\u5165\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u4e2d\uff0c<\/p>\n\n\n\n
import logging\nlogger = logging.getLogger(__name__)\nlogger.setLevel(level = logging.INFO)\nhandler = logging.FileHandler(\"log.txt\")\nhandler.setLevel(logging.INFO)\nformatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')\nhandler.setFormatter(formatter)\nlogger.addHandler(handler)\n\nlogger.info(\"Start print log\")\nlogger.debug(\"Do something\")\nlogger.warning(\"Something maybe fail.\")\nlogger.info(\"Finish\")<\/pre>\n\n\n\nlog.txt\u4e2d\u65e5\u5fd7\u6570\u636e\u4e3a\uff0c<\/p>\n\n\n\n
2016-10-09 19:01:13,263 - __main__ - INFO - Start print log\n2016-10-09 19:01:13,263 - __main__ - WARNING - Something maybe fail.\n2016-10-09 19:01:13,263 - __main__ - INFO - Finish<\/code><\/pre>\n\n\n\n
\u5c06\u65e5\u5fd7\u540c\u65f6\u8f93\u51fa\u5230\u5c4f\u5e55\u548c\u65e5\u5fd7\u6587\u4ef6<\/strong><\/h4>\n\n\n\n
logger\u4e2d\u6dfb\u52a0StreamHandler\uff0c\u53ef\u4ee5\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u5c4f\u5e55\u4e0a\uff0c<\/p>\n\n\n\n
import logging\nlogger = logging.getLogger(__name__)\nlogger.setLevel(level = logging.INFO)\nhandler = logging.FileHandler(\"log.txt\")\nhandler.setLevel(logging.INFO)\nformatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')\nhandler.setFormatter(formatter)\n\nconsole = logging.StreamHandler()\nconsole.setLevel(logging.INFO)\n\nlogger.addHandler(handler)\nlogger.addHandler(console)\n\nlogger.info(\"Start print log\")\nlogger.debug(\"Do something\")\nlogger.warning(\"Something maybe fail.\")\nlogger.info(\"Finish\")<\/pre>\n\n\n\n\u53ef\u4ee5\u5728log.txt\u6587\u4ef6\u548c\u63a7\u5236\u53f0\u4e2d\u770b\u5230\uff0c<\/p>\n\n\n\n
2016-10-09 19:20:46,553 - __main__ - INFO - Start print log\n2016-10-09 19:20:46,553 - __main__ - WARNING - Something maybe fail.\n2016-10-09 19:20:46,553 - __main__ - INFO - Finish<\/code><\/pre>\n\n\n\n
\u53ef\u4ee5\u53d1\u73b0\uff0clogging\u6709\u4e00\u4e2a\u65e5\u5fd7\u5904\u7406\u7684\u4e3b\u5bf9\u8c61\uff0c\u5176\u4ed6\u5904\u7406\u65b9\u5f0f\u90fd\u662f\u901a\u8fc7addHandler\u6dfb\u52a0\u8fdb\u53bb\uff0clogging\u4e2d\u5305\u542b\u7684handler\u4e3b\u8981\u6709\u5982\u4e0b\u51e0\u79cd\uff0c<\/p>\n\n\n\n
handler\u540d\u79f0\uff1a\u4f4d\u7f6e\uff1b\u4f5c\u7528\n\nStreamHandler\uff1alogging.StreamHandler\uff1b\u65e5\u5fd7\u8f93\u51fa\u5230\u6d41\uff0c\u53ef\u4ee5\u662fsys.stderr\uff0csys.stdout\u6216\u8005\u6587\u4ef6\nFileHandler\uff1alogging.FileHandler\uff1b\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\nBaseRotatingHandler\uff1alogging.handlers.BaseRotatingHandler\uff1b\u57fa\u672c\u7684\u65e5\u5fd7\u56de\u6eda\u65b9\u5f0f\nRotatingHandler\uff1alogging.handlers.RotatingHandler\uff1b\u65e5\u5fd7\u56de\u6eda\u65b9\u5f0f\uff0c\u652f\u6301\u65e5\u5fd7\u6587\u4ef6\u6700\u5927\u6570\u91cf\u548c\u65e5\u5fd7\u6587\u4ef6\u56de\u6eda\nTimeRotatingHandler\uff1alogging.handlers.TimeRotatingHandler\uff1b\u65e5\u5fd7\u56de\u6eda\u65b9\u5f0f\uff0c\u5728\u4e00\u5b9a\u65f6\u95f4\u533a\u57df\u5185\u56de\u6eda\u65e5\u5fd7\u6587\u4ef6\nSocketHandler\uff1alogging.handlers.SocketHandler\uff1b\u8fdc\u7a0b\u8f93\u51fa\u65e5\u5fd7\u5230TCP\/IP sockets\nDatagramHandler\uff1alogging.handlers.DatagramHandler\uff1b\u8fdc\u7a0b\u8f93\u51fa\u65e5\u5fd7\u5230UDP sockets\nSMTPHandler\uff1alogging.handlers.SMTPHandler\uff1b\u8fdc\u7a0b\u8f93\u51fa\u65e5\u5fd7\u5230\u90ae\u4ef6\u5730\u5740\nSysLogHandler\uff1alogging.handlers.SysLogHandler\uff1b\u65e5\u5fd7\u8f93\u51fa\u5230syslog\nNTEventLogHandler\uff1alogging.handlers.NTEventLogHandler\uff1b\u8fdc\u7a0b\u8f93\u51fa\u65e5\u5fd7\u5230Windows NT\/2000\/XP\u7684\u4e8b\u4ef6\u65e5\u5fd7\nMemoryHandler\uff1alogging.handlers.MemoryHandler\uff1b\u65e5\u5fd7\u8f93\u51fa\u5230\u5185\u5b58\u4e2d\u7684\u6307\u5b9abuffer\nHTTPHandler\uff1alogging.handlers.HTTPHandler\uff1b\u901a\u8fc7\"GET\"\u6216\u8005\"POST\"\u8fdc\u7a0b\u8f93\u51fa\u5230HTTP\u670d\u52a1\u5668<\/code><\/pre>\n\n\n\n
\u65e5\u5fd7\u56de\u6eda<\/strong><\/h4>\n\n\n\n
\u4f7f\u7528RotatingFileHandler\uff0c\u53ef\u4ee5\u5b9e\u73b0\u65e5\u5fd7\u56de\u6eda\uff0c<\/p>\n\n\n\n
import logging\nfrom logging.handlers import RotatingFileHandler\nlogger = logging.getLogger(__name__)\nlogger.setLevel(level = logging.INFO)\n#\u5b9a\u4e49\u4e00\u4e2aRotatingFileHandler\uff0c\u6700\u591a\u5907\u4efd3\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff0c\u6bcf\u4e2a\u65e5\u5fd7\u6587\u4ef6\u6700\u59271K\nrHandler = RotatingFileHandler(\"log.txt\",maxBytes = 1*1024,backupCount = 3)\nrHandler.setLevel(logging.INFO)\nformatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')\nrHandler.setFormatter(formatter)\n\nconsole = logging.StreamHandler()\nconsole.setLevel(logging.INFO)\nconsole.setFormatter(formatter)\n\nlogger.addHandler(rHandler)\nlogger.addHandler(console)\n\nlogger.info(\"Start print log\")\nlogger.debug(\"Do something\")\nlogger.warning(\"Something maybe fail.\")\nlogger.info(\"Finish\")<\/pre>\n\n\n\n\u53ef\u4ee5\u5728\u5de5\u7a0b\u76ee\u5f55\u4e2d\u770b\u5230\uff0c\u5907\u4efd\u7684\u65e5\u5fd7\u6587\u4ef6\uff0c<\/p>\n\n\n\n
2016\/10\/09 19:36 732 log.txt\n2016\/10\/09 19:36 967 log.txt.1\n2016\/10\/09 19:36 985 log.txt.2\n2016\/10\/09 19:36 976 log.txt.3<\/code><\/pre>\n\n\n\n
\u8bbe\u7f6e\u6d88\u606f\u7684\u7b49\u7ea7<\/h4>\n\n\n\n
\u53ef\u4ee5\u8bbe\u7f6e\u4e0d\u540c\u7684\u65e5\u5fd7\u7b49\u7ea7\uff0c\u7528\u4e8e\u63a7\u5236\u65e5\u5fd7\u7684\u8f93\u51fa\uff0c<\/p>\n\n\n\n
\u65e5\u5fd7\u7b49\u7ea7\uff1a\u4f7f\u7528\u8303\u56f4\n\nFATAL\uff1a\u81f4\u547d\u9519\u8bef\nCRITICAL\uff1a\u7279\u522b\u7cdf\u7cd5\u7684\u4e8b\u60c5\uff0c\u5982\u5185\u5b58\u8017\u5c3d\u3001\u78c1\u76d8\u7a7a\u95f4\u4e3a\u7a7a\uff0c\u4e00\u822c\u5f88\u5c11\u4f7f\u7528\nERROR\uff1a\u53d1\u751f\u9519\u8bef\u65f6\uff0c\u5982IO\u64cd\u4f5c\u5931\u8d25\u6216\u8005\u8fde\u63a5\u95ee\u9898\nWARNING\uff1a\u53d1\u751f\u5f88\u91cd\u8981\u7684\u4e8b\u4ef6\uff0c\u4f46\u662f\u5e76\u4e0d\u662f\u9519\u8bef\u65f6\uff0c\u5982\u7528\u6237\u767b\u5f55\u5bc6\u7801\u9519\u8bef\nINFO\uff1a\u5904\u7406\u8bf7\u6c42\u6216\u8005\u72b6\u6001\u53d8\u5316\u7b49\u65e5\u5e38\u4e8b\u52a1\nDEBUG\uff1a\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\u4f7f\u7528DEBUG\u7b49\u7ea7\uff0c\u5982\u7b97\u6cd5\u4e2d\u6bcf\u4e2a\u5faa\u73af\u7684\u4e2d\u95f4\u72b6\u6001<\/code><\/pre>\n\n\n\n
\u6355\u83b7traceback<\/h4>\n\n\n\n
Python\u4e2d\u7684traceback\u6a21\u5757\u88ab\u7528\u4e8e\u8ddf\u8e2a\u5f02\u5e38\u8fd4\u56de\u4fe1\u606f\uff0c\u53ef\u4ee5\u5728logging\u4e2d\u8bb0\u5f55\u4e0btraceback\uff0c<\/p>\n\n\n\n
\u4ee3\u7801\uff0c<\/p>\n\n\n\n
import logging\nlogger = logging.getLogger(__name__)\nlogger.setLevel(level = logging.INFO)\nhandler = logging.FileHandler(\"log.txt\")\nhandler.setLevel(logging.INFO)\nformatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')\nhandler.setFormatter(formatter)\n\nconsole = logging.StreamHandler()\nconsole.setLevel(logging.INFO)\n\nlogger.addHandler(handler)\nlogger.addHandler(console)\n\nlogger.info(\"Start print log\")\nlogger.debug(\"Do something\")\nlogger.warning(\"Something maybe fail.\")\ntry:\n open(\"sklearn.txt\",\"rb\")\nexcept (SystemExit,KeyboardInterrupt):\n raise\nexcept Exception:\n logger.error(\"Faild to open sklearn.txt from logger.error\",exc_info = True)\n\nlogger.info(\"Finish\")<\/pre>\n\n\n\n<\/figure>\n\n\n\n\u63a7\u5236\u53f0\u548c\u65e5\u5fd7\u6587\u4ef6log.txt\u4e2d\u8f93\u51fa\uff0c<\/p>\n\n\n\n
Start print log\nSomething maybe fail.\nFaild to open sklearn.txt from logger.error\nTraceback (most recent call last):\n File \"G:\\zhb7627\\Code\\Eclipse WorkSpace\\PythonTest\\test.py\", line 23, in <module>\n open(\"sklearn.txt\",\"rb\")\nIOError: [Errno 2] No such file or directory: 'sklearn.txt'\nFinish<\/code><\/pre>\n\n\n\n
\u4e5f\u53ef\u4ee5\u4f7f\u7528logger.exception(msg,_args)\uff0c\u5b83\u7b49\u4ef7\u4e8elogger.error(msg,exc_info = True,_args)\uff0c<\/p>\n\n\n\n
\u5c06<\/p>\n\n\n\n
logger.error(\"Faild to open sklearn.txt from logger.error\",exc_info = True)<\/code><\/pre>\n\n\n\n
\u66ff\u6362\u4e3a\uff0c<\/p>\n\n\n\n
logger.exception(\"Failed to open sklearn.txt from logger.exception\")<\/code><\/pre>\n\n\n\n
\u63a7\u5236\u53f0\u548c\u65e5\u5fd7\u6587\u4ef6log.txt\u4e2d\u8f93\u51fa\uff0c<\/p>\n\n\n\n
Start print log\nSomething maybe fail.\nFailed to open sklearn.txt from logger.exception\nTraceback (most recent call last):\n File \"G:\\zhb7627\\Code\\Eclipse WorkSpace\\PythonTest\\test.py\", line 23, in <module>\n open(\"sklearn.txt\",\"rb\")\nIOError: [Errno 2] No such file or directory: 'sklearn.txt'\nFinish<\/code><\/pre>\n\n\n\n
\u591a\u6a21\u5757\u4f7f\u7528logging<\/h4>\n\n\n\n
\u4e3b\u6a21\u5757mainModule.py\uff0c<\/p>\n\n\n\n
import logging\nimport subModule\nlogger = logging.getLogger(\"mainModule\")\nlogger.setLevel(level = logging.INFO)\nhandler = logging.FileHandler(\"log.txt\")\nhandler.setLevel(logging.INFO)\nformatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')\nhandler.setFormatter(formatter)\n\nconsole = logging.StreamHandler()\nconsole.setLevel(logging.INFO)\nconsole.setFormatter(formatter)\n\nlogger.addHandler(handler)\nlogger.addHandler(console)\n\n\nlogger.info(\"creating an instance of subModule.subModuleClass\")\na = subModule.SubModuleClass()\nlogger.info(\"calling subModule.subModuleClass.doSomething\")\na.doSomething()\nlogger.info(\"done with subModule.subModuleClass.doSomething\")\nlogger.info(\"calling subModule.some_function\")\nsubModule.som_function()\nlogger.info(\"done with subModule.some_function\")<\/pre>\n\n\n\n\u5b50\u6a21\u5757subModule.py\uff0c<\/p>\n\n\n\n
import logging\n\nmodule_logger = logging.getLogger(\"mainModule.sub\")\nclass SubModuleClass(object):\n def __init__(self):\n self.logger = logging.getLogger(\"mainModule.sub.module\")\n self.logger.info(\"creating an instance in SubModuleClass\")\n def doSomething(self):\n self.logger.info(\"do something in SubModule\")\n a = []\n a.append(1)\n self.logger.debug(\"list a = \" + str(a))\n self.logger.info(\"finish something in SubModuleClass\")\n\ndef som_function():\n module_logger.info(\"call function some_function\")<\/pre>\n\n\n\n\u6267\u884c\u4e4b\u540e\uff0c\u5728\u63a7\u5236\u548c\u65e5\u5fd7\u6587\u4ef6log.txt\u4e2d\u8f93\u51fa\uff0c<\/p>\n\n\n\n
2016-10-09 20:25:42,276 - mainModule - INFO - creating an instance of subModule.subModuleClass\n2016-10-09 20:25:42,279 - mainModule.sub.module - INFO - creating an instance in SubModuleClass\n2016-10-09 20:25:42,279 - mainModule - INFO - calling subModule.subModuleClass.doSomething\n2016-10-09 20:25:42,279 - mainModule.sub.module - INFO - do something in SubModule\n2016-10-09 20:25:42,279 - mainModule.sub.module - INFO - finish something in SubModuleClass\n2016-10-09 20:25:42,279 - mainModule - INFO - done with subModule.subModuleClass.doSomething\n2016-10-09 20:25:42,279 - mainModule - INFO - calling subModule.some_function\n2016-10-09 20:25:42,279 - mainModule.sub - INFO - call function some_function\n2016-10-09 20:25:42,279 - mainModule - INFO - done with subModule.some_function<\/code><\/pre>\n\n\n\n
\u9996\u5148\u5728\u4e3b\u6a21\u5757\u5b9a\u4e49\u4e86logger'mainModule'\uff0c\u5e76\u5bf9\u5b83\u8fdb\u884c\u4e86\u914d\u7f6e\uff0c\u5c31\u53ef\u4ee5\u5728\u89e3\u91ca\u5668\u8fdb\u7a0b\u91cc\u9762\u7684\u5176\u4ed6\u5730\u65b9\u901a\u8fc7getLogger('mainModule')\u5f97\u5230\u7684\u5bf9\u8c61\u90fd\u662f\u4e00\u6837\u7684\uff0c\u4e0d\u9700\u8981\u91cd\u65b0\u914d\u7f6e\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002\u5b9a\u4e49\u7684\u8be5logger\u7684\u5b50logger\uff0c\u90fd\u53ef\u4ee5\u5171\u4eab\u7236logger\u7684\u5b9a\u4e49\u548c\u914d\u7f6e\uff0c\u6240\u8c13\u7684\u7236\u5b50logger\u662f\u901a\u8fc7\u547d\u540d\u6765\u8bc6\u522b\uff0c\u4efb\u610f\u4ee5'mainModule'\u5f00\u5934\u7684logger\u90fd\u662f\u5b83\u7684\u5b50logger\uff0c\u4f8b\u5982'mainModule.sub'\u3002<\/p>\n\n\n\n
\u5b9e\u9645\u5f00\u53d1\u4e00\u4e2aapplication\uff0c\u9996\u5148\u53ef\u4ee5\u901a\u8fc7logging\u914d\u7f6e\u6587\u4ef6\u7f16\u5199\u597d\u8fd9\u4e2aapplication\u6240\u5bf9\u5e94\u7684\u914d\u7f6e\uff0c\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u6839logger\uff0c\u5982'PythonAPP'\uff0c\u7136\u540e\u5728\u4e3b\u51fd\u6570\u4e2d\u901a\u8fc7fileConfig\u52a0\u8f7dlogging\u914d\u7f6e\uff0c\u63a5\u7740\u5728application\u7684\u5176\u4ed6\u5730\u65b9\u3001\u4e0d\u540c\u7684\u6a21\u5757\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6839logger\u7684\u5b50logger\uff0c\u5982'PythonAPP.Core'\uff0c'PythonAPP.Web'\u6765\u8fdb\u884clog\uff0c\u800c\u4e0d\u9700\u8981\u53cd\u590d\u7684\u5b9a\u4e49\u548c\u914d\u7f6e\u5404\u4e2a\u6a21\u5757\u7684logger\u3002<\/p>\n\n\n\n
\u901a\u8fc7JSON\u6216\u8005YAML\u6587\u4ef6\u914d\u7f6elogging\u6a21\u5757<\/h4>\n\n\n\n
\u5c3d\u7ba1\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u914d\u7f6elogging\uff0c\u4f46\u662f\u8fd9\u6837\u5e76\u4e0d\u591f\u7075\u6d3b\uff0c\u6700\u597d\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6\u6765\u914d\u7f6e\u3002\u5728Python 2.7\u53ca\u4ee5\u540e\u7684\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u4ece\u5b57\u5178\u4e2d\u52a0\u8f7dlogging\u914d\u7f6e\uff0c\u4e5f\u5c31\u610f\u5473\u7740\u53ef\u4ee5\u901a\u8fc7JSON\u6216\u8005YAML\u6587\u4ef6\u52a0\u8f7d\u65e5\u5fd7\u7684\u914d\u7f6e\u3002<\/p>\n\n\n\n
\u901a\u8fc7JSON\u6587\u4ef6\u914d\u7f6e<\/h4>\n\n\n\n
JSON\u914d\u7f6e\u6587\u4ef6\uff0c<\/p>\n\n\n\n
{\n \"version\":1,\n \"disable_existing_loggers\":false,\n \"formatters\":{\n \"simple\":{\n \"format\":\"%(asctime)s - %(name)s - %(levelname)s - %(message)s\"\n }\n },\n \"handlers\":{\n \"console\":{\n \"class\":\"logging.StreamHandler\",\n \"level\":\"DEBUG\",\n \"formatter\":\"simple\",\n \"stream\":\"ext:\/\/sys.stdout\"\n },\n \"info_file_handler\":{\n \"class\":\"logging.handlers.RotatingFileHandler\",\n \"level\":\"INFO\",\n \"formatter\":\"simple\",\n \"filename\":\"info.log\",\n \"maxBytes\":\"10485760\",\n \"backupCount\":20,\n \"encoding\":\"utf8\"\n },\n \"error_file_handler\":{\n \"class\":\"logging.handlers.RotatingFileHandler\",\n \"level\":\"ERROR\",\n \"formatter\":\"simple\",\n \"filename\":\"errors.log\",\n \"maxBytes\":10485760,\n \"backupCount\":20,\n \"encoding\":\"utf8\"\n }\n },\n \"loggers\":{\n \"my_module\":{\n \"level\":\"ERROR\",\n \"handlers\":[\"info_file_handler\"],\n \"propagate\":\"no\"\n }\n },\n \"root\":{\n \"level\":\"INFO\",\n \"handlers\":[\"console\",\"info_file_handler\",\"error_file_handler\"]\n }\n}<\/code><\/pre>\n\n\n\n
\u901a\u8fc7JSON\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\uff0c\u7136\u540e\u901a\u8fc7logging.dictConfig\u914d\u7f6elogging\uff0c<\/p>\n\n\n\n
import json\nimport logging.config\nimport os\n\ndef setup_logging(default_path = \"logging.json\",default_level = logging.INFO,env_key = \"LOG_CFG\"):\n path = default_path\n value = os.getenv(env_key,None)\n if value:\n path = value\n if os.path.exists(path):\n with open(path,\"r\") as f:\n config = json.load(f)\n logging.config.dictConfig(config)\n else:\n logging.basicConfig(level = default_level)\n\ndef func():\n logging.info(\"start func\")\n\n logging.info(\"exec func\")\n\n logging.info(\"end func\")\n\nif __name__ == \"__main__\":\n setup_logging(default_path = \"logging.json\")\n func()<\/pre>\n\n\n\n\u901a\u8fc7YAML\u6587\u4ef6\u914d\u7f6e<\/h4>\n\n\n\n
\u901a\u8fc7YAML\u6587\u4ef6\u8fdb\u884c\u914d\u7f6e\uff0c\u6bd4JSON\u770b\u8d77\u6765\u66f4\u52a0\u7b80\u4ecb\u660e\u4e86\uff0c<\/p>\n\n\n\n
version: 1\ndisable_existing_loggers: False\nformatters:\n simple:\n format: \"%(asctime)s - %(name)s - %(levelname)s - %(message)s\"\nhandlers:\n console:\n class: logging.StreamHandler\n level: DEBUG\n formatter: simple\n stream: ext:\/\/sys.stdout\n info_file_handler:\n class: logging.handlers.RotatingFileHandler\n level: INFO\n formatter: simple\n filename: info.log\n maxBytes: 10485760\n backupCount: 20\n encoding: utf8\n error_file_handler:\n class: logging.handlers.RotatingFileHandler\n level: ERROR\n formatter: simple\n filename: errors.log\n maxBytes: 10485760\n backupCount: 20\n encoding: utf8\nloggers:\n my_module:\n level: ERROR\n handlers: [info_file_handler]\n propagate: no\nroot:\n level: INFO\n handlers: [console,info_file_handler,error_file_handler]<\/code><\/pre>\n\n\n\n
\u901a\u8fc7YAML\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\uff0c\u7136\u540e\u901a\u8fc7logging.dictConfig\u914d\u7f6elogging\uff0c<\/p>\n\n\n\n
import yaml\nimport logging.config\nimport os\n\ndef setup_logging(default_path = \"logging.yaml\",default_level = logging.INFO,env_key = \"LOG_CFG\"):\n path = default_path\n value = os.getenv(env_key,None)\n if value:\n path = value\n if os.path.exists(path):\n with open(path,\"r\") as f:\n config = yaml.load(f)\n logging.config.dictConfig(config)\n else:\n logging.basicConfig(level = default_level)\n\ndef func():\n logging.info(\"start func\")\n\n logging.info(\"exec func\")\n\n logging.info(\"end func\")\n\nif __name__ == \"__main__\":\n setup_logging(default_path = \"logging.yaml\")\n func()<\/pre>\n\n\n\nsubprocess\u6a21\u5757<\/h2>\n\n\n\n
\n\n\n\nsubprocess\u662fPython 2.4\u4e2d\u65b0\u589e\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u5b83\u5141\u8bb8\u4f60\u751f\u6210\u65b0\u7684\u8fdb\u7a0b\uff0c\u8fde\u63a5\u5230\u5b83\u4eec\u7684 input\/output\/error \u7ba1\u9053\uff0c\u5e76\u83b7\u53d6\u5b83\u4eec\u7684\u8fd4\u56de\uff08\u72b6\u6001\uff09\u7801\u3002\u8fd9\u4e2a\u6a21\u5757\u7684\u76ee\u7684\u5728\u4e8e\u66ff\u6362\u51e0\u4e2a\u65e7\u7684\u6a21\u5757\u548c\u65b9\u6cd5\uff0c\u5982\uff1a<\/p>\n\n\n\n
- os.system<\/li>
- os.spawn*<\/li><\/ul>\n\n\n\n
subprocess\u6a21\u5757\u4e2d\u7684\u5e38\u7528\u51fd\u6570<\/h3>\n\n\n\n
\u51fd\u6570<\/th> \u63cf\u8ff0<\/th><\/tr><\/thead> subprocess.run()<\/td> Python 3.5\u4e2d\u65b0\u589e\u7684\u51fd\u6570\u3002\u6267\u884c\u6307\u5b9a\u7684\u547d\u4ee4\uff0c\u7b49\u5f85\u547d\u4ee4\u6267\u884c\u5b8c\u6210\u540e\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6267\u884c\u7ed3\u679c\u7684CompletedProcess\u7c7b\u7684\u5b9e\u4f8b\u3002<\/td><\/tr> subprocess.call()<\/td> \u6267\u884c\u6307\u5b9a\u7684\u547d\u4ee4\uff0c\u8fd4\u56de\u547d\u4ee4\u6267\u884c\u72b6\u6001\uff0c\u5176\u529f\u80fd\u7c7b\u4f3c\u4e8eos.system(cmd)\u3002<\/td><\/tr> subprocess.check_call()<\/td> Python 2.5\u4e2d\u65b0\u589e\u7684\u51fd\u6570\u3002 \u6267\u884c\u6307\u5b9a\u7684\u547d\u4ee4\uff0c\u5982\u679c\u6267\u884c\u6210\u529f\u5219\u8fd4\u56de\u72b6\u6001\u7801\uff0c\u5426\u5219\u629b\u51fa\u5f02\u5e38\u3002\u5176\u529f\u80fd\u7b49\u4ef7\u4e8esubprocess.run(..., check=True)\u3002<\/td><\/tr> subprocess.check_output()<\/td> Python 2.7\u4e2d\u65b0\u589e\u7684\u7684\u51fd\u6570\u3002\u6267\u884c\u6307\u5b9a\u7684\u547d\u4ee4\uff0c\u5982\u679c\u6267\u884c\u72b6\u6001\u7801\u4e3a0\u5219\u8fd4\u56de\u547d\u4ee4\u6267\u884c\u7ed3\u679c\uff0c\u5426\u5219\u629b\u51fa\u5f02\u5e38\u3002<\/td><\/tr> subprocess.getoutput(cmd)<\/td> \u63a5\u6536\u5b57\u7b26\u4e32\u683c\u5f0f\u7684\u547d\u4ee4\uff0c\u6267\u884c\u547d\u4ee4\u5e76\u8fd4\u56de\u6267\u884c\u7ed3\u679c\uff0c\u5176\u529f\u80fd\u7c7b\u4f3c\u4e8eos.popen(cmd).read()\u548ccommands.getoutput(cmd)\u3002<\/td><\/tr> subprocess.getstatusoutput(cmd)<\/td> \u6267\u884ccmd\u547d\u4ee4\uff0c\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4(\u547d\u4ee4\u6267\u884c\u72b6\u6001, \u547d\u4ee4\u6267\u884c\u7ed3\u679c\u8f93\u51fa)\uff0c\u5176\u529f\u80fd\u7c7b\u4f3c\u4e8ecommands.getstatusoutput()\u3002<\/td><\/tr><\/tbody><\/table>\n\n\n\n \u8bf4\u660e\uff1a<\/em><\/strong><\/p>\n\n\n\n
- \u5728Python 3.5\u4e4b\u540e\u7684\u7248\u672c\u4e2d\uff0c\u5b98\u65b9\u6587\u6863\u4e2d\u63d0\u5021\u901a\u8fc7subprocess.run()\u51fd\u6570\u66ff\u4ee3\u5176\u4ed6\u51fd\u6570\u6765\u4f7f\u7528subproccess\u6a21\u5757\u7684\u529f\u80fd\uff1b<\/li>
- \u5728Python 3.5\u4e4b\u524d\u7684\u7248\u672c\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7subprocess.call()\uff0csubprocess.getoutput()\u7b49\u4e0a\u9762\u5217\u51fa\u7684\u5176\u4ed6\u51fd\u6570\u6765\u4f7f\u7528subprocess\u6a21\u5757\u7684\u529f\u80fd\uff1b<\/li>
- subprocess.run()\u3001subprocess.call()\u3001subprocess.check_call()\u548csubprocess.check_output()\u90fd\u662f\u901a\u8fc7\u5bf9subprocess.Popen\u7684\u5c01\u88c5\u6765\u5b9e\u73b0\u7684\u9ad8\u7ea7\u51fd\u6570\uff0c\u56e0\u6b64\u5982\u679c\u6211\u4eec\u9700\u8981\u66f4\u590d\u6742\u529f\u80fd\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7subprocess.Popen\u6765\u5b8c\u6210\u3002<\/li>
- subprocess.getoutput()\u548csubprocess.getstatusoutput()\u51fd\u6570\u662f\u6765\u81eaPython 2.x\u7684commands\u6a21\u5757\u7684\u4e24\u4e2a\u9057\u7559\u51fd\u6570\u3002\u5b83\u4eec\u9690\u5f0f\u7684\u8c03\u7528\u7cfb\u7edfshell\uff0c\u5e76\u4e14\u4e0d\u4fdd\u8bc1\u5176\u4ed6\u51fd\u6570\u6240\u5177\u6709\u7684\u5b89\u5168\u6027\u548c\u5f02\u5e38\u5904\u7406\u7684\u4e00\u81f4\u6027\u3002\u53e6\u5916\uff0c\u5b83\u4eec\u4ecePython 3.3.4\u5f00\u59cb\u624d\u652f\u6301Windows\u5e73\u53f0\u3002<\/li><\/ol>\n\n\n\n
\u4e0a\u9762\u5404\u51fd\u6570\u7684\u5b9a\u4e49\u53ca\u53c2\u6570\u8bf4\u660e<\/h3>\n\n\n\n
\u51fd\u6570\u53c2\u6570\u5217\u8868\uff1a<\/h4>\n\n\n\n
subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, shell=False, timeout=None, check=False, universal_newlines=False)\n\nsubprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)\n\nsubprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)\n\nsubprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False, timeout=None)\n\nsubprocess.getstatusoutput(cmd)\n\nsubprocess.getoutput(cmd)<\/code><\/pre>\n\n\n\n
\u53c2\u6570\u8bf4\u660e\uff1a<\/h4>\n\n\n\n
- args\uff1a<\/em><\/strong> \u8981\u6267\u884c\u7684shell\u547d\u4ee4\uff0c\u9ed8\u8ba4\u5e94\u8be5\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u5e8f\u5217\uff0c\u5982['df', '-Th']\u6216('df', '-Th')\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5982'df -Th'\uff0c\u4f46\u662f\u6b64\u65f6\u9700\u8981\u628ashell\u53c2\u6570\u7684\u503c\u7f6e\u4e3aTrue\u3002<\/li>
- shell\uff1a<\/em><\/strong> \u5982\u679cshell\u4e3aTrue\uff0c\u90a3\u4e48\u6307\u5b9a\u7684\u547d\u4ee4\u5c06\u901a\u8fc7shell\u6267\u884c\u3002\u5982\u679c\u6211\u4eec\u9700\u8981\u8bbf\u95ee\u67d0\u4e9bshell\u7684\u7279\u6027\uff0c\u5982\u7ba1\u9053\u3001\u6587\u4ef6\u540d\u901a\u914d\u7b26\u3001\u73af\u5883\u53d8\u91cf\u6269\u5c55\u529f\u80fd\uff0c\u8fd9\u5c06\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u5f53\u7136\uff0cpython\u672c\u8eab\u4e5f\u63d0\u4f9b\u4e86\u8bb8\u591a\u7c7b\u4f3cshell\u7684\u7279\u6027\u7684\u5b9e\u73b0\uff0c\u5982glob\u3001fnmatch\u3001os.walk()\u3001os.path.expandvars()\u3001os.expanduser()\u548cshutil\u7b49\u3002<\/li>
- check\uff1a<\/em><\/strong> \u5982\u679ccheck\u53c2\u6570\u7684\u503c\u662fTrue\uff0c\u4e14\u6267\u884c\u547d\u4ee4\u7684\u8fdb\u7a0b\u4ee5\u975e0\u72b6\u6001\u7801\u9000\u51fa\uff0c\u5219\u4f1a\u629b\u51fa\u4e00\u4e2aCalledProcessError\u7684\u5f02\u5e38\uff0c\u4e14\u8be5\u5f02\u5e38\u5bf9\u8c61\u4f1a\u5305\u542b \u53c2\u6570\u3001\u9000\u51fa\u72b6\u6001\u7801\u3001\u4ee5\u53castdout\u548cstderr(\u5982\u679c\u5b83\u4eec\u6709\u88ab\u6355\u83b7\u7684\u8bdd)\u3002<\/li>
- stdout, stderr\uff1a<\/em><\/strong><\/li>
- run()\u51fd\u6570\u9ed8\u8ba4\u4e0d\u4f1a\u6355\u83b7\u547d\u4ee4\u6267\u884c\u7ed3\u679c\u7684\u6b63\u5e38\u8f93\u51fa\u548c\u9519\u8bef\u8f93\u51fa\uff0c\u5982\u679c\u6211\u4eec\u5411\u83b7\u53d6\u8fd9\u4e9b\u5185\u5bb9\u9700\u8981\u4f20\u9012subprocess.PIPE\uff0c\u7136\u540e\u53ef\u4ee5\u901a\u8fc7\u8fd4\u56de\u7684CompletedProcess\u7c7b\u5b9e\u4f8b\u7684stdout\u548cstderr\u5c5e\u6027\u6216\u6355\u83b7\u76f8\u5e94\u7684\u5185\u5bb9\uff1b<\/li>
- call()\u548ccheck_call()\u51fd\u6570\u8fd4\u56de\u7684\u662f\u547d\u4ee4\u6267\u884c\u7684\u72b6\u6001\u7801\uff0c\u800c\u4e0d\u662fCompletedProcess\u7c7b\u5b9e\u4f8b\uff0c\u6240\u4ee5\u5bf9\u4e8e\u5b83\u4eec\u800c\u8a00\uff0cstdout\u548cstderr\u4e0d\u9002\u5408\u8d4b\u503c\u4e3asubprocess.PIPE\uff1b<\/li>
- check_output()\u51fd\u6570\u9ed8\u8ba4\u5c31\u4f1a\u8fd4\u56de\u547d\u4ee4\u6267\u884c\u7ed3\u679c\uff0c\u6240\u4ee5\u4e0d\u7528\u8bbe\u7f6estdout\u7684\u503c\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u5728\u7ed3\u679c\u4e2d\u6355\u83b7\u9519\u8bef\u4fe1\u606f\uff0c\u53ef\u4ee5\u6267\u884cstderr=subprocess.STDOUT\u3002<\/li>
- input\uff1a<\/em><\/strong> \u8be5\u53c2\u6570\u662f\u4f20\u9012\u7ed9Popen.communicate()\uff0c\u901a\u5e38\u8be5\u53c2\u6570\u7684\u503c\u5fc5\u987b\u662f\u4e00\u4e2a\u5b57\u8282\u5e8f\u5217\uff0c\u5982\u679cuniversal_newlines=True\uff0c\u5219\u5176\u503c\u5e94\u8be5\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/li>
- universal_newlines\uff1a<\/em><\/strong> \u8be5\u53c2\u6570\u5f71\u54cd\u7684\u662f\u8f93\u5165\u4e0e\u8f93\u51fa\u7684\u6570\u636e\u683c\u5f0f\uff0c\u6bd4\u5982\u5b83\u7684\u503c\u9ed8\u8ba4\u4e3aFalse\uff0c\u6b64\u65f6stdout\u548cstderr\u7684\u8f93\u51fa\u662f\u5b57\u8282\u5e8f\u5217\uff1b\u5f53\u8be5\u53c2\u6570\u7684\u503c\u8bbe\u7f6e\u4e3aTrue\u65f6\uff0cstdout\u548cstderr\u7684\u8f93\u51fa\u662f\u5b57\u7b26\u4e32\u3002<\/li><\/ul>\n\n\n\n
subprocess.CompletedProcess\u7c7b\u4ecb\u7ecd<\/h3>\n\n\n\n
\u9700\u8981\u8bf4\u660e\u7684\u662f\uff0csubprocess.run()\u51fd\u6570\u662fPython3.5\u4e2d\u65b0\u589e\u7684\u4e00\u4e2a\u9ad8\u7ea7\u51fd\u6570\uff0c\u5176\u8fd4\u56de\u503c\u662f\u4e00\u4e2asubprocess.CompletedPorcess\u7c7b\u7684\u5b9e\u4f8b\uff0c\u56e0\u6b64\uff0csubprocess.completedPorcess\u7c7b\u4e5f\u662fPython 3.5\u4e2d\u624d\u5b58\u5728\u7684\u3002\u5b83\u8868\u793a\u7684\u662f\u4e00\u4e2a\u5df2\u7ed3\u675f\u8fdb\u7a0b\u7684\u72b6\u6001\u4fe1\u606f\uff0c\u5b83\u6240\u5305\u542b\u7684\u5c5e\u6027\u5982\u4e0b\uff1a<\/p>\n\n\n\n
- args\uff1a<\/em><\/strong> \u7528\u4e8e\u52a0\u8f7d\u8be5\u8fdb\u7a0b\u7684\u53c2\u6570\uff0c\u8fd9\u53ef\u80fd\u662f\u4e00\u4e2a\u5217\u8868\u6216\u4e00\u4e2a\u5b57\u7b26\u4e32<\/li>
- returncode\uff1a<\/em><\/strong> \u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u72b6\u6001\u7801\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u9000\u51fa\u72b6\u6001\u7801\u4e3a0\u5219\u8868\u793a\u8fdb\u7a0b\u6210\u529f\u8fd0\u884c\u4e86\uff1b\u4e00\u4e2a\u8d1f\u503c-N\u8868\u793a\u8fd9\u4e2a\u5b50\u8fdb\u7a0b\u88ab\u4fe1\u53f7N\u7ec8\u6b62\u4e86<\/li>
- stdout\uff1a<\/em><\/strong> \u4ece\u5b50\u8fdb\u7a0b\u6355\u83b7\u7684stdout\u3002\u8fd9\u901a\u5e38\u662f\u4e00\u4e2a\u5b57\u8282\u5e8f\u5217\uff0c\u5982\u679crun()\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u6307\u5b9auniversal_newlines=True\uff0c\u5219\u8be5\u5c5e\u6027\u503c\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u5982\u679crun()\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u6307\u5b9astderr=subprocess.STDOUT\uff0c\u90a3\u4e48stdout\u548cstderr\u5c06\u4f1a\u88ab\u6574\u5408\u5230\u8fd9\u4e00\u4e2a\u5c5e\u6027\u4e2d\uff0c\u4e14stderr\u5c06\u4f1a\u4e3aNone<\/li>
- stderr\uff1a<\/em><\/strong> \u4ece\u5b50\u8fdb\u7a0b\u6355\u83b7\u7684stderr\u3002\u5b83\u7684\u503c\u4e0estdout\u4e00\u6837\uff0c\u662f\u4e00\u4e2a\u5b57\u8282\u5e8f\u5217\u6216\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u5982\u679cstderr\u706d\u6709\u88ab\u6355\u83b7\u7684\u8bdd\uff0c\u5b83\u7684\u503c\u5c31\u4e3aNone<\/li>
- check_returncode()\uff1a<\/em><\/strong> \u5982\u679creturncode\u662f\u4e00\u4e2a\u975e0\u503c\uff0c\u5219\u8be5\u65b9\u6cd5\u4f1a\u629b\u51fa\u4e00\u4e2aCalledProcessError\u5f02\u5e38\u3002<\/li><\/ul>\n\n\n\n
\u5b9e\u4f8b<\/h3>\n\n\n\n
subprocess.run()<\/h4>\n\n\n\n
>>> subprocess.run([\"ls\", \"-l\"]) # doesn't capture output\nCompletedProcess(args=['ls', '-l'], returncode=0)\n\n>>> subprocess.run(\"exit 1\", shell=True, check=True)\nTraceback (most recent call last):\n ...\nsubprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1\n\n>>> subprocess.run([\"ls\", \"-l\", \"\/dev\/null\"], stdout=subprocess.PIPE)\nCompletedProcess(args=['ls', '-l', '\/dev\/null'], returncode=0,\nstdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 \/dev\/null\\n')<\/code><\/pre>\n\n\n\n
subprocess.call()<\/h4>\n\n\n\n
>>> subprocess.call(['ls', '-l'])\n\u603b\u7528\u91cf 160\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u516c\u5171\u7684\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u6a21\u677f\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u89c6\u9891\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u56fe\u7247\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u6587\u6863\ndrwxr-xr-x 2 wader wader 4096 4\u6708 13 2016 \u4e0b\u8f7d\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u97f3\u4e50\ndrwxr-xr-x 7 wader wader 4096 5\u6708 26 2016 \u684c\u9762\n0\n>>> subprocess.call('ls -l', shell=True)\n\u603b\u7528\u91cf 160\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u516c\u5171\u7684\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u6a21\u677f\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u89c6\u9891\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u56fe\u7247\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u6587\u6863\ndrwxr-xr-x 2 wader wader 4096 4\u6708 13 2016 \u4e0b\u8f7d\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u97f3\u4e50\ndrwxr-xr-x 7 wader wader 4096 5\u6708 26 2016 \u684c\u9762\n0\n>>> subprocess.call(['ls', '-l'], stdout=subprocess.DEVNULL)\n0\n>>> subprocess.call(['ls', '-l', '\/test'])\nls: \u65e0\u6cd5\u8bbf\u95ee\/test: \u6ca1\u6709\u90a3\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55\n2<\/code><\/pre>\n\n\n\n
suprocess.check_call()<\/h4>\n\n\n\n
>>> subprocess.check_call(['ls', '-l'])\n\u603b\u7528\u91cf 160\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u516c\u5171\u7684\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u6a21\u677f\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u89c6\u9891\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u56fe\u7247\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u6587\u6863\ndrwxr-xr-x 2 wader wader 4096 4\u6708 13 2016 \u4e0b\u8f7d\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u97f3\u4e50\ndrwxr-xr-x 7 wader wader 4096 5\u6708 26 2016 \u684c\u9762\n0\n>>> subprocess.check_call('ls -l', shell=True)\n\u603b\u7528\u91cf 160\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u516c\u5171\u7684\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u6a21\u677f\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u89c6\u9891\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u56fe\u7247\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u6587\u6863\ndrwxr-xr-x 2 wader wader 4096 4\u6708 13 2016 \u4e0b\u8f7d\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u97f3\u4e50\ndrwxr-xr-x 7 wader wader 4096 5\u6708 26 2016 \u684c\u9762\n0\n>>> subprocess.check_call('ls -l \/test', shell=True)\nls: \u65e0\u6cd5\u8bbf\u95ee\/test: \u6ca1\u6709\u90a3\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55\nTraceback (most recent call last):\n File \"<stdin>\", line 1, in <module>\n File \"\/usr\/lib\/python3.4\/subprocess.py\", line 557, in check_call\n raise CalledProcessError(retcode, cmd)\nsubprocess.CalledProcessError: Command 'ls -l \/test' returned non-zero exit status 2\n<\/code><\/pre>\n\n\n\n
sbuprocess.check_output()<\/h4>\n\n\n\n
>>> ret = subprocess.check_output(['ls', '-l'])\n>>> print(ret)\nb' \\xe5\\x85\\xac\\xe5\\x85\\xb1\\xe7\\x9a\\x84\\ndrwxr-xr-x 2 wader wader 4096 12\\xe6\\x9c\\x88 7 2015 \\xe6\\xa8\\xa1\\xe6\\x9d\\xbf\\ndrwxr-xr-x 2 wader wader 4096 12\\xe6\\x9c\\x88 7 2015 \\xe8\\xa7\\x86\\xe9\\xa2\\x91\\ndrwxr-xr-x 2 wader wader 4096 12\\xe6\\x9c\\x88 7 2015 \\xe5\\x9b\\xbe\\xe7\\x89\\x87\\ndrwxr-xr-x 2 wader wader 4096 12\\xe6\\x9c\\x88 7 2015 \\xe6\\x96\\x87\\xe6\\xa1\\xa3\\ndrwxr-xr-x 2 wader wader 4096 4\\xe6\\x9c\\x88 13 2016 \\xe4\\xb8\\x8b\\xe8\\xbd\\xbd\\ndrwxr-xr-x 2 wader wader 4096 12\\xe6\\x9c\\x88 7 2015 \\xe9\\x9f\\xb3\\xe4\\xb9\\x90\\ndrwxr-xr-x 7 wader wader 4096 5\\xe6\\x9c\\x88 26 2016 \\xe6\\xa1\\x8c\\xe9\\x9d\\xa2\\n'\n>>> ret = subprocess.check_output(['ls', '-l'], universal_newlines=True)\n>>> print(ret)\n\u603b\u7528\u91cf 160\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u516c\u5171\u7684\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u6a21\u677f\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u89c6\u9891\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u56fe\u7247\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u6587\u6863\ndrwxr-xr-x 2 wader wader 4096 4\u6708 13 2016 \u4e0b\u8f7d\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u97f3\u4e50\ndrwxr-xr-x 7 wader wader 4096 5\u6708 26 2016 \u684c\u9762<\/code><\/pre>\n\n\n\n
subprocess.getoutput()\u4e0esubprocess.getstatusoutput()<\/h4>\n\n\n\n
>>> ret = subprocess.getoutput('ls -l')\n>>> print(ret)\n\u603b\u7528\u91cf 160\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u516c\u5171\u7684\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u6a21\u677f\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u89c6\u9891\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u56fe\u7247\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u6587\u6863\ndrwxr-xr-x 2 wader wader 4096 4\u6708 13 2016 \u4e0b\u8f7d\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u97f3\u4e50\ndrwxr-xr-x 7 wader wader 4096 5\u6708 26 2016 \u684c\u9762\n>>> retcode, output = subprocess.getstatusoutput('ls -l')\n>>> print(retcode)\n0\n>>> print(output)\n\u603b\u7528\u91cf 160\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u516c\u5171\u7684\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u6a21\u677f\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u89c6\u9891\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u56fe\u7247\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u6587\u6863\ndrwxr-xr-x 2 wader wader 4096 4\u6708 13 2016 \u4e0b\u8f7d\ndrwxr-xr-x 2 wader wader 4096 12\u6708 7 2015 \u97f3\u4e50\ndrwxr-xr-x 7 wader wader 4096 5\u6708 26 2016 \u684c\u9762\n>>> retcode, output = subprocess.getstatusoutput('ls -l \/test')\n>>> print(retcode)\n2\n>>> print(output)\nls: \u65e0\u6cd5\u8bbf\u95ee\/test: \u6ca1\u6709\u90a3\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55\n<\/code><\/pre>\n\n\n\n
subprocess.Popen\u4ecb\u7ecd<\/h2>\n\n\n\n
\n\n\n\n\u8be5\u7c7b\u7528\u4e8e\u5728\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\u4e2d\u6267\u884c\u4e00\u4e2a\u5b50\u7a0b\u5e8f\u3002\u524d\u9762\u6211\u4eec\u63d0\u5230\u8fc7\uff0c\u4e0a\u9762\u4ecb\u7ecd\u7684\u8fd9\u4e9b\u51fd\u6570\u90fd\u662f\u57fa\u4e8esubprocess.Popen\u7c7b\u5b9e\u73b0\u7684\uff0c\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u88ab\u5c01\u88c5\u540e\u7684\u9ad8\u7ea7\u51fd\u6570\u53ef\u4ee5\u5f88\u65b9\u9762\u7684\u5b8c\u6210\u4e00\u4e9b\u5e38\u89c1\u7684\u9700\u6c42\u3002\u7531\u4e8esubprocess\u6a21\u5757\u5e95\u5c42\u7684\u8fdb\u7a0b\u521b\u5efa\u548c\u7ba1\u7406\u662f\u7531Popen\u7c7b\u6765\u5904\u7406\u7684\uff0c\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u65e0\u6cd5\u901a\u8fc7\u4e0a\u9762\u54ea\u4e9b\u9ad8\u7ea7\u51fd\u6570\u6765\u5b9e\u73b0\u4e00\u4e9b\u4e0d\u592a\u5e38\u89c1\u7684\u529f\u80fd\u65f6\u5c31\u53ef\u4ee5\u901a\u8fc7subprocess.Popen\u7c7b\u63d0\u4f9b\u7684\u7075\u6d3b\u7684api\u6765\u5b8c\u6210\u3002<\/p>\n\n\n\n
subprocess.Popen\u7684\u6784\u9020\u51fd\u6570<\/h3>\n\n\n\n
class subprocess.Popen(args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None, \n preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=False,\n startup_info=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=())<\/code><\/pre>\n\n\n\n
\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n\n\n\n
- args\uff1a<\/em><\/strong> \u8981\u6267\u884c\u7684shell\u547d\u4ee4\uff0c\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u4ee5\u662f\u547d\u4ee4\u5404\u4e2a\u53c2\u6570\u7ec4\u6210\u7684\u5e8f\u5217\u3002\u5f53\u8be5\u53c2\u6570\u7684\u503c\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u65f6\uff0c\u8be5\u547d\u4ee4\u7684\u89e3\u91ca\u8fc7\u7a0b\u662f\u4e0e\u5e73\u53f0\u76f8\u5173\u7684\uff0c\u56e0\u6b64\u901a\u5e38\u5efa\u8bae\u5c06args\u53c2\u6570\u4f5c\u4e3a\u4e00\u4e2a\u5e8f\u5217\u4f20\u9012\u3002<\/li>
- bufsize\uff1a<\/em><\/strong> \u6307\u5b9a\u7f13\u5b58\u7b56\u7565\uff0c0\u8868\u793a\u4e0d\u7f13\u51b2\uff0c1\u8868\u793a\u884c\u7f13\u51b2\uff0c\u5176\u4ed6\u5927\u4e8e1\u7684\u6570\u5b57\u8868\u793a\u7f13\u51b2\u533a\u5927\u5c0f\uff0c\u8d1f\u6570 \u8868\u793a\u4f7f\u7528\u7cfb\u7edf\u9ed8\u8ba4\u7f13\u51b2\u7b56\u7565\u3002<\/li>
- stdin, stdout, stderr\uff1a<\/em><\/strong> \u5206\u522b\u8868\u793a\u7a0b\u5e8f\u6807\u51c6\u8f93\u5165\u3001\u8f93\u51fa\u3001\u9519\u8bef\u53e5\u67c4\u3002<\/li>
- preexec_fn\uff1a<\/em><\/strong> \u7528\u4e8e\u6307\u5b9a\u4e00\u4e2a\u5c06\u5728\u5b50\u8fdb\u7a0b\u8fd0\u884c\u4e4b\u524d\u88ab\u8c03\u7528\u7684\u53ef\u6267\u884c\u5bf9\u8c61\uff0c\u53ea\u5728Unix\u5e73\u53f0\u4e0b\u6709\u6548\u3002<\/li>
- close_fds\uff1a<\/em><\/strong> \u5982\u679c\u8be5\u53c2\u6570\u7684\u503c\u4e3aTrue\uff0c\u5219\u9664\u4e860,1\u548c2\u4e4b\u5916\u7684\u6240\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\u90fd\u5c06\u4f1a\u5728\u5b50\u8fdb\u7a0b\u6267\u884c\u4e4b\u524d\u88ab\u5173\u95ed\u3002<\/li>
- shell\uff1a<\/em><\/strong> \u8be5\u53c2\u6570\u7528\u4e8e\u6807\u8bc6\u662f\u5426\u4f7f\u7528shell\u4f5c\u4e3a\u8981\u6267\u884c\u7684\u7a0b\u5e8f\uff0c\u5982\u679cshell\u503c\u4e3aTrue\uff0c\u5219\u5efa\u8bae\u5c06args\u53c2\u6570\u4f5c\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u4f20\u9012\u800c\u4e0d\u8981\u4f5c\u4e3a\u4e00\u4e2a\u5e8f\u5217\u4f20\u9012\u3002<\/li>
- cwd\uff1a<\/em><\/strong> \u5982\u679c\u8be5\u53c2\u6570\u503c\u4e0d\u662fNone\uff0c\u5219\u8be5\u51fd\u6570\u5c06\u4f1a\u5728\u6267\u884c\u8fd9\u4e2a\u5b50\u8fdb\u7a0b\u4e4b\u524d\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002<\/li>
- env\uff1a<\/em><\/strong> \u7528\u4e8e\u6307\u5b9a\u5b50\u8fdb\u7a0b\u7684\u73af\u5883\u53d8\u91cf\uff0c\u5982\u679cenv=None\uff0c\u90a3\u4e48\u5b50\u8fdb\u7a0b\u7684\u73af\u5883\u53d8\u91cf\u5c06\u4ece\u7236\u8fdb\u7a0b\u4e2d\u7ee7\u627f\u3002\u5982\u679cenv!=None\uff0c\u5b83\u7684\u503c\u5fc5\u987b\u662f\u4e00\u4e2a\u6620\u5c04\u5bf9\u8c61\u3002<\/li>
- universal_newlines\uff1a<\/em><\/strong> \u5982\u679c\u8be5\u53c2\u6570\u503c\u4e3aTrue\uff0c\u5219\u8be5\u6587\u4ef6\u5bf9\u8c61\u7684stdin\uff0cstdout\u548cstderr\u5c06\u4f1a\u4f5c\u4e3a\u6587\u672c\u6d41\u88ab\u6253\u5f00\uff0c\u5426\u5219\u4ed6\u4eec\u5c06\u4f1a\u88ab\u4f5c\u4e3a\u4e8c\u8fdb\u5236\u6d41\u88ab\u6253\u5f00\u3002<\/li>
- startupinfo\u548ccreationflags\uff1a<\/em><\/strong> \u8fd9\u4e24\u4e2a\u53c2\u6570\u53ea\u5728Windows\u4e0b\u6709\u6548\uff0c\u5b83\u4eec\u5c06\u88ab\u4f20\u9012\u7ed9\u5e95\u5c42\u7684CreateProcess()\u51fd\u6570\uff0c\u7528\u4e8e\u8bbe\u7f6e\u5b50\u8fdb\u7a0b\u7684\u4e00\u4e9b\u5c5e\u6027\uff0c\u5982\u4e3b\u7a97\u53e3\u7684\u5916\u89c2\uff0c\u8fdb\u7a0b\u4f18\u5148\u7ea7\u7b49\u3002<\/li><\/ul>\n\n\n\n
subprocess.Popen\u7c7b\u7684\u5b9e\u4f8b\u53ef\u8c03\u7528\u7684\u65b9\u6cd5<\/h3>\n\n\n\n
\u65b9\u6cd5<\/th> \u63cf\u8ff0<\/th><\/tr><\/thead> Popen.poll()<\/td> \u7528\u4e8e\u68c0\u67e5\u5b50\u8fdb\u7a0b\uff08\u547d\u4ee4\uff09\u662f\u5426\u5df2\u7ecf\u6267\u884c\u7ed3\u675f\uff0c\u6ca1\u7ed3\u675f\u8fd4\u56deNone\uff0c\u7ed3\u675f\u540e\u8fd4\u56de\u72b6\u6001\u7801\u3002<\/td><\/tr> Popen.wait(timeout=None)<\/td> \u7b49\u5f85\u5b50\u8fdb\u7a0b\u7ed3\u675f\uff0c\u5e76\u8fd4\u56de\u72b6\u6001\u7801\uff1b\u5982\u679c\u5728timeout\u6307\u5b9a\u7684\u79d2\u6570\u4e4b\u540e\u8fdb\u7a0b\u8fd8\u6ca1\u6709\u7ed3\u675f\uff0c\u5c06\u4f1a\u629b\u51fa\u4e00\u4e2aTimeoutExpired\u5f02\u5e38\u3002<\/td><\/tr> Popen.communicate(input=None, timeout=None)<\/td> \u8be5\u65b9\u6cd5\u53ef\u7528\u6765\u4e0e\u8fdb\u7a0b\u8fdb\u884c\u4ea4\u4e92\uff0c\u6bd4\u5982\u53d1\u9001\u6570\u636e\u5230stdin\uff0c\u4ecestdout\u548cstderr\u8bfb\u53d6\u6570\u636e\uff0c\u76f4\u5230\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e\u3002<\/td><\/tr> Popen.send_signal(signal)<\/td> \u53d1\u9001\u6307\u5b9a\u7684\u4fe1\u53f7\u7ed9\u8fd9\u4e2a\u5b50\u8fdb\u7a0b\u3002<\/td><\/tr> Popen.terminate()<\/td> \u505c\u6b62\u8be5\u5b50\u8fdb\u7a0b\u3002<\/td><\/tr> Popen.kill()<\/td> \u6740\u6b7b\u8be5\u5b50\u8fdb\u7a0b\u3002<\/td><\/tr><\/tbody><\/table>\n\n\n\n \u5173\u4e8ecommunicate()\u65b9\u6cd5\u7684\u8bf4\u660e\uff1a<\/h4>\n\n\n\n
- \u8be5\u65b9\u6cd5\u4e2d\u7684\u53ef\u9009\u53c2\u6570 input \u5e94\u8be5\u662f\u5c06\u88ab\u53d1\u9001\u7ed9\u5b50\u8fdb\u7a0b\u7684\u6570\u636e\uff0c\u6216\u8005\u5982\u6ca1\u6709\u6570\u636e\u53d1\u9001\u7ed9\u5b50\u8fdb\u7a0b\uff0c\u8be5\u53c2\u6570\u5e94\u8be5\u662fNone\u3002input\u53c2\u6570\u7684\u6570\u636e\u7c7b\u578b\u5fc5\u987b\u662f\u5b57\u8282\u4e32\uff0c\u5982\u679cuniversal_newlines\u53c2\u6570\u503c\u4e3aTrue\uff0c\u5219input\u53c2\u6570\u7684\u6570\u636e\u7c7b\u578b\u5fc5\u987b\u662f\u5b57\u7b26\u4e32\u3002<\/li>
- \u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4(stdout_data, stderr_data)\uff0c\u8fd9\u4e9b\u6570\u636e\u5c06\u4f1a\u662f\u5b57\u8282\u7a7f\u6216\u5b57\u7b26\u4e32\uff08\u5982\u679cuniversal_newlines\u7684\u503c\u4e3aTrue\uff09\u3002<\/li>
- \u5982\u679c\u5728timeout\u6307\u5b9a\u7684\u79d2\u6570\u540e\u8be5\u8fdb\u7a0b\u8fd8\u6ca1\u6709\u7ed3\u675f\uff0c\u5c06\u4f1a\u629b\u51fa\u4e00\u4e2aTimeoutExpired\u5f02\u5e38\u3002\u6355\u83b7\u8fd9\u4e2a\u5f02\u5e38\uff0c\u7136\u540e\u91cd\u65b0\u5c1d\u8bd5\u901a\u4fe1\u4e0d\u4f1a\u4e22\u5931\u4efb\u4f55\u8f93\u51fa\u7684\u6570\u636e\u3002\u4f46\u662f\u8d85\u65f6\u4e4b\u540e\u5b50\u8fdb\u7a0b\u5e76\u6ca1\u6709\u88ab\u6740\u6b7b\uff0c\u4e3a\u4e86\u5408\u7406\u7684\u6e05\u9664\u76f8\u5e94\u7684\u5185\u5bb9\uff0c\u4e00\u4e2a\u597d\u7684\u5e94\u7528\u5e94\u8be5\u624b\u52a8\u6740\u6b7b\u8fd9\u4e2a\u5b50\u8fdb\u7a0b\u6765\u7ed3\u675f\u901a\u4fe1\u3002<\/li>
- \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u91cc\u8bfb\u53d6\u7684\u6570\u636e\u662f\u7f13\u51b2\u5728\u5185\u5b58\u4e2d\u7684\uff0c\u6240\u4ee5\uff0c\u5982\u679c\u6570\u636e\u5927\u5c0f\u975e\u5e38\u5927\u6216\u8005\u662f\u65e0\u9650\u7684\uff0c\u5c31\u4e0d\u5e94\u8be5\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u3002<\/li><\/ul>\n\n\n\n
subprocess.Popen\u4f7f\u7528\u5b9e\u4f8b<\/h3>\n\n\n\n
\u5b9e\u4f8b1\uff1a<\/h4>\n\n\n\n
>>> import subprocess\n>>>\n>>> p = subprocess.Popen('df -Th', stdout=subprocess.PIPE, shell=True)\n>>> print(p.stdout.read())\nFilesystem Type Size Used Avail Use% Mounted on\n\/dev\/vda1 ext4 40G 12G 26G 31% \/\ndevtmpfs devtmpfs 3.9G 0 3.9G 0% \/dev\ntmpfs tmpfs 3.9G 0 3.9G 0% \/dev\/shm\ntmpfs tmpfs 3.9G 386M 3.5G 10% \/run\ntmpfs tmpfs 3.9G 0 3.9G 0% \/sys\/fs\/cgroup\ntmpfs tmpfs 783M 0 783M 0% \/run\/user\/0\ntmpfs tmpfs 783M 0 783M 0% \/run\/user\/1000<\/code><\/pre>\n\n\n\n
\u5b9e\u4f8b2\uff1a<\/h4>\n\n\n\n
>>> obj = subprocess.Popen([\"python\"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n>>> obj.stdin.write('print(1) \\n')\n>>> obj.stdin.write('print(2) \\n')\n>>> obj.stdin.write('print(3) \\n')\n>>> out,err = obj.communicate()\n>>> print(out)\n1\n2\n3\n\n>>> print(err)\n<\/code><\/pre>\n\n\n\n
\u5b9e\u4f8b3\uff1a<\/h4>\n\n\n\n
>>> obj = subprocess.Popen([\"python\"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n>>> out,err = obj.communicate(input='print(1) \\n')\n>>> print(out)\n1\n\n>>> print(err)\n<\/code><\/pre>\n\n\n\n
\u5b9e\u4f8b4\uff1a<\/h4>\n\n\n\n
\u5b9e\u73b0\u7c7b\u4f3c
df -Th | grep data<\/code>\u547d\u4ee4\u7684\u529f\u80fd\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u5b9e\u73b0shell\u4e2d\u7ba1\u9053\u7684\u5171\u529f\u80fd\u3002<\/p>\n\n\n\n
>>> \n>>> p1 = subprocess.Popen(['df', '-Th'], stdout=subprocess.PIPE)\n>>> p2 = subprocess.Popen(['grep', 'data'], stdin=p1.stdout, stdout=subprocess.PIPE)\n>>> out,err = p2.communicate()\n>>> print(out)\n\/dev\/vdb1 ext4 493G 4.8G 463G 2% \/data\n\/dev\/vdd1 ext4 1008G 420G 537G 44% \/data1\n\/dev\/vde1 ext4 985G 503G 432G 54% \/data2\n\n>>> print(err)\nNone<\/code><\/pre>\n\n\n\n
\u603b\u7ed3<\/h2>\n\n\n\n
\n\n\n\n\u90a3\u4e48\u6211\u4eec\u5230\u5e95\u8be5\u7528\u54ea\u4e2a\u6a21\u5757\u3001\u54ea\u4e2a\u51fd\u6570\u6765\u6267\u884c\u547d\u4ee4\u4e0e\u7cfb\u7edf\u53ca\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u5462\uff1f\u4e0b\u9762\u6211\u4eec\u6765\u505a\u4e2a\u603b\u7ed3\uff1a<\/p>\n\n\n\n
- \u9996\u5148\u5e94\u8be5\u77e5\u9053\u7684\u662f\uff0cPython2.4\u7248\u672c\u5f15\u5165\u4e86subprocess\u6a21\u5757\u7528\u6765\u66ff\u6362os.system()\u3001os.popen()\u3001os.spawn*()\u7b49\u51fd\u6570\u4ee5\u53cacommands\u6a21\u5757\uff1b\u4e5f\u5c31\u662f\u8bf4\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fPython 2.4\u53ca\u4ee5\u4e0a\u7684\u7248\u672c\u5c31\u5e94\u8be5\u4f7f\u7528subprocess\u6a21\u5757\u4e86\u3002<\/li>
- \u5982\u679c\u4f60\u7684\u5e94\u7528\u4f7f\u7528\u7684Python 2.4\u4ee5\u4e0a\uff0c\u4f46\u662f\u662fPython 3.5\u4ee5\u4e0b\u7684\u7248\u672c\uff0cPython\u5b98\u65b9\u7ed9\u51fa\u7684\u5efa\u8bae\u662f\u4f7f\u7528subprocess.call()\u51fd\u6570\u3002Python 2.5\u4e2d\u65b0\u589e\u4e86\u4e00\u4e2asubprocess.check_call()\u51fd\u6570\uff0cPython 2.7\u4e2d\u65b0\u589e\u4e86\u4e00\u4e2asubprocess.check_output()\u51fd\u6570\uff0c\u8fd9\u4e24\u4e2a\u51fd\u6570\u4e5f\u53ef\u4ee5\u6309\u7167\u9700\u6c42\u8fdb\u884c\u4f7f\u7528\u3002<\/li>
- \u5982\u679c\u4f60\u7684\u5e94\u7528\u4f7f\u7528\u7684\u662fPython 3.5\u53ca\u4ee5\u4e0a\u7684\u7248\u672c\uff08\u76ee\u524d\u5e94\u8be5\u8fd8\u5f88\u5c11\uff09\uff0cPython\u5b98\u65b9\u7ed9\u51fa\u7684\u5efa\u8bae\u662f\u5c3d\u91cf\u4f7f\u7528subprocess.run()\u51fd\u6570\u3002<\/li>
- \u5f53subprocess.call()\u3001subprocess.check_call()\u3001subprocess.check_output()\u548csubprocess.run()\u8fd9\u4e9b\u9ad8\u7ea7\u51fd\u6570\u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528subprocess.Popen\u7c7b\u6765\u5b9e\u73b0\u6211\u4eec\u9700\u8981\u7684\u590d\u6742\u529f\u80fd\u3002<\/li><\/ul>\n\n\n\n