{"id":222122,"date":"2021-07-17T10:11:07","date_gmt":"2021-07-17T02:11:07","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=222122"},"modified":"2021-07-07T11:15:54","modified_gmt":"2021-07-07T03:15:54","slug":"code-python-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/code-python-linux.html","title":{"rendered":"\u5b9e\u4f8b\u4ee3\u7801\u5206\u4eabPython\u5b9e\u73b0Linux\u76d1\u63a7"},"content":{"rendered":"
\u5bfc\u8bfb<\/td>\n | \u672c\u6587\u901a\u8fc7\u5b9e\u4f8b\u4ee3\u7801\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e86Python\u5b9e\u73b0Linux\u76d1\u63a7\u7684\u65b9\u6cd5\uff0c\u975e\u5e38\u4e0d\u9519\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u501f\u9274\u4ef7\u503c,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n <\/p>\n \u5de5\u4f5c\u539f\u7406\uff1a\u57fa\u4e8e\/proc \u6587\u4ef6\u7cfb\u7edf<\/strong><\/div>\n Linux \u7cfb\u7edf\u4e3a\u7ba1\u7406\u5458\u63d0\u4f9b\u4e86\u975e\u5e38\u597d\u7684\u65b9\u6cd5\uff0c\u4f7f\u5176\u53ef\u4ee5\u5728\u7cfb\u7edf\u8fd0\u884c\u65f6\u66f4\u6539\u5185\u6838\uff0c\u800c\u4e0d\u9700\u8981\u91cd\u65b0\u5f15\u5bfc\u5185\u6838\u7cfb\u7edf\uff0c\u8fd9\u662f\u901a\u8fc7\/proc \u865a\u62df\u6587\u4ef6\u7cfb\u7edf\u5b9e\u73b0\u7684\u3002\/proc \u6587\u4ef6\u865a\u62df\u7cfb\u7edf\u662f\u4e00\u79cd\u5185\u6838\u548c\u5185\u6838\u6a21\u5757\u7528\u6765\u5411\u8fdb\u7a0b\uff08process\uff09\u53d1\u9001\u4fe1\u606f\u7684\u673a\u5236\uff08\u6240\u4ee5\u53eb\u505a\u201c\/proc\u201d\uff09\uff0c\u8fd9\u4e2a\u4f2a\u6587\u4ef6\u7cfb\u7edf\u5141\u8bb8\u4e0e\u5185\u6838\u5185\u90e8\u6570\u636e\u7ed3\u6784\u4ea4\u4e92\uff0c\u83b7\u53d6\u6709\u5173\u8fdb\u7a0b\u7684\u6709\u7528\u4fe1\u606f\uff0c\u5728\u8fd0\u884c\u4e2d\uff08on the fly\uff09\u6539\u53d8\u8bbe\u7f6e\uff08\u901a\u8fc7\u6539\u53d8\u5185\u6838\u53c2\u6570\uff09\u3002\u4e0e\u5176\u4ed6\u6587\u4ef6\u7cfb\u7edf\u4e0d\u540c\uff0c\/proc \u5b58\u5728\u4e8e\u5185\u5b58\u800c\u4e0d\u662f\u786c\u76d8\u4e2d\u3002proc \u6587\u4ef6\u7cfb\u7edf\u63d0\u4f9b\u7684\u4fe1\u606f\u5982\u4e0b\uff1a<\/p>\n \u8fdb\u7a0b\u4fe1\u606f\uff1a\u7cfb\u7edf\u4e2d\u7684\u4efb\u4f55\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u5728 proc \u7684\u5b50\u76ee\u5f55\u4e2d\u90fd\u6709\u4e00\u4e2a\u540c\u540d\u7684\u8fdb\u7a0b ID\uff0c\u53ef\u4ee5\u627e\u5230 cmdline\u3001mem\u3001root\u3001stat\u3001statm\uff0c\u4ee5\u53ca status\u3002\u67d0\u4e9b\u4fe1\u606f\u53ea\u6709\u8d85\u7ea7\u7528\u6237\u53ef\u89c1\uff0c\u4f8b\u5982\u8fdb\u7a0b\u6839\u76ee\u5f55\u3002\u6bcf\u4e00\u4e2a\u5355\u72ec\u542b\u6709\u73b0\u6709\u8fdb\u7a0b\u4fe1\u606f\u7684\u8fdb\u7a0b\u6709\u4e00\u4e9b\u53ef\u7528\u7684\u4e13\u95e8\u94fe\u63a5\uff0c\u7cfb\u7edf\u4e2d\u7684\u4efb\u4f55\u4e00\u4e2a\u8fdb\u7a0b\u90fd\u6709\u4e00\u4e2a\u5355\u72ec\u7684\u81ea\u94fe\u63a5\u6307\u5411\u8fdb\u7a0b\u4fe1\u606f\uff0c\u5176\u7528\u5904\u5c31\u662f\u4ece\u8fdb\u7a0b\u4e2d\u83b7\u53d6\u547d\u4ee4\u884c\u4fe1\u606f\u3002<\/p>\n \u7cfb\u7edf\u4fe1\u606f\uff1a\u5982\u679c\u9700\u8981\u4e86\u89e3\u6574\u4e2a\u7cfb\u7edf\u4fe1\u606f\u4e2d\u4e5f\u53ef\u4ee5\u4ece\/proc\/stat \u4e2d\u83b7\u5f97\uff0c\u5176\u4e2d\u5305\u62ec CPU \u5360\u7528\u60c5\u51b5\u3001\u78c1\u76d8\u7a7a\u95f4\u3001\u5185\u5b58\u5bf9\u6362\u3001\u4e2d\u65ad\u7b49\u3002<\/p>\n CPU \u4fe1\u606f\uff1a\u5229\u7528\/proc\/CPUinfo \u6587\u4ef6\u53ef\u4ee5\u83b7\u5f97\u4e2d\u592e\u5904\u7406\u5668\u7684\u5f53\u524d\u51c6\u786e\u4fe1\u606f\u3002<\/p>\n \u8d1f\u8f7d\u4fe1\u606f\uff1a\/proc\/loadavg \u6587\u4ef6\u5305\u542b\u7cfb\u7edf\u8d1f\u8f7d\u4fe1\u606f\u3002<\/p>\n \u7cfb\u7edf\u5185\u5b58\u4fe1\u606f\uff1a\/proc\/meminfo \u6587\u4ef6\u5305\u542b\u7cfb\u7edf\u5185\u5b58\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5176\u4e2d\u663e\u793a\u7269\u7406\u5185\u5b58\u7684\u6570\u91cf\u3001\u53ef\u7528\u4ea4\u6362\u7a7a\u95f4\u7684\u6570\u91cf\uff0c\u4ee5\u53ca\u7a7a\u95f2\u5185\u5b58\u7684\u6570\u91cf\u7b49\u3002<\/p>\n \/proc \u76ee\u5f55\u4e2d\u7684\u4e3b\u8981\u6587\u4ef6\u7684\u8bf4\u660e<\/strong><\/div>\n \n- apm \u9ad8\u7ea7\u7535\u6e90\u7ba1\u7406\u4fe1\u606f<\/p>\n - cmdline \u8fd9\u4e2a\u6587\u4ef6\u7ed9\u51fa\u4e86\u5185\u6838\u542f\u52a8\u7684\u547d\u4ee4\u884c<\/p>\n - CPUinfo \u4e2d\u592e\u5904\u7406\u5668\u4fe1\u606f<\/p>\n - devices \u53ef\u4ee5\u7528\u5230\u7684\u8bbe\u5907\uff08\u5757\u8bbe\u5907\/\u5b57\u7b26\u8bbe\u5907\uff09<\/p>\n - dma \u663e\u793a\u5f53\u524d\u4f7f\u7528\u7684 DMA \u901a\u9053<\/p>\n - filesystems \u6838\u5fc3\u914d\u7f6e\u7684\u6587\u4ef6\u7cfb\u7edf<\/p>\n - ioports \u5f53\u524d\u4f7f\u7528\u7684 I\/O \u7aef\u53e3<\/p>\n - interrupts \u8fd9\u4e2a\u6587\u4ef6\u7684\u6bcf\u4e00\u884c\u90fd\u6709\u4e00\u4e2a\u4fdd\u7559\u7684\u4e2d\u65ad<\/p>\n - kcore \u7cfb\u7edf\u7269\u7406\u5185\u5b58\u6620\u50cf<\/p>\n - kmsg \u6838\u5fc3\u8f93\u51fa\u7684\u6d88\u606f\uff0c\u88ab\u9001\u5230\u65e5\u5fd7\u6587\u4ef6<\/p>\n - mdstat \u8fd9\u4e2a\u6587\u4ef6\u5305\u542b\u4e86\u7531 md \u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\u63a7\u5236\u7684 RAID \u8bbe\u5907\u4fe1\u606f<\/p>\n - loadavg \u7cfb\u7edf\u5e73\u5747\u8d1f\u8f7d\u5747\u8861<\/p>\n - meminfo \u5b58\u50a8\u5668\u4f7f\u7528\u4fe1\u606f\uff0c\u5305\u62ec\u7269\u7406\u5185\u5b58\u548c\u4ea4\u6362\u5185\u5b58<\/p>\n - modules \u8fd9\u4e2a\u6587\u4ef6\u7ed9\u51fa\u53ef\u52a0\u8f7d\u5185\u6838\u6a21\u5757\u7684\u4fe1\u606f\u3002lsmod \u7a0b\u5e8f\u7528\u8fd9\u4e9b\u4fe1\u606f\u663e\u793a\u6709\u5173\u6a21\u5757\u7684\u540d\u79f0\uff0c\u5927\u5c0f\uff0c\u4f7f\u7528\u6570\u76ee\u65b9\u9762\u7684\u4fe1\u606f<\/p>\n - net \u7f51\u7edc\u534f\u8bae\u72b6\u6001\u4fe1\u606f<\/p>\n - partitions \u7cfb\u7edf\u8bc6\u522b\u7684\u5206\u533a\u8868<\/p>\n - pci pci \u8bbe\u5907\u4fe1\u606f<\/p>\n - scsi scsi \u8bbe\u5907\u4fe1\u606f<\/p>\n - self \u5230\u67e5\u770b\/proc \u7a0b\u5e8f\u8fdb\u7a0b\u76ee\u5f55\u7684\u7b26\u53f7\u8fde\u63a5<\/p>\n - stat \u8fd9\u4e2a\u6587\u4ef6\u5305\u542b\u7684\u4fe1\u606f\u6709 CPU \u5229\u7528\u7387\uff0c\u78c1\u76d8\uff0c\u5185\u5b58\u9875\uff0c\u5185\u5b58\u5bf9\u6362\uff0c\u5168\u90e8\u4e2d\u65ad\uff0c\u63a5\u89e6\u5f00\u5173\u4ee5\u53ca\u8d4f\u8d50\u81ea\u4e3e\u65f6\u95f4<\/p>\n - swaps \u663e\u793a\u7684\u662f\u4ea4\u6362\u5206\u533a\u7684\u4f7f\u7528\u60c5\u51b5<\/p>\n - uptime \u8fd9\u4e2a\u6587\u4ef6\u7ed9\u51fa\u81ea\u4ece\u4e0a\u6b21\u7cfb\u7edf\u81ea\u4e3e\u4ee5\u6765\u7684\u79d2\u6570\uff0c\u4ee5\u53ca\u5176\u4e2d\u6709\u591a\u5c11\u79d2\u5904\u4e8e\u7a7a\u95f2<\/p>\n - version \u8fd9\u4e2a\u6587\u4ef6\u53ea\u6709\u4e00\u884c\u5185\u5bb9\uff0c\u8bf4\u660e\u6b63\u5728\u8fd0\u884c\u7684\u5185\u6838\u7248\u672c\u3002\u53ef\u4ee5\u7528\u6807\u51c6\u7684\u7f16\u7a0b\u65b9\u6cd5\u8fdb\u884c\u5206\u6790\u83b7\u5f97\u6240\u9700\u7684\u7cfb\u7edf\u4fe1\u606f\n<\/p>\n <\/p>\n \u83b7\u53d6 CPU \u7684\u4fe1\u606f<\/strong><\/div>\n #!\/usr\/bin\/env Python\r\nfrom __future__ import print_function\r\nfrom collections import OrderedDict\r\nimport pprint\r\ndef CPUinfo():\r\n''' Return the information in \/proc\/CPUinfo\r\nas a dictionary in the following format:\r\nCPU_info['proc0']={...}\r\nCPU_info['proc1']={...}\r\n'''\r\nCPUinfo=OrderedDict()\r\nprocinfo=OrderedDict()\r\nnprocs = 0\r\nwith open('\/proc\/CPUinfo') as f:\r\nfor line in f:\r\nif not line.strip():\r\n# end of one processor\r\nCPUinfo['proc%s' % nprocs] = procinfo\r\nnprocs=nprocs+1\r\n# Reset\r\nprocinfo=OrderedDict()\r\nelse:\r\nif len(line.split(':')) == 2:\r\nprocinfo[line.split(':')[0].strip()] = line.split(':')[1].strip()\r\nelse:\r\nprocinfo[line.split(':')[0].strip()] = ''\r\nreturn CPUinfo\r\nif __name__=='__main__':\r\nCPUinfo = CPUinfo()\r\nfor processor in CPUinfo.keys():\r\nprint(CPUinfo[processor]['model name'])<\/pre>\n |