Another possible fix for server issues.

This commit is contained in:
blitzmann
2015-10-30 21:07:56 -04:00
parent 6eafbb0a25
commit 649b99d7bd
2 changed files with 15 additions and 10 deletions

View File

@@ -5,6 +5,7 @@ import threading
import copy
import uuid
import wx
import time
from wx.lib.pubsub import pub
@@ -112,6 +113,7 @@ class Crest():
logging.debug("Starting server")
if self.httpd:
self.stopServer()
time.sleep(1) # we need this to ensure that the previous get_request finishes, and then the socket will close
self.httpd = service.StoppableHTTPServer(('', 6461), service.AuthHandler)
thread.start_new_thread(self.httpd.serve, ())

View File

@@ -58,8 +58,12 @@ class StoppableHTTPServer(BaseHTTPServer.HTTPServer):
def server_bind(self):
BaseHTTPServer.HTTPServer.server_bind(self)
# Allow listeing for 60 seconds
self.socket.settimeout(60)
# Allow listening for 60 seconds
sec = 60
self.socket.settimeout(0.5)
self.max_tries = sec / self.socket.gettimeout()
self.tries = 0
self.run = True
def get_request(self):
@@ -72,20 +76,19 @@ class StoppableHTTPServer(BaseHTTPServer.HTTPServer):
pass
def stop(self):
self.socket.close()
self.run = False
self.server_close()
def handle_timeout(self):
logger.debug("Server timed out waiting for connection")
self.stop()
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):
while self.run:
try:
self.handle_request()
except TypeError:
# this can happen if stopping server in middle of request?
pass
self.handle_request()
if __name__ == "__main__":
httpd = StoppableHTTPServer(('', 6461), AuthHandler)