我有一个在 Debian 中运行的 Python (2.7.13) HTTP 服务器,我想停止任何花费超过 10 秒的 GET 请求,但在任何地方都找不到解决方案。
我已经尝试了以下问题中发布的所有片段:如何在 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()
作为测试,我正在运行一个需要 20 秒才能执行的 shell 命令,因此我需要在此之前停止服务器。