{"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":"
\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 ip.list\u91cc\u5199server\u7aef\u7684IP\u5730\u5740\uff0c\u6bcf\u884c\u4e00\u4e2a\uff0c\u5982\uff1a<\/p>\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> \u4ee3\u7801\u5df2\u653e\u5230github\u4e0a<\/a>\u3002<\/p>\n \u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.361way.com\/python-socket-cs-command\/4227.html<\/a><\/p>\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
[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
1.1.1.1\r\n2.2.2.2\r\n<\/pre>\n
\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
\n