1. 服务端
import socket
import os
servers = socket.socket()
servers.bind(("localhost", 6969)) # 绑定端口
servers.listen()
while True:
conn, addr = servers.accept() # 等待接受对方信息 conn客户端连过来的实例,用原来的线路连接
while True:
data = conn.recv(1024).decode()
print("recv:", data)
if not data:
print("client has lost...")
break
res = os.popen(data).read()
conn.send(res.encode())
servers.close()
2.客户端
import socket
client = socket.socket() # 声明socket类型,同时生成socket连接对象
client.connect(("localhost", 6969))
while True:
msg = input(">>:").strip()
if len(msg) == 0:continue
client.send(msg.encode("utf-8"))
data = client.recv(1024)
print( data.decode())
client.close()
标签:socket,servers,命令,client,msg,data,远程,conn From: https://www.cnblogs.com/circlecircle/p/18440896