标签:
1.执行CMD命令
SERVER: __author__ = ‘Alex‘ #coding:utf-8 import socket import subprocess ip_port = (‘127.0.0.1‘,8888) sk = socket.socket() sk.bind(ip_port) sk.listen(5) print ("服务端启动...") while True: conn,addr = sk.accept() while True: try: recv_data = conn.recv(1024) except Exception: break print (str(recv_data,"utf8")) cmd = str(recv_data,"utf8").strip() cmd_call = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE) print (type(cmd_call)) cmd_ret = cmd_call.stdout.read() print (type(cmd_ret)) print (len(cmd_ret)) if len(cmd_ret) == 0: cmd_ret = b‘no output‘ conn.send(cmd_ret) conn.close() CLIENT: __author__ = ‘Alex‘ #coding:utf-8 import socket ip_port = (‘127.0.0.1‘,8888) sk = socket.socket() sk.connect(ip_port) print ("客户端启动...") while True: send_data = input(">>>>>").strip() if len(send_data) == 0: continue if send_data == ‘q‘: break sk.sendall(bytes(send_data,‘gbk‘)) recv_data = sk.recv(1024) print (str(recv_data,"gbk")) if send_data =="exit": break sk.close()
标签:
原文地址:http://www.cnblogs.com/python-study/p/5790587.html