views:

44

answers:

3

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 it 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

A: 

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

Yes. Use UDP instead of TCP.

S.Lott
+1  A: 

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.

Daniel Stutzbach
A: 

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.

Anoop