When sending:
first transmitter header length (dict-> json.dumps sequence number (JSON string) -> bytes-> struct.pack packing)
re-encoded header and the content transmission (dict-> json string -> bytes)
last sent the true content
is received:
received first header length, taken out with a struct (struct.unpack unpacked)
charged content based on the length of the header removed, then decoded, deserialization ( decoding -> json.loads deserialization -> dict )
from deserialized extracted result of access to data information to be described, then the contents of the real data fetch
--------------------------------------tcp_server.py--------------------------------------
# coding:utf-8
import json
import struct
import socket
import subprocess
server = socket.socket()
ip_port = ("127.0.0.1", 8001)
server.bind(ip_port)
server.listen(5)
while 1:
conn, addr = server.accept()
from_client_msg = conn.recv(1024)
if not from_client_msg.decode("utf-8"):
break
print("来自客户端的消息:", from_client_msg.decode("utf-8"))
res = subprocess.Popen(
from_client_msg.decode("utf-8"),
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
cmd_result = res.stderr.read()
if not cmd_result:
cmd_result =res.stdout.read ()
cmd_result_len = len (cmd_result) # data length
headers = { " Data_Size " : cmd_result_len} # header information (dict type)
header_json = json.dumps (headers) # header information (JSON string)
header_json_bytes bytes = (header_json, encoding = " UTF-. 8 " ) # header information (bytes type)
conn.send (struct.pack ( ' I ' , len (header_json_bytes))) # sends the length of the header
conn.send (header_json_bytes) # and then send the header content
conn.sendall (cmd_result) # 最后发送数据
conn.close()
server.close()
--------------------------------------tcp_client.py--------------------------------------
# coding:utf-8
import json
import struct
import socket
client = socket.socket()
ip_port = ("127.0.0.1", 8001)
client.connect(ip_port)
while 1:
cmd = input(">>>: ").strip()
if not cmd: continue
client.send(cmd.encode(" UTF-. 8 " ))
from_server_head_msg_len = client.recv (. 4) # received first header length (bytes Type)
from_server_head_msg_len struct.unpack = ( ' I ' , from_server_head_msg_len) # unpack (tuple type)
Print ( " from the service side message: " , from_server_head_msg_len [0]) # the first element of the tuple is the length of the header
from_server_head_msg = client.recv (from_server_head_msg_len [0]) # receiving header information
from_server_head_msg = from_server_head_msg.decode ( " UTF-. 8 " ) # header decoding information
= json.loads from_server_head_msg (from_server_head_msg) # header information into a type dict
Print ( " message from the service side: " , from_server_head_msg)
from_server_real_msg = client.recv (1024) # receiving actual data
from_server_real_len = len (from_server_real_msg) # Get a received size of the data
the while from_server_head_msg [ ' Data_Size ' ]> from_server_real_len: # judging whether the data reception complete
from_server_real_msg + = client.recv (1024) # again not received data completely received
from_server_real_len = len (from_server_real_msg) # statistical data size received again
Print ( " message from the service side: " , from_server_real_msg.decode ( " GBK " ))
client.close ()