469,934 Members | 1,632 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

socket and os.system


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 ?



Aug 3 '05 #1
4 2269
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?
--
Mandus - the only mandus around.
Aug 3 '05 #2
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".

-Peter
Aug 3 '05 #3
In article <Jb********************@powergate.ca>,
Peter Hansen <pe***@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, do**@u.washington.edu
Aug 5 '05 #4
Donn Cave wrote:
Peter Hansen <pe***@engcorp.com> wrote:
PLEASE learn to format your questions more appropriately! Your post is
simply _awful_ to read. ...
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
Aug 5 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by DreJoh | last post: by
3 posts views Thread by Robert A. van Ginkel | last post: by
7 posts views Thread by Colin | last post: by
11 posts views Thread by hazz | last post: by
2 posts views Thread by manasap | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.