473,322 Members | 1,690 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,322 software developers and data experts.

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

I have a socket server like below which I want to exit when
it's out of data. If I interrupt the client, I'll get a
broken pipe on the server side, and after a Ctrl-C, I can
restart the server again, but if I let it run out of data,
and exit via handle_error as can be seen below, I will get
a socket.error: (98, 'Address already in use') when I try
to restart the server. (Unless I wait a minute or so, or
use another port.) I feel like I'm waving a dead chicken
here. I can't seem to find any proper description on how to
handle this in the docs for SocketServer or socket, and my
futile experiments seem to lead nowhere... Neither Nutshell
nor the Essential Reference helped me solve this.

I'm using Python 2.2.3 on Linux. Why doesn't my socket
get released by the OS when I exit via my handle_error?

class MyRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
while True:
[snip]
try:
[snip]
except [snip]:
# Done!
# Uncommenting the next line makes no difference
# self.request.close()
sys.exit()

class ExitableSocketServer(SocketServer.TCPServer):
def handle_error(self, request, client_address):
exc_info = sys.exc_info()
if exc_info and isinstance(exc_info[1], SystemExit):
request.shutdown(2)
request.close()
del request
self.socket.shutdown(2)
self.socket.close()
del self.socket
sys.exit()
SocketServer.TCPServer.handle_error(self, request, client_address)

....

server = ExitableSocketServer(('', port), MyRequestHandler)
server.serve_forever()
Jul 19 '05 #1
3 13877
On Mon, 30 May 2005 20:04:02 +0200, Magnus Lycka <ly***@carmen.se>
declaimed the following in comp.lang.python:

I'm using Python 2.2.3 on Linux. Why doesn't my socket
get released by the OS when I exit via my handle_error?
Not a Python problem...

Normal socket behavior IS to lock out for some period of time.

Check the options available when creating the socket. (I'm not
heavily familiar with socket programming, hence the vagueness) Some
option for "Reuse Address"...

SO_REUSEADDR is the constant in socket.h... And appears to be
visible in socket.py

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 19 '05 #2
Magnus Lyckå wrote:
Why doesn't my socket
get released by the OS when I exit via my handle_error?


Hi Magnus,

I wrote about this at
http://www.dalkescientific.com/writi...ng_xmlrpc.html

The reason for it is described at
http://hea-www.harvard.edu/~fine/Tech/addrinuse.html

You can set the class variable "allow_reuse_address = True" in
your derived ExitableSocketServer to get the behaviour you
expect, at the expense of some problems mentioned in the
above URL.

Andrew
da***@dalkescientific.com

Jul 19 '05 #3
Thank's a lot Andrew and Dennis.

Andrew Dalke wrote:
You can set the class variable "allow_reuse_address = True" in
your derived ExitableSocketServer to get the behaviour you
expect, at the expense of some problems mentioned in the
above URL.


Ok. I added a STOP message to my protocol, so now the client
shuts down the socket first (when it has read the STOP), and
then it seems to work much better. What I did, in case the
client gets behind, was this in my handle method: I send a STOP
message repeatedly with a delay. When the client gets the first
one, it will close the socket, and I will get an exception the
next time I try to send. This seems to work well, without
(I hope) introducing any new potential problems.

....
delay = 1
print "No more data to serve. Send STOP message."
print "Waiting for client to close socket."
while 1:
try:
self.request.send("STOP\n")
time.sleep(delay)
print '.'
finally:
sys.exit()
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Johannes Eble | last post by:
Hello Python community, I am trying the echo-client and echo-server examples in Chapter 10, "Programming Python" by Mark Lutz. It is probably the most simple sockets sample: A socket server just...
6
by: Rune | last post by:
Hi, I've written a very simple 'kill-server' to help me shut down processes through Telnet or HTTP. The kill-server is a function and is launched as a thread. I use the module socket.py on Python...
0
by: Ajay | last post by:
hi! my application is throwing a bad socket error raise error(EBADF, 'Bad file descriptor') socket.error: (9, 'Bad file descriptor') basically i have the following interchange application ...
0
by: Eric.Hillmuth | last post by:
I'm just getting started with postgresql and things are going pretty well. However, I seem to be having trouble connecting to my database via TCP/IP. I'm assuming that my problems are related to...
11
by: Tor Erik | last post by:
Hi, The reason is that my application does about 16 connects and data transfers per second, to the same 16 remote hosts. After approx 200 secs there are 4000 sockets waiting to be garbage...
0
by: palmem | last post by:
I am trying to write a simple FTP server in order to learn about sockets This is my first time trying sockets This code should take a connection on port 8110, dump it to a client "thread" (not...
0
by: sivasankarmathuraju | last post by:
Hi! I am using: Java 2 SDK 1.4.0 Standard Edition on Windows XP I have one java client application and one gateway that is in C language runing on the same machine. The server creates a...
1
by: ntech | last post by:
hello, i got a problem while restarting postgresql, i removed postmaste.pid and even the temporary sock file and while restarting postgres, i got the following error: LOG: could not bind IPv6...
1
by: khalid galal | last post by:
Hi, i am using NetBeans IDE 6.0.1 and i created a server project and a client project, first i run the server that listens to the port "8221" and waits for clients accessing that port, and when i run...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: 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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.