码迷,mamicode.com
首页 > 其他好文 > 详细

TPC/UDP协议

时间:2019-01-19 18:48:19      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:打包   socket   head   code   bsp   struct   RoCE   sub   bin   

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()

 

TPC/UDP协议

标签:打包   socket   head   code   bsp   struct   RoCE   sub   bin   

原文地址:https://www.cnblogs.com/python-by-xiaoma/p/10292635.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!