From 97cdd751b8c11a7e1dd07175acb563a6c3bebddd Mon Sep 17 00:00:00 2001 From: blitzman Date: Mon, 13 Feb 2017 21:55:02 -0500 Subject: [PATCH] Fix up the CREST server thread to make it a bit more robust, as well as fixing issue in which thread may never die --- service/crest.py | 14 ++++++----- service/server.py | 61 +++++++++++++++++++++++++---------------------- 2 files changed, 41 insertions(+), 34 deletions(-) diff --git a/service/crest.py b/service/crest.py index 39863fb31..bbe8cd1bb 100644 --- a/service/crest.py +++ b/service/crest.py @@ -168,19 +168,23 @@ class Crest(object): self.stopServer() time.sleep(1) # we need this to ensure that the previous get_request finishes, and then the socket will close - self.httpd = StoppableHTTPServer(('', 6461), AuthHandler) - thread.start_new_thread(self.httpd.serve, (self.handleLogin,)) + self.httpd = StoppableHTTPServer(('localhost', 6461), AuthHandler) + + self.serverThread = threading.Thread(target=self.httpd.serve, args=(self.handleLogin,)) + self.serverThread.name = "CRESTServer" + self.serverThread.daemon = True + self.serverThread.start() self.state = str(uuid.uuid4()) return self.eve.auth_uri(scopes=self.scopes, state=self.state) def handleLogin(self, message): if not message: - return + raise Exception("Could not parse out querystring parameters.") if message['state'][0] != self.state: logger.warn("OAUTH state mismatch") - return + raise Exception("OAUTH State Mismatch.") logger.debug("Handling CREST login with: %s" % message) @@ -222,5 +226,3 @@ class Crest(object): eos.db.save(char) wx.PostEvent(self.mainFrame, GE.SsoLogin(type=CrestModes.USER)) - - self.stopServer() diff --git a/service/server.py b/service/server.py index 9fc93f05f..38ecebfe4 100644 --- a/service/server.py +++ b/service/server.py @@ -1,11 +1,8 @@ import BaseHTTPServer import urlparse import socket -import thread import logging - -# noinspection PyPackageRequirements -import wx +import threading from service.settings import CRESTSettings @@ -20,12 +17,13 @@ HTML = ''' pyfa Local Server @@ -34,32 +32,29 @@ HTML = '''

pyfa

-
-

If you see this message then it means you should be logged into CREST. You may close this window and return to the application.

-
+ {0}
''' - # https://github.com/fuzzysteve/CREST-Market-Downloader/ class AuthHandler(BaseHTTPServer.BaseHTTPRequestHandler): def do_GET(self): @@ -67,11 +62,20 @@ class AuthHandler(BaseHTTPServer.BaseHTTPRequestHandler): return parsed_path = urlparse.urlparse(self.path) parts = urlparse.parse_qs(parsed_path.query) - self.send_response(200) - self.end_headers() - self.wfile.write(HTML) - wx.CallAfter(self.server.callback, parts) + msg = "" + + try: + self.server.callback(parts) + msg = "If you see this message then it means you should be logged into CREST. You may close this window and return to the application." + except Exception, ex: + msg = "

Error

\n

{}

".format(ex.message) + finally: + self.send_response(200) + self.end_headers() + self.wfile.write(HTML.format(msg)) + + self.server.stop() def log_message(self, format, *args): return @@ -87,7 +91,7 @@ class StoppableHTTPServer(BaseHTTPServer.HTTPServer): sec = self.settings.get('timeout') logger.debug("Running server for %d seconds", sec) - self.socket.settimeout(0.5) + self.socket.settimeout(1) self.max_tries = sec / self.socket.gettimeout() self.tries = 0 self.run = True @@ -105,24 +109,25 @@ class StoppableHTTPServer(BaseHTTPServer.HTTPServer): self.run = False def handle_timeout(self): - # logger.debug("Number of tries: %d"%self.tries) + # logger.debug("Number of tries: %d" % self.tries) self.tries += 1 if self.tries == self.max_tries: logger.debug("Server timed out waiting for connection") self.stop() - def serve(self, callback): + def serve(self, callback=None): self.callback = callback while self.run: try: self.handle_request() except TypeError: pass + self.server_close() if __name__ == "__main__": httpd = StoppableHTTPServer(('', 6461), AuthHandler) - thread.start_new_thread(httpd.serve, ()) + t = threading.Thread(target=httpd.serve) raw_input("Press to stop server\n") httpd.stop()