我希望能够通过立即返回消息然后启动进程以在后台运行并完成任务来响应GET。这适用于BaseHTTPServer.HTTPServer并扩展BaseHTTPRequestHandler。但是,当我按照说明here将SSL添加到服务器时,在子流程完成之前不会发送响应。假设Firefox在接收它时显示它收到的内容,并使用Firebug进行双重检查,看起来在子进程完成之前根本没有发送任何东西。我在Chrome中测试过并看到了相同的结果。代码如下;我对如何继续调试感到茫然。
class SecureHTTPServer(HTTPServer):
def __init__(self, server_address, HandlerClass):
BaseServer.__init__(self, server_address, HandlerClass)
ctx = SSL.Context(SSL.SSLv3_METHOD)
ctx.use_privatekey_file(SSL_CERT)
ctx.use_certificate_file(SSL_CERT)
self.socket = SSL.Connection(ctx, socket.socket(self.address_family, self.socket_type))
self.server_bind()
self.server_activate()
def shutdown_request(self, request):
request.shutdown()
class RecordingHandler(BaseHTTPRequestHandler):
def setup(self):
self.connection = self.request
self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
def do_GET(self):
key = 'abc'
p = Process(target = some_func, args = (key,))
# Some_func takes ~15 seconds to complete, and it logs its progress so that it's clear that the response is only sent after some_func is complete.
p.daemon = True
p.start()
self.send_response(200)
self.end_headers()
self.wfile.write('recording key ' + key)
def run():
try:
server = SecureHTTPServer(('', 8080), RecordingHandler)
print 'Waiting for input on port 8080.'
server.serve_forever()
except KeyboardInterrupt:
print 'Received keyboard interrupt, exiting.'
server.shutdown()
答案 0 :(得分:0)
显然我只需要放弃,重新启动,然后再尝试谷歌搜索。我找到了this solution:
import BaseHTTPServer, SimpleHTTPServer
import ssl
httpd = BaseHTTPServer.HTTPServer(('localhost', 4443), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, certfile='path/to/localhost.pem', server_side=True)
httpd.serve_forever()
我仍然不知道以前的设置有什么问题,但这很好用。