标签:utf8 shel pack imp comm 解决 思路 net pipe
import socket
import struct
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
addr = ('127.0.0.1',8000)
client.connect(addr)
while True:
# 发送消息
msg = input("请输入你要讲的话>>>>")
client.send(bytes(msg.encode('utf8')))
# 接受消息
leng = client.recv(4) # 提前接受客户端传输数据的大小
date_lens = struct.unpack('i',leng)[0] # 转化
data = client.recv(date_lens) # data_lens为服务端即将发来数据的大小
print(data.decode('gbk'))
import socket
import struct
import subprocess
service = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
addr = ('127.0.0.1',8000)
service.bind(addr)
service.listen(5)
conn,addr = service.accept()
while True:
# 接受消息
client_msg = conn.recv(1024)
print(client_msg)
common = client_msg.decode('utf8')
print(common)
obj = subprocess.Popen(common,
shell=True,
stderr=subprocess.PIPE,
stdout = subprocess.PIPE
)
stderr = obj.stderr.read()
stdout = obj.stdout.read()
msg_len = len(stderr) + len(stdout) # 传输数据的长度
one = struct.pack('i',msg_len) # 转化
conn.send(one) # 告知client端 本次传入数据的大小
conn.send(stdout+stderr)
客户端
import socket
client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
addr = ('127.0.0.1',8000)
msg = input("请输入你的内容>>>>")
client.sendto(bytes(msg.encode('utf8')),addr) # 传输到服务端
data,addr = client.recvfrom(1024) # 接受消息
print(data.decode('utf8'))
服务端
import socket
service = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
addr = ('127.0.0.1',8000)
service.bind(addr)
date,addr = service.recvfrom(1024) # 接受消息
print(date.decode('utf8'))
service.sendto(date.upper(),addr) # 发送消息
标签:utf8 shel pack imp comm 解决 思路 net pipe
原文地址:https://www.cnblogs.com/plf-Jack/p/11105244.html