Passing Python socket objects to child processes in Windows

dan.jakubiec at gmail.com dan.jakubiec at gmail.com
Mon Jan 22 23:39:52 EST 2007


I'm trying to write a Python app which accepts a socket connection and
then spawns another Python process to handle it.  I need it to run
under both Linux and Windows.  I have it working under Linux, but am
having problems with the Windows implementation.

My app uses the subprocess.Popen class to spawn the child process.  The
first problem I ran into was that Windows Python won't let you pass a
socket object via the Popen stdin argument (it results in a "bad file
descriptor" error).  File object work okay, but sockets seem to be
handled differently.

After some searching, I found something of a work-around in this post:
http://mail.python.org/pipermail/python-list/2004-October/287742.html.

This workaround uses pywin32 to duplicate the original socket handle
via the Win32 API.  It almost works, in the sense that I now have a
file stream in my child process which corresponds to the original
socket object in the parent.  But I can't find a way to convert the
resulting Windows file handle into a Python socket object (there does
not appear to be a Windows-equivalent of the socket.fromfd() function).
 My app needs the socket object in order to control TCP stuff.

So I'm a bit stuck.  Is there a better way (or any way?) to pass a
socket to a child process in Windows, and to actually end up with a
Python socket object in the child process?

Thanks in advance for any help.

--
Dan Jakubiec




More information about the Python-list mailing list