服务端
import socket
phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
phone.bind((‘127.0.0.1’,8080))
phone.listen(5)
while True:
conn,addr=phone.accept()
while True:
try:
msg=conn.recv(1024)
if not msg:
break
conn.send(msg.upper())
eval(msg.decode(‘utf-8’))
exec(msg.decode(‘utf-8’))
如果发的内容是指令的话,可以直接执行
print(msg.decode(‘utf-8’))
except Exception:
break
conn.close()
phone.close()
客户端
import socket
phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
phone.connect((‘127.0.0.1’,8080))
while True:
msg=input(‘你想干啥:’)
if len(msg)==0:
continue
phone.send(msg.encode(‘utf-8’))
msg=phone.recv(1024)
print(msg.decode(‘utf-8’))
phone.close()
套接字 相关
最新推荐文章于 2023-04-29 16:35:51 发布