When I run my python server file simplehttpwebsite.py
in the linux shell and I do control+c and run it again I get socket.error: [Errno 98] Address already in use
.
How do I make sure the socket closes down when I do ctrl+c?
simplehttpwebsite.py
#!/usr/bin/env python
import SimpleHTTPServer
import SocketServer
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
server = SocketServer.TCPServer(('0.0.0.0', 8080), Handler)
server.serve_forever()
To stop the server, I just press Ctrl+C.
The SimpleHTTPServer module is a Python module that enables a developer to lay the foundation for developing a web server. However, as sysadmins, we can use the module to serve files from a directory. The module loads and serves any files within the directory on port 8000 by default.
You can easily clear the python socket error 48: Address already in use by specifying an unused port or freeing up the port that the process is bound to. If you get the error on Raspberry Pi, simply restart it to repair.
Here is how you do it
#!/usr/bin/env python
import SimpleHTTPServer
import SocketServer
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
class MyTCPServer(SocketServer.TCPServer):
allow_reuse_address = True
server = MyTCPServer(('0.0.0.0', 8080), Handler)
server.serve_forever()
IMHO this isn't very well documented, and it should definitely be the default.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With