标签:read byte 客户端 end color span connect sock bin
两个脚本模拟远程执行命令
cmd_server.py
import socket import subprocess sk = socket.socket() addess = (‘127.0.0.1‘, 8000) sk.bind(addess) sk.listen(3) conn, addr = sk.accept() print(‘有客户端连接:‘, addr) data = conn.recv(1024) obj = subprocess.Popen(str(data, ‘utf8‘),shell=True,stdout=subprocess,PIPE) cmd_result = obj.stdot.read()
len_result = bytes(str(len(cmd_result))‘utf8‘)
conn.send(len_result)
conn.send(cmd_result)
cmd_client.py
import socket sk =socket.socket() addess = (‘127.0.0.1‘, 8000) sk.connect(addess) while True: inp = input(‘>>:‘) sk.send(bytes(inp, ‘utf8‘))
data = sk.recv(1024)
len_result = int(str(data, ‘utf8‘))
data = bytes()
while len(data) != len_result:
recv = sk.recv(1024)
data += recv
print(str(data, ‘gbk‘))
标签:read byte 客户端 end color span connect sock bin
原文地址:https://www.cnblogs.com/uncle-kay/p/9539344.html