9

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()
2
  • 1
    Possible duplicate: stackoverflow.com/questions/4465959/… Commented May 16, 2012 at 7:52
  • 1
    @freakish: same cause yes, but different solution within the SocketServer framework Commented May 16, 2012 at 8:09

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.