【问题标题】:Python - BaseHTTPServer - Overriding "handle_timeout" methodPython - BaseHTTPServer - 覆盖“handle_timeout”方法
【发布时间】:2016-08-25 18:29:41
【问题描述】:

我目前正在使用 Python 的 BaseHTTPServer,我需要覆盖服务器的 handle_timeout() 方法。

根据 Python 的文档 (https://docs.python.org/2/library/basehttpserver.html),BaseHTTPServer 是 TCPServer 的子类。 TCPServer 实现了我想要覆盖的方法handle_timeout (https://docs.python.org/2/library/socketserver.html#SocketServer.BaseServer.handle_timeout)。

我的代码目前看起来像这样:

class newServer(BaseHTTPServer.HTTPServer):

def handle_timeout(self):
    print "Timeout!"

class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):

def setup(self):
    self.timeout=2
    BaseHTTPServer.BaseHTTPRequestHandler.setup(self)  


if __name__ == '__main__':

server=newServer

httpd = server((HOST_NAME, PORT_NUMBER), RequestHandler)
print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
try:
    httpd.serve_forever()
except KeyboardInterrupt:
    pass


httpd.server_close()
print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)

超时本身有效,我从服务器收到以下消息:

Request timed out: timeout('timed out',)

问题是,这不是我要打印的消息。 对我的问题有帮助吗?

【问题讨论】:

    标签: python timeout basehttpserver


    【解决方案1】:

    对于任何有兴趣的人,在尝试了一些事情之后,我终于得出了一个结论:

    根据这个文件(https://svn.python.org/projects/python/trunk/Lib/BaseHTTPServer.py),我直接从BaseHTTPRequestHandlerhandle_one_request-方法中复制了代码,并覆盖了最后几行以最终使我的代码工作。

    我所有的其他尝试(在服务器类中覆盖它,在请求处理程序或main-方法中捕获异常)似乎都不起作用。

    希望这有时可以帮助某人。

    【讨论】:

      猜你喜欢
      • 2016-02-28
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 2010-10-27
      • 2012-09-27
      • 2019-06-30
      • 2021-01-14
      相关资源
      最近更新 更多