Note that there are some explanatory texts on larger screens.

plurals
  1. POStart a TCPServer with ThreadingMixIn again in code directly after shutting it down. (Gives `Address already in use`)
    primarykey
    data
    text
    <p>I try to program a TCPServer with threads (ThreadingMixIn) in Python. The problem is that I can't shut it down properly as I get the <code>socket.error: [Errno 48] Address already in use</code> when I try to run it again. This is a minimal example of the python code that triggers the problem:</p> <pre><code>import socket import threading import SocketServer class FakeNetio230aHandler(SocketServer.BaseRequestHandler): def send(self,message): self.request.send(message+N_LINE_ENDING) def handle(self): self.request.send("Hello\n") class FakeNetio230a(SocketServer.ThreadingMixIn, SocketServer.TCPServer): def __init__(self, server_address, RequestHandlerClass): self.allow_reuse_address = True SocketServer.TCPServer.__init__(self, server_address, RequestHandlerClass) if __name__ == '__main__': for i in range(2): fake_server = FakeNetio230a(("", 1234), FakeNetio230aHandler) server_thread = threading.Thread(target=fake_server.serve_forever) server_thread.setDaemon(True) server_thread.start() # might add some client connection here fake_server.shutdown() </code></pre> <p>All the main code should do is to start the server, shut it down and run it again. But it triggers the error stated above because the socket has not been released after the first shutdown.</p> <p>I thought that setting <code>self.allow_reuse_address = True</code> could solve the problem, but that did not work. When the python program finishes I can run it again straight away and it can start the server once (but again not twice).<br> However the problem is gone when I randomize the port (replace <code>1234</code> by <code>1234+i</code> for example) as no other server is listening on that address.</p> <p>There is a similar SO Q <a href="https://stackoverflow.com/questions/3137640">Shutting down gracefully from ThreadingTCPServer</a> but the solution (set <code>allow_reuse_address</code> to <code>True</code> does not work for my code and I don't use ThreadingTCPServer).</p> <p><strong>How do I have to modify my code in order to be able to start the server twice in my code?</strong></p> <p>Some more information: The reason why I'm doing this is that I want to run some unit tests for my python project. This requires to provide a (fake) server that my software should to connect to.</p> <p><strong>edit</strong>:<br> I just found the most correct answer to my problem: I have to add <code>fake_server.server_close()</code> at the end of my main execution code (right after <code>fake_server.shutdown()</code>). I found it in the source file of the <code>TCPServer</code> implementation. All it does is <code>self.socket.close()</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload