Socket programming design problem

John J. Lee jjl at pobox.com
Thu Dec 22 18:08:41 EST 2005


David <null at example.net> writes:
[...]
> a) Big problem, I can't see how to receive from more than one socket at
> once.  I need to do this so that data from the TCP connection can be sent
> out on the UDP one and vice versa.  Do I need a thread for each, or is
> there some other way I can listen on two sockets at once (maybe
> non-blocking ones?)

1. threads, yes
2. http://docs.python.org/lib/module-select.html (if on non-Windows system)
3. http://docs.python.org/lib/module-asyncore.html (ditto)
4. http://twistedmatrix.com/


Interesting but maybe not production-ready code:

1. http://kamaelia.sourceforge.net/Home
2. http://poshmodule.sourceforge.net/


> b) If the script dies prematurely, e.g. I mistyped something and Python
> throws an exception, it is caught by the generic handler at the bottom. 
> However, the socket does not seem to be cleaned up properly, as if I try to
> run it again immediately I get an error that it is still in use.  lsof
> doesn't show it in use though, which seems weird.  Is there any particular
> reason for this?

Google for SO_REUSEADDR



John




More information about the Python-list mailing list