我是python的新手,在python中寫了一個簡單的httpserver。我試圖關閉服務器從請求到服務器。我怎樣才能實現從處理程序調用服務器功能的這種功能?如何從httprequest處理程序停止Python中的簡單HTTP服務器?
class MyHandler(SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == '/shutdown':
pass # I want to call MainServer.shutdown from here
class MainServer()
def __init__(self, port = 8123):
self._server = HTTPServer(('0.0.0.0', port), MyHandler)
self._thread = threading.Thread(target = self._server.serve_forever)
self._thread.deamon = True
def start(self):
self._thread.start()
def shut_down(self):
self._thread.close()