我有一个在Debian中运行的Python(2.7.13)HTTP服务器,我想停止任何超过10秒的GET请求,但是在任何地方都找不到解决方案。在
我已经试过以下问题中的所有片段:How to implement Timeout in BaseHTTPServer.BaseHTTPRequestHandler Python
#!/usr/bin/env python
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import os
class handlr(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type','text-html')
self.end_headers()
self.wfile.write(os.popen('sleep 20 & echo "this took 20 seconds"').read())
def run():
server_address = ('127.0.0.1', 8080)
httpd = HTTPServer(server_address, handlr)
httpd.serve_forever()
if __name__ == '__main__':
run()
作为测试,我运行的shell命令需要20秒才能执行,所以我需要在这之前停止服务器。在
将操作放在后台线程上,然后等待后台线程完成。没有一种通用的安全方法来中止线程,所以这个实现不幸地让函数在后台运行,即使它已经放弃了。在
如果可以,可以考虑在服务器前面放置一个代理服务器(比如
nginx
),让它为您处理超时,或者使用一个更健壮的HTTP服务器实现,它允许将此作为配置选项。但下面的答案基本上应该涵盖它。在相关问题 更多 >
编程相关推荐