标签:.com opened bin indicator ddr str list 数据 pac
1 import socket 2 import struct 3 import json 4 import subprocess 5 phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #买手机 6 phone.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) #就是它,在bind前加 7 phone.bind((‘127.0.0.1‘,8080)) #插入卡 8 phone.listen(5) #开机 9 while True: #链接循环 10 conn,addr=phone.accept() 11 print(‘client :‘,addr) 12 while True: #通讯循环 13 try: 14 cmd=conn.recv(1024) 15 if not cmd:break #针对linux,客户端断开链接的异常处理 16 print(‘from client msg :%s‘ %cmd) 17 18 res=subprocess.Popen(cmd.decode(‘utf-8‘), 19 shell=True, 20 stdout=subprocess.PIPE, 21 stderr=subprocess.PIPE) 22 23 err=res.stderr.read() 24 if err: 25 back_msg=err 26 else: 27 back_msg=res.stdout.read() 28 #第一阶段:制作报头 29 head_dic={ 30 ‘data_size‘:len(back_msg) 31 } 32 head_json=json.dumps(head_dic) 33 head_bytes=head_json.encode(‘utf-8‘) 34 35 #第二阶段:发送报头的长度 36 conn.send(struct.pack(‘i‘,len(head_bytes))) 37 38 #第三阶段:发报头 39 conn.send(head_bytes) 40 #第四阶段:发真实数据 41 conn.sendall(back_msg) 42 43 44 except Exception: 45 break 46 conn.close() 47 phone.close()
1 import socket 2 import struct 3 import json 4 client=socket.socket(socket.AF_INET,socket.SOCK_STREAM) 5 client.connect((‘127.0.0.1‘,8080)) 6 7 while True: 8 cmd=input(‘>>: ‘).strip() 9 if not cmd:continue 10 11 client.send(cmd.encode(‘utf-8‘)) 12 13 #收报头的长度 14 head=client.recv(4) 15 head_size=struct.unpack(‘i‘,head)[0] 16 17 #收报头(根据报头长度) 18 head_bytes=client.recv(head_size) 19 head_json=head_bytes.decode(‘utf-8‘) 20 head_dic=json.loads(head_json) 21 ‘‘‘ 22 head_dic={ 23 ‘data_size‘:len(back_msg) 24 } 25 ‘‘‘ 26 data_size=head_dic[‘data_size‘] #取出真实数据的长度 27 28 #收真实的数据 29 recv_size=0 30 recv_bytes=b‘‘ 31 while recv_size < data_size: 32 res=client.recv(1024) 33 recv_bytes+=res 34 recv_size+=len(res) 35 print(recv_bytes.decode(‘gbk‘))
标签:.com opened bin indicator ddr str list 数据 pac
原文地址:http://www.cnblogs.com/xp1005/p/6529896.html