4.20作业
编写远程执行命令的CS架构软件
服务端
import socket
phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
phone.bind(('192.168.1.2',8080))
phone.listen(5)
while 1:
conn,client_addr=phone.accept()
print("客户端的ip和端口:",client_addr)
while 1:
try:
data=conn.recv(1024)
if len(data)==0:
break
print('客户端信息:',data.decode('utf-8'))
conn.send(data.upper())
except Exception:
break
conn.close()
phone.close()
客户端
import socket
phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
phone.connect(('192.168.1.2',8080))
while 1:
msg=input('输入要发送到消息:').strip()
if len(msg)==0:continue
phone.send(msg.encode('utf-8 '))
data=phone.recv(1024)
print(data.decode('utf-8'))
phone.close()