472,982 Members | 2,223 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,982 software developers and data experts.

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 2378
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DreJoh | last post by:
I've read many articles on the subject and the majority of them give the same solution that's in article 821625 on the MSDN website. I'm using the following code and when a the client disconnects...
3
by: Robert A. van Ginkel | last post by:
Hello Fellow Developer, I use the System.Net.Sockets to send/receive data (no tcpclient/tcplistener), I made a receivethread in my wrapper, the receivethread loops/sleeps while waiting for data...
4
by: zbcong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting for a...
7
by: Colin | last post by:
I'm writing a little console socket server but I'm having some difficulty. Can I ask your advice - where is the best place to get some help on that topic? It would be nice if some people who knew...
11
by: hazz | last post by:
smtpClient.Send(message) is causing me problems as per specifics in the trace below. Email is sent but not without this error typically upon sending the second email, but sometimes when running...
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
0
by: Buddy Home | last post by:
There is two examples of code. Example 1. Send and Receive within the same process. Put this code in a console app called SendAndReceive and run the code. using System; using...
2
by: manasap | last post by:
Hi all! I've written a server and a client application using asynchronous sockets.The client sends data packets for every 7 seconds.The server receives the packets. This process proceeds...
1
by: keksy | last post by:
Hi every1, I am writing a small client/server application and in it I want to send an image asynchronous from the client to the server through a TCP socket. I found an example code on the MSDN...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.