运行 Get 上的脚本和 Return 立即响应
Run Script on Get and Return response immediately
抱歉,我对 Python httpserver 非常陌生。我也知道它的漏洞或局限性,但它非常适合我的需要。
我有大约 10 行代码 运行 对服务器的每个 get 请求进行处理(基于请求 header 中的内容)。这些最多可能需要 8 个小时才能完成 运行,但我想 return 立即回复请求。这样做的最佳方法是什么?
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging
class S(BaseHTTPRequestHandler):
def _set_response(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
THIS IS WHERE MY CODE IS RUNNING
logging.info("GET request,nPath: %snHeaders:n%sn", str(self.path), str(self.headers))
self._set_response()
self.wfile.write("HEY A GET request for {}".format(self.path).encode('utf-8'))
def run(server_class=HTTPServer, handler_class=S, port=8080):
logging.basicConfig(level=logging.INFO)
server_address = ('', port)
httpd = server_class(server_address, handler_class)
logging.info('Starting httpd...n')
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
logging.info('Stopping httpd...n')
if __name__ == '__main__':
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()
谢谢
这正是多线程的用途,我建议您阅读这篇文章tutorial以了解更多信息。
这里是如何从 do_GET 函数内部创建新线程的示例:
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging
import thread
class S(BaseHTTPRequestHandler):
def _set_response(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_process(self):
# Your code here
def do_GET(self):
thread.start_new_thread(self.do_process)
logging.info("GET request,nPath: %snHeaders:n%sn", str(self.path), str(self.headers))
self._set_response()
self.wfile.write("HEY A GET request for {}".format(self.path).encode('utf-8'))
def run(server_class=HTTPServer, handler_class=S, port=8080):
logging.basicConfig(level=logging.INFO)
server_address = ('', port)
httpd = server_class(server_address, handler_class)
logging.info('Starting httpd...n')
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
logging.info('Stopping httpd...n')
if __name__ == '__main__':
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()
抱歉,我对 Python httpserver 非常陌生。我也知道它的漏洞或局限性,但它非常适合我的需要。
我有大约 10 行代码 运行 对服务器的每个 get 请求进行处理(基于请求 header 中的内容)。这些最多可能需要 8 个小时才能完成 运行,但我想 return 立即回复请求。这样做的最佳方法是什么?
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging
class S(BaseHTTPRequestHandler):
def _set_response(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
THIS IS WHERE MY CODE IS RUNNING
logging.info("GET request,nPath: %snHeaders:n%sn", str(self.path), str(self.headers))
self._set_response()
self.wfile.write("HEY A GET request for {}".format(self.path).encode('utf-8'))
def run(server_class=HTTPServer, handler_class=S, port=8080):
logging.basicConfig(level=logging.INFO)
server_address = ('', port)
httpd = server_class(server_address, handler_class)
logging.info('Starting httpd...n')
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
logging.info('Stopping httpd...n')
if __name__ == '__main__':
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()
谢谢
这正是多线程的用途,我建议您阅读这篇文章tutorial以了解更多信息。
这里是如何从 do_GET 函数内部创建新线程的示例:
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging
import thread
class S(BaseHTTPRequestHandler):
def _set_response(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_process(self):
# Your code here
def do_GET(self):
thread.start_new_thread(self.do_process)
logging.info("GET request,nPath: %snHeaders:n%sn", str(self.path), str(self.headers))
self._set_response()
self.wfile.write("HEY A GET request for {}".format(self.path).encode('utf-8'))
def run(server_class=HTTPServer, handler_class=S, port=8080):
logging.basicConfig(level=logging.INFO)
server_address = ('', port)
httpd = server_class(server_address, handler_class)
logging.info('Starting httpd...n')
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
logging.info('Stopping httpd...n')
if __name__ == '__main__':
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()