SocketServer处理请求时发生异常('127.0.0.1', xxxx)

7 投票
1 回答
16271 浏览
提问于 2025-04-18 02:59

我写了一个预览功能,基于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

有没有人知道怎么解决这个问题?

1 个回答

4

我遇到的情况和你完全一样。我发现了Python 2.7中的一个旧bug,还有一个本来应该解决这个问题的补丁。我检查了一下,发现这个补丁已经在SocketServer.py里面了。不过,这个补丁并没有解决我的问题。
无论如何,我在这里贴的链接里的解释值得一读,也许能给你一些线索。

https://bugs.python.org/issue14574

撰写回答