TPC/UDP协议
Posted python-by-xiaoma
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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()
以上是关于TPC/UDP协议的主要内容,如果未能解决你的问题,请参考以下文章
Linux-UDP协议(传输层协议)-知名端口-UDP协议特点-UDP协议格式
UDP协议详解(UDP协议特点,UDP协议格式UDP的应用)