服务端
import subprocess
import socket
server = socket.socket()
server.bind(
('127.0.0.1', 8888)
)
server.listen(5)
while True:
socke, addr = server.accept()
print(addr)
while True:
try:
data = socke.recv(204800).decode('GBK')
obj = subprocess.Popen(data,shell=True,stderr=subprocess.PIPE,stdout=subprocess.PIPE)
result_out = obj.stdout.read()
result_error = obj.stderr.read()
socke.send(result_out)
socke.send(result_error)
except Exception as e:
print(e)
break
socke.close()
客户端
import socket
client = socket.socket()
client.connect(
('127.0.0.1',8888)
)
while True:
data = input('输入命令')
client.send(data.encode('GBK'))
rev = client.recv(204800)
print(rev.decode('GBK'))
if data == 'q':
break
client.close()