开发者

Split up python packets?

开发者 https://www.devze.com 2023-01-19 12:03 出处:网络
Is there a way python can distinguish between packets being sent ? e.g. python receives data it process data

Is there a way python can distinguish between packets being sent ? e.g.

python receives data

it process data

clients sends first packet

client sends second packet

python receives data, can i receive the first packet rather then all info in the buffer

I know i can set it up up so i开发者_C百科t sends data i confirm and the client wont send more data it i have confirmed that have a processed the last piece but i'd rather not

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("", 2000))
sock.listen(5)

all the relevant socket data


There are basically two approaches:

  1. At the start of each packet, send an integer specifying how long that packet will be. When you receive data, read the integer first, then read that many more bytes as the first packet.

  2. Send some sort special marker between packets. This only works if you can guarantee that the marker cannot occur within a packet.

As S. Lott points out, you could instead use UDP (which is packet-based) instead of TCP (which is stream-based), but then you give up the other features that TCP provides (retransmission of dropped packets, sequential packets, and congestion control). It's not too hard to write your own code for retransmission, but congestion control is difficult to get right.


Is there a way python can distinguish between packets being sent ?

Yes. Use UDP instead of TCP.


Netstring is a simple serialization format used to send data packets. Each data packet is of the form 'length:data'. http://en.wikipedia.org/wiki/Netstring

Python networking frameworks like twisted has direct support for netstring.

0

精彩评论

暂无评论...
验证码 换一张
取 消