473,325 Members | 2,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 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 2396
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.