0

Hi I'm trying use python's SocketServer.TCPServer The question here doesn't give any solution and closed as unclear

My sample code

class MyTCPHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        # self.request is the TCP socket connected to the client         
        self.data = self.request.recv(1024).strip()                      
        print "{} wrote:".format(self.client_address[0])                 
        print self.data                                                  
        time.sleep(5)                                                    
        # just send back the same data, but upper-cased                  
        self.request.sendall(self.data.upper())                          
        print 'done handing request from {0}'.format(self.client_address[0])

if __name__ == '__main':
    HOST = socket.gethostname()
    PORT = int(sys.argv[1])
    server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
    server.serve_forever()

Problem is when restart the server it says

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

Looked at what server.shutdown() is doing in SocketServer.TCPServer.

It is not closing any socket, so I have tried running this in a different thread an handling the shutdown of the server by calling

server.shutdown()
server.server_close()

Tried changing the order of the calls to shutdown() and server_close()

I still get socket.error: [Errno 98] Address already in use

I used curl command to test.

Using netstat I can see that the port is in TIME_WAIT state

Community
  • 1
  • 1
  • system always keeps socket for awhile and you can't use it. Normally socket can use `s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)` (before `s.bind()`) to solve this problem. – furas Jan 02 '17 at 08:53
  • 1
    Are you sure `shutdown()` and `server_close()` are called and finish properly? Does this example work for you? https://docs.python.org/2/library/socketserver.html#asynchronous-mixins – ppasler Jan 02 '17 at 08:54
  • Possible duplicate of [python socket.error: \[Errno 98\] Address already in use](http://stackoverflow.com/questions/17780291/python-socket-error-errno-98-address-already-in-use) – Krishnadas PC Jan 02 '17 at 08:58
  • thanks @furas i tried doing this `server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler, False) server.allow_reuse_address = True server.server_bind() server.server_activate() ` it works – sohail yunus Jan 02 '17 at 09:19
  • @ppasler that example uses 0 as port which takes any arbitrary unused port i tried changing it to a fixed port, in which case I'm getting the error address already in use – sohail yunus Jan 02 '17 at 09:24
  • This is strange, I changed the the port from `0` to `1337` and restarted the script several time - the port was always free (it was closed correctly). – ppasler Jan 02 '17 at 09:28
  • @ppasler yeah it is strange, i my system `1337` is used by some other program, so i changed it to 1338 and tried running the script couple of times, it fails to acquire the port – sohail yunus Jan 02 '17 at 09:35
  • Thanks @furas that solves the problem, i also found in python docs[link](https://docs.python.org/2/library/socket.html) – sohail yunus Jan 02 '17 at 16:07

0 Answers0