English 中文(简体)
Packet Queue in Python?
原标题:

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.

最佳回答

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.

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签