socket and os.system

Donn Cave donn at u.washington.edu
Fri Aug 5 14:29:11 EDT 2005


In article <JbqdncA4KumVhGzfRVn-ow at powergate.ca>,
 Peter Hansen <peter at engcorp.com> wrote:

> mfaujour wrote:
> > I HAVE THIS PYTHON PROGRAMM:
> [snip]
> 
> > socket.error: (98, 'Address already in use')
> >                                                                      
> > DOES SOMEONE HAS AN IDEA ?
> 
> PLEASE learn to format your questions more appropriately!  Your post is 
> simply _awful_ to read.  At the very least, ALL CAPS is considered to be 
> "shouting", though I can see why you had to use them since it would have 
> been impossible to see the questions amongst all the code.
> 
> In any case, assuming I've been able to guess at the specific problem 
> based on the above lines, which isn't certain, you need to use a line 
> something like this in your code to allow your server socket to bind to 
> an address that was previously in use:
> 
> server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
> 
> For more background, I suggest a Google search on "python so_reuseaddr".

For heaven's sake, it wasn't that hard to read.  Of course
the upper case text was an unpardonable violation of people's
tender sensibilities, but in this case it does have the virtue
of a strong visible distinction between his code and his comments.
The good thing is that he did provide example that clearly
illustrates the problem.

Which is not really that he can't reuse the socket address.
I mean, it's usually good to take care of that, but ordinarily
for reasons having to do with shutdown latency.  In the present
case, his application is holding the socket open from a fork
that inherited it by accident.

I think the current stock answer is "use the subprocess module."
If that's not helpful, either because it doesn't provide any
feature that allows you to close a descriptor in a fork (I seem
to recall it does), or it isn't supported in your version of
Python (< 2.4), then you have your choice of two slightly awkward
solutions:

 1. fcntl F_SETFD FD_CLOEXEC (see man 2 fcntl)
 2. implement your own spawn command (see os.py's
    spawnv()) and close the socket FD in the fork.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list