SocketServer.ThreadingTCPServer - Cannot bind to address after program restart
In this particular case, .setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
may be called from the TCPServer class when the allow_reuse_address
option is set. So I was able to solve it as follows:
httpd = SocketServer.ThreadingTCPServer(('localhost', port), CustomHandler, False) # Do not automatically bind
httpd.allow_reuse_address = True # Prevent 'cannot bind to address' errors on restart
httpd.server_bind() # Manually bind, to support allow_reuse_address
httpd.server_activate() # (see above comment)
httpd.serve_forever()
Anyway, thought this might be useful. The solution will differ slightly in Python 3.0
Cannot bind to address after socket program crashes
Use .setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
on your listening socket.
A search for those terms will net you many explanations for why this is necessary. Basically, after your first program closes down, the OS keeps the previous listening socket around in a shutdown state for TIME_WAIT
time. SO_REUSEADDR
says that you want to use the same listening port regardless.
Can't reuse socket in SocketServer.TCPServer
Adding a self.httpd.server_close()
after self.httpd.shutdown()
did the trick.
Start a TCPServer with ThreadingMixIn again in code directly after shutting it down. (Gives `Address already in use`)
Somehow, fake_server
doesn't unbind when you assign to it (in first line in for
statement).
To fix that, just remove fake_server
at the end of loop:
del fake_server # force server to unbind
python TCPServer address already in use but I close the server and I use `allow_reuse_address`
Thanks to the other answers, I figured it out. allow_reuse_address
should be on the class, not on the instance:
SocketServer.TCPServer.allow_reuse_address = True
httpd = SocketServer.TCPServer(("", PORT), MyRequestHandler)
I'm still not sure why closing the socket didn't free it up for the next run of the server, though.
Problem running tcp server via systemd service getting address already in use
Address already in use will mostly occur for 2 reasons:
The address is actually in use by another running process. You can check that by running
netstat -pnutl
The address is used by a previous bind attempt made by the same process and didn't have time to close properly. This has a simple solution by turning on the socket option
SO_REUSEADDR
:s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Python [Errno 98] Address already in use
Yes, it is intended. Here you can read detailed explanation. It is possible to override this behavior by setting SO_REUSEADDR option on a socket. For example:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Shutting down gracefully from ThreadingTCPServer
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.
Related Topics
How to Take a Screenshot/Image of a Website Using Python
How to Limit Memory Usage Within a Python Process
How Do Rpy2, Pyrserve and Pyper Compare
How to Dereference Variable Id'S
Display Image as Grayscale Using Matplotlib
Making a String Out of a String and an Integer in Python
Plotting a 3D Cube, a Sphere and a Vector in Matplotlib
Why Isn't Pycharm's Autocomplete Working for Libraries I Install
Converting JSON String to Dictionary Not List
How to Detect Whether a Python Variable Is a Function
How to Load All Entries in an Infinite Scroll at Once to Parse the HTML in Python
How to Search Directories and Find Files That Match Regex
Placing Custom Images in a Plot Window--As Custom Data Markers or to Annotate Those Markers
Sort List of Lists Ascending and Then Descending