Passing Python socket objects to child processes in Windows

D

dan.jakubiec

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top