views:

95

answers:

1

Hi, is there any way to queue packets to a socket in Python? I've been looking for something like the libipq library, but can't find anything equivalent.

Here's what I'm trying to accomplish:

  1. Create tcp socket connection between server and client (both under my control).
  2. Try transmitting data (waiting for connection to fault -- e.g. client loses connectivity because of shutting laptop)
  3. Catch SocketException and hold on to the data that was trying to be sent while keeping the remaining data waiting (in a Queue?)
  4. Enter in to loop trying to reconnect (assuming that success is inevitable)
  5. Create new socket upon success
  6. Resume data transmission

Any suggestions? Can Twisted do this? Do I need to involve pcapy? Should I do this (sockets, queues, etc) in C and use Boost to make hybrid code?

Thanks in advance.


Edit 1:

Response to Nick:

I left out the fact that the data I'll be transmitting will be generalized and unending -- think of this app sitting under an ssh session (i'm not in any way trying to peek into the packets). So, the transmission will be bilateral. I want to be able to go from the office to my home (closing my laptop in between), open the laptop at home and continue in my session seamlessly. (I know SCREEN exists). This might lead you to wonder how it'd work without proxies. It won't, I just haven't explained that design. Blockquote

With the added context, I should also say I won't have to catch a SocketException on the server side since that machine will be (or assume to be) fixed. When the client figures out that it's got connectivity again, it'll just re-connect to the server.

A: 

I suggest you implement an application protocol, so when the client receives data it acknowledges it to the server, with a serial number for each bit of data.

The server then keeps note of which serial number the client needs next and sends it. If the connection breaks then the server re-makes it.

The data could be buffered anyway you like, but I'd keep it simple to start with and use a deque probably which is a bit more efficient than a list for taking data of the start and end.

I'd probably create a Packet object which has a serial number attribute, and a way to serialize and unserialize it and its data to a byte stream (eg using netstrings). You would then store these Packets in your deque. You would pop the packet off the deque and whenever you got an acknowledgement from the client and send the next one.

Such protocols have lots of corner cases, timeouts needed and are generally annoying to debug. Look at the source code for a SMTP server some day if you want some idea!

As for sockets, you could easily implement this with normal python sockets and see how it goes. If you plan to have lots of clients at once then twisted will be a good idea.

Nick Craig-Wood
I left out the fact that the data I'll be transmitting will be generalized and unending -- think of this app sitting under an ssh session (i'm not in any way trying to peek into the packets). So, the transmission will be bilateral.I want to be able to go from the office to my home (closing my laptop in between), open the laptop at home and continue in my session seamlessly. (I know SCREEN exists).This might lead you to wonder how it'd work without proxies. It won't, I just haven't explained that design.Thank you though Nick!
Kevin