标签:end socket rip == 编码 服务器 nec put continue
服务器端
import socket
import os
server = socket.socket()
server.bind(( ‘127.0.0.1‘,9999 ))
server.listen() #设置最大连接数
while True:
conn, addr = server.accept()
while True:
data = conn.recv(1024)
if not data:
print(‘客户端已断开‘)
break
cmd_res = os.popen( ( data.decode() ) ).read() #执行系统cmd命令
cmd_res = cmd_res.encode(‘utf-8‘) #执行结果编码
conn.send( str(len(cmd_res)).encode(‘utf-8‘) ) #编码后执行结果内容长度
conn.recv(1024) #解决linux系统粘包问题
conn.send(cmd_res)
server.close()
客户端
import socket
client = socket.socket()
client.connect((‘127.0.0.1‘,9999))
while True:
cmd = input(‘>>:‘).scrip()
if len(cmd) == 0: continue
client.send(cmd.encode(‘utf-8‘)) #发送信息,进行编码
cmd_res_size = client.recv(1024)
cmd_res_size = cmd_res_size.decode()
cmd_res_size = int( cmd_res_size )
received_size = 0
client.send(‘已经准备好接受数据了‘.encode(‘utf-8‘)) #随便发送数据,解决linux系统粘包问题
while received_size < cmd_res_size:
data = client.recv(1024)
received_size += len(data)
data = data.decode()
print(data)
client.close()
标签:end socket rip == 编码 服务器 nec put continue
原文地址:https://www.cnblogs.com/jasonLiu2018/p/10733355.html