1. IPV4服务端
import socket
import subprocess
ip_addr = ('127.0.0.1', 8000)
tcp_server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
tcp_server.bind(ip_addr)
tcp_server.listen(5)
while True:
conn, addr = tcp_server.accept()
while True:
cmd = conn.recv(1024)
if not cmd:
break
if cmd.decode('utf-8') == 'quit':
break
print(cmd.decode('utf-8'))
res = subprocess.Popen(cmd.decode('utf-8'), shell=True,
stdout=subprocess.PIPE, stdin=subprocess.PIPE,
stderr=subprocess.PIPE)
err = res.stderr.read()
out = res.stdout.read()
print(bool(res))
if err:
ref = err
elif not out:
ref = b'12'
else:
ref = out
conn.send(