{"id":111631,"date":"2024-12-06T10:57:12","date_gmt":"2024-12-06T02:57:12","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=111631"},"modified":"2024-12-06T10:57:12","modified_gmt":"2024-12-06T02:57:12","slug":"python-c-s","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-c-s.html","title":{"rendered":"python\u5165\u95e8\u7b80\u5355\u7684 c\/s\u8fdc\u7a0b\u64cd\u4f5c"},"content":{"rendered":"
\u5b9e\u73b0\u76ee\u6807\uff1a<\/strong><\/div>\n

\u901a\u8fc7\u63a7\u5236\u7aef\uff0c\u53ef\u4ee5\u5b9e\u73b0N\u53f0\u4e3b\u673a\u6267\u884c\u540c\u4e00\u64cd\u4f5c\u3002<\/p>\n

\u5177\u4f53\u4ee3\u7801\u5982\u4e0b<\/strong>\uff1a<\/p>\n

1\u3001\u63a7\u5236\u7aef\u4ee3\u7801<\/strong><\/span><\/div>\n
[root@361way tmp]# vim client.py\r\n#!\/usr\/bin\/env python\r\nimport socket,os,sys\r\nost=sys.argv[1]\r\n\u25bdort=4567\r\nfor host in os.popen('cat ip.list').readlines():\r\nprint host\r\ns=socket.socket(socket.AF_INET,socket.SOCK_STREAM)\r\ns.connect((host,port))\r\nwhile 1:\r\ndata=sys.argv[2]\r\nif not data:\r\nbreak\r\ns.sendall(data)\r\ndata=s.recv(1024)\r\nif not data:\r\nbreak\r\nprint data\r\ns.close()\r\n<\/pre>\n
2\u3001\u88ab\u63a7\u5236\u7aef\u4ee3\u7801<\/strong><\/span><\/div>\n
[root@localhost tmp]# vim server.py\r\n#!\/usr\/bin\/env python\r\nimport socket,os\r\nhost=''\r\nport=4567\r\ns=socket.socket(socket.AF_INET,socket.SOCK_STREAM)\r\ns.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)\r\ns.bind((host,port))\r\ns.listen(1)\r\nprint \"Server is running on port %d; press ctrl-c to terminate.\" % port\r\nwhile 1:\r\nclientsock,clientaddr=s.accept()\r\nprint \"connect from %s\" % str(clientaddr)\r\nclientfile=clientsock.makefile('rw',0)\r\ndata=clientsock.recv(1024)\r\ncommand=os.popen(data).read()\r\nclientfile.write(\"%s\" % command)\r\nclientfile.close()\r\nclientsock.close()\r\n<\/pre>\n
3\u3001\u64cd\u4f5c\u65b9\u6cd5<\/strong><\/span><\/div>\n

ip.list\u91cc\u5199server\u7aef\u7684IP\u5730\u5740\uff0c\u6bcf\u884c\u4e00\u4e2a\uff0c\u5982\uff1a<\/p>\n

1.1.1.1\r\n2.2.2.2\r\n<\/pre>\n

\u4f7f\u7528\u65b9\u6cd5\uff0c\u5c06server.py\u90e8\u7f72\u4e8e\u76ee\u6807\u4e3b\u673a\u4e0a\uff0c\u6267\u884cserver.py\uff1b\u5ba2\u6237\u7aef\u6267\u884cpython client.py ip.list \"command<\/span>
\n\"\u5373\u53ef\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n

[root@361way tmp]# python client.py ip.list ifconfig\r\n192.168.0.106\r\neth0 Link encap:Ethernet HWaddr 52:54:00:D8:F3:E7\r\ninet addr:192.168.0.106 Bcast:192.168.0.255 Mask:255.255.255.0\r\ninet6 addr: fe80::5054:ff:fed8:f3e7\/64 Scope:Link\r\nUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1\r\nRX packets:627 errors:0 dropped:0 overruns:0 frame:0\r\nTX packets:262 errors:0 dropped:0 overruns:0 carrier:0\r\ncollisions:0 txqueuelen:1000\r\nRX bytes:71041 (69.3 Kb) TX bytes:55281 (53.9 Kb)\r\n192.168.0.110\r\neth0 Link encap:Ethernet HWaddr 52:54:00:D7:F8:E7\r\ninet addr:192.168.0.110 Bcast:192.168.0.255 Mask:255.255.255.0\r\ninet6 addr: fe80::5054:ff:fed7:f8e7\/64 Scope:Link\r\nUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1\r\nRX packets:770 errors:0 dropped:0 overruns:0 frame:0\r\nTX packets:438 errors:0 dropped:0 overruns:0 carrier:0\r\ncollisions:0 txqueuelen:1000\r\nRX bytes:87223 (85.1 KiB) TX bytes:69993 (68.3 KiB)\r\n<\/pre>\n

\u4ee3\u7801\u5df2\u653e\u5230github\u4e0a<\/a>\u3002<\/p>\n

\n

\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.361way.com\/python-socket-cs-command\/4227.html<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttps:\/\/lrxjmw.cn\/python-c-s.html<\/a>\u7f16\u8f91\uff1a\u5f20\u96c4\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u5b9e\u73b0\u76ee\u6807\uff1a \u901a\u8fc7\u63a7\u5236\u7aef\uff0c\u53ef\u4ee5\u5b9e\u73b0N\u53f0\u4e3b\u673a\u6267\u884c\u540c\u4e00\u64cd\u4f5c\u3002 \u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a 1\u3001\u63a7\u5236\u7aef\u4ee3\u7801 [root@361wa […]<\/p>\n","protected":false},"author":643,"featured_media":111652,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-111631","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/111631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=111631"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/111631\/revisions"}],"predecessor-version":[{"id":111683,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/111631\/revisions\/111683"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/111652"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=111631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=111631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=111631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}