473,396 Members | 2,113 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,396 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 13887
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.