day 26(作业)
1
TCP连接的建立(三次握手)
TCP连接的释放(四次挥手)
02
# 服务端
import socket
import subprocess
server=socket.socket()
server.bind(('127.0.0.1',9608)) # 输入ip+port,主机地址,服务地址
server.listen(5)
while True:
conn,addr=server.accept() # conn相当于服务端往客户端挖的管道
print('client-id;',addr)
while True:
try:
data=conn.recv(1024).decode('utf-8')
print('client-msg;',data)
if not data:
continue
if data=='q':
break
res=subprocess.Popen(data,stdout=subprocess.PIPE, stderr=subprocess.PIPE,shell=True)
err=res.stderr.read()
out=res.stdout.read()
if err:
print(err)
gs=err
else:
print(out)
gs=out
msg=f'''信息;{data}
错误或结果;{gs}'''
conn.send(msg.encode('utf-8'))
except Exception as e:
print(e)
break
conn.close()
cl=input('是否结束;')
if cl=='q':
break
# 客户端
import socket
client = socket.socket()
client.connect(('127.0.0.1', 9608))
while True:
send_msg = input('client--server;')
client.send(send_msg.encode('utf-8'))
if send_msg == 'q':
break
data = client.recv(1024).decode('utf-8')
print('server--msg;',data)
client.close()