TPC/UDP协议

TPC

客户端

impor socket, struct         # struct 用来打包数据长度,或者报头长度

sk = socket.socket()
sk.bind(('127.0.0.1', 9999))
sk.listen()
coon, adress = sk.accept()
print(adress)
coon.send('你好'.encode('utf-8'))
rec = coon.recv(1024).decode('utf-8')
cmd_rec = subprocess.Popen(rec, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
val = cmd_rec.stdout.read() + cmd_rec.stderr.read()
rec_headlen = struct.pack('i', len(val))
coon.send(rec_headlen)
coon.send(val)
coon.close()

服务器:

import socket,subprocess,struct

sk = socket.socket()
sk.bind(('127.0.0.1', 9999))
sk.listen()
coon, adress = sk.accept()
coon.send('你好'.encode('utf-8'))
rec = coon.recv(1024).decode('utf-8')
cmd_rec = subprocess.Popen(rec, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
val = cmd_rec.stdout.read() + cmd_rec.stderr.read()
rec_headlen = struct.pack('i', len(val))
coon.send(rec_headlen)
coon.send(val)
coon.close()

 

posted @ 2019-01-19 17:44  小马_81  阅读(553)  评论(0编辑  收藏  举报