6

I've created a simple test app (Python 2.6.1) that runs a ThreadingTCPServer, based on the example here. If the client sends a command "bye" I want to shut down the server and exit cleanly from the application. The exit part works OK, but when I try to re-run the app, I get:

socket.error: [Errno 48] Address already in use

I tried the solution given here for setting the socket options but that didn't seem to help. I've tried various ways to close the server down, but always get the same error.

Any idea what I'm doing wrong?

import SocketServer
import socket
import sys
import threading
import time

class RequestHandler(SocketServer.BaseRequestHandler):

    def setup(self):
        print("Connection received from %s" % str(self.client_address))
        self.request.send("Welcome!\n")

    def handle(self):
        while 1:
            data = self.request.recv(1024)
            if (data.strip() == 'bye'):
                 print("Leaving server.")
                 self.finish()
                 self.server.shutdown()
                 # None of these things seem to work either
                 #time.sleep(2)
                 #del self.server.socket
                 #self.server.socket.shutdown(socket.SHUT_WR)
                 #self.server.socket.close()
                 #self.server.server_close()
                 break


    def finish(self):
        self.request.send("Goodbye!  Please come back soon.")

if __name__ == "__main__":
       server = SocketServer.ThreadingTCPServer(("localhost", 9999), RequestHandler)
       # This doesn't seem to help.
       #server.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
       #server.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
       server.serve_forever()
       print("Exiting program.")
Community
  • 1
  • 1
Lynn
  • 1,103
  • 15
  • 29
  • How long do you wait between when the program shuts down and when you start it up again? – David Z Jun 29 '10 at 03:28
  • Just a few seconds. If I wait a minute or two, the TCP socket is released and everything's ok. – Lynn Jun 29 '10 at 04:10
  • Slight update. I was able to get the reuse address bit to work by doing this: SocketServer.ThreadingTCPServer.allow_reuse_address = True But I still don't understand why the server wasn't cleaned up in the first place. – Lynn Jun 29 '10 at 04:11
  • 2
    Because you didn't set `allow_reuse_address` -- as the accepted answer to the SO Q you pointed to clearly shows is necessary! Of course "it didn't seem to help" if you weren't actually *using* it;-). – Alex Martelli Jun 29 '10 at 04:47
  • I was actually using it when I tested it, though :) I just removed that code prior to posting the alternate solution. – Lynn Jun 29 '10 at 18:24

1 Answers1

1

If you have not already found an answer, I believe this may assist...

How to close a socket left open by a killed program?

However, this is the same solution offered by Alex, so perhaps this is just an opportunity to close an old question.

Community
  • 1
  • 1
Mike Pennington
  • 41,899
  • 19
  • 136
  • 174