socket and os.system

M

mfaujour

I HAVE THIS PYTHON PROGRAMM:


[test]$ cat socpb.py
import BaseHTTPServer, SocketServer, os, socket, threading


# the server initialisation
server = SocketServer.TCPServer((socket.gethostname(),
8088),BaseHTTPServer.BaseHTTPRequestHandler)
t=threading.Thread(target=server.serve_forever)
t.setDaemon(1)
t.start()


# the command
os.system("sleep 3600")
[test]$






I LAUNCH IT:


[test]$ python socpb.py


[1]+ Stopped python socpb.py
[test]$ bg
[1]+ python socpb.py &






I LOOK THE PORT IT USE --> OK:


[test]$ netstat -taupe | grep 8088
tcp 0 0 test:8088 *:* LISTEN mik 32481 14092/python






I KILL THE PYTHON PROGRAMM (OR IT STOP BY ANOTHER WAY):


[test]$ kill 14092
[test]$
[1]+ Terminated python socpb.py






I LOOK THE PORT --> NOK, I EXPECTED THAT THE PORT WAS FREED BUT SLEEP
IS LISTENING AND IF I TRY TO LAUNCH MY PROGRAMM, IT FAIL:


[test]$ netstat -taupe | grep 8088
tcp 0 0 test:8088 *:* LISTEN mik 32481 14094/sleep
[test]$ python socpb.py
Traceback (most recent call last):
File "socpb.py", line 4, in ?
server = SocketServer.TCPServer((socket.gethostname(),
8088),BaseHTTPServer.BaseHTTPRequestHandler)
File "/usr/local/lib/python2.3/SocketServer.py", line 330, in __init__
self.server_bind()
File "/usr/local/lib/python2.3/SocketServer.py", line 341, in
server_bind
self.socket.bind(self.server_address)
File "<string>", line 1, in bind




socket.error: (98, 'Address already in use')




IF I KILL THE SLEEP, ALL COME BACK FINE:


[test]$ kill 14094
[test]$ netstat -taupe | grep 8088
[test]$ python socpb.py








I HAVE SEARCHED ON THE WEB HOW TO LAUNCH THE SLEEP WHITHOUT IT GET THE
PORT IN THE ENVIRONNEMENT BUT NOTHING...


I HAVE A WORKAROUND BY USING rsh BUT I AM NOT VERY PLEASED WITH THAT.


DOES SOMEONE HAS AN IDEA ?
 
M

Mandus

Wed, 03 Aug 2005 15:46:50 -0000 skrev mfaujour:
I HAVE THIS PYTHON PROGRAMM:
[snip]

welcome to usenet!

Maybe you get an answer if you doesn't shout that much. Or maybe you
just have a problem with you Caps Lock?
 
P

Peter Hansen

mfaujour said:
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".

-Peter
 
D

Donn Cave

Peter Hansen said:
mfaujour said:
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, (e-mail address removed)
 
P

Peter Hansen

Donn said:
For heaven's sake, it wasn't that hard to read.

My apologies to the original poster. I did find it very hard to read
with the very brief time I put into reading posts which don't fit Usenet
conventions. Upon rereading it now a third time I can parse it a little
better, and my outburst does seem harsh. (Normally I quickly hit "k" on
such posts and carry on, but the apparent SO_REUSEADDR symptom caught my
eye milliseconds before I did so.)
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.

So I'm doubly wrong and apologize for not having tried hard enough to
read it to avoid giving an unhelpful answer.

-putting-ice-on-his-slapped-wrists-ly y'rs,
Peter
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,194
Latest member
KarriWhitt

Latest Threads

Top