python发送tcp数据包_使用python解码tcp数据包

I am trying to decode data received over a tcp connection. The packets are small, no more than 100 bytes. However when there is a lot of them I receive some of the the packets joined together. Is there a way to prevent this. I am using python

I have tried to separate the packets, my source is below. The packets start with STX byte and end with ETX bytes, the byte following the STX is the packet length, (packet lengths less than 5 are invalid) the checksum is the last bytes before the ETX

def decode(data):

while True:

start = data.find(STX)

if start == -1: #no stx in message

pkt = ''

data = ''

break

#stx found , next byte is the length

pktlen = ord(data[1])

#check message ends in ETX (pktken -1) or checksum invalid

if pktlen < 5 or data[pktlen-1] != ETX or checksum_valid(data[start:pktlen]) == False:

print "Invalid Pkt"

data = data[start+1:]

continue

else:

pkt = data[start:pktlen]

data = data[pktlen:]

break

return data , pkt

I use it like this

#process reports

try:

data = sock.recv(256)

except: continue

else:

while data:

data, pkt = decode(data)

if pkt:

process(pkt)

Also if there are multiple packets in the data stream, is it best to return the packets as a collection of lists or just return the first packet

I am not that familiar with python, only C, is this method OK. Any advice would be most appreciated. Thanks in advance

Thanks

解决方案

I would create a class that is responsible for decoding the packets from a stream, like this:

class PacketDecoder(object):

STX = ...

ETX = ...

def __init__(self):

self._stream = ''

def feed(self, buffer):

self._stream += buffer

def decode(self):

'''

Yields packets from the current stream.

'''

while len(self._stream) > 2:

end = self._stream.find(self.ETX)

if end == -1:

break

packet_len = ord(self._stream[1])

packet = self._stream[:end]

if packet_len >= 5 and check_sum_valid(packet):

yield packet

self._stream = self._stream[end+1:]

And then use like this:

decoder = PacketDecoder()

while True:

data = sock.recv(256)

if not data:

# handle lost connection...

decoder.feed(data)

for packet in decoder.decode():

process(packet)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值