I wrote a preview function based on SimpleHTTPServer and SocketServer, I catch KeyboardInterrupt
exception as I enter Ctrl-C
to stop server:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import SimpleHTTPServer
import SocketServer
class Reuse_TCPServer(SocketServer.TCPServer):
timeout = 1
allow_reuse_address = True
def preview(port=8000):
try:
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = Reuse_TCPServer(("", port), Handler)
except OSError as e:
print("Could not listen on port {}".format(port))
sys.exit(getattr(e, 'exitcode', 1))
try:
httpd.serve_forever()
except (KeyboardInterrupt, SystemExit) as e:
print("Shutting down server")
httpd.socket.close()
if __name__ == "__main__":
preview()
But most of the time , if I open localhost:8000 and immediately(some seconds) enter 'Ctrl-C', it will display the message first and then close the socket:
127.0.0.1 - - [16/Apr/2014 22:20:42] code 404, message File not found
127.0.0.1 - - [16/Apr/2014 22:20:42] "GET /static/css/autumn.css HTTP/1.1" 404 -
^C----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 52787)
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 649, in __init__
self.handle()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 340, in handle
self.handle_one_request()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 310, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 476, in readline
data = self._sock.recv(self._rbufsize)
KeyboardInterrupt
----------------------------------------
f^C----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 52788)
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 649, in __init__
self.handle()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 340, in handle
self.handle_one_request()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 310, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 476, in readline
data = self._sock.recv(self._rbufsize)
KeyboardInterrupt
----------------------------------------
^CShutting down server
Does anyone know how to solve this problem?
I'm experiencing exactly the same. I found an old bug in Python 2.7 and a patch that was supposed to solve this. I checked it and the patch is of course already in SocketServer.py. However, it didn't solve this problem. In any case, the explanation in the link I'm pasting here is worth to be read to try to get a hint.
https://bugs.python.org/issue14574
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