Python 在SocketServer中处理来自(';127.0.0.1';xxxx)的请求时发生异常

Python 在SocketServer中处理来自(';127.0.0.1';xxxx)的请求时发生异常,python,sockets,http,Python,Sockets,Http,我基于SimpleHTTPServer和SocketServer编写了一个预览函数,当我输入Ctrl-C停止服务器时,我捕捉到KeyboardInterrupt异常: #!/usr/bin/env python # -*- coding: utf-8 -*- import sys import SimpleHTTPServer import SocketServer class Reuse_TCPServer(SocketServer.TCPServer): timeout =

我基于SimpleHTTPServer和SocketServer编写了一个预览函数,当我输入
Ctrl-C
停止服务器时,我捕捉到
KeyboardInterrupt
异常:

#!/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()
但大多数情况下,如果我打开localhost:8000并立即(几秒钟)输入“Ctrl-C”,它将首先显示消息,然后关闭套接字:

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

有人知道如何解决这个问题吗?

我也有同样的经历。我在Python2.7中发现了一个旧的bug和一个应该解决这个问题的补丁。我检查了它,补丁当然已经在SocketServer.py中了。然而,它并没有解决这个问题。 在任何情况下,我粘贴在这里的链接中的解释都值得一读,以试图得到一个提示


您的问题是什么?@laike9m抱歉,我有更新问题:“如何解决此问题”我仍然不知道您的问题是什么您还必须多次按Ctrl-C才能使其工作?@laike9m问题是如何解决我在我的问题中粘贴的内容,消息“处理来自…的请求时发生异常”这里也一样:同样的问题,同样的挫折,同样令人讨厌的异常堵塞了我的日志文件。