commit
4459824cc8
|
|
@ -90,6 +90,9 @@ Sec-WebSocket-Accept: %s\r
|
|||
class CClose(Exception):
|
||||
pass
|
||||
|
||||
class Terminate(Exception):
|
||||
pass
|
||||
|
||||
def __init__(self, listen_host='', listen_port=None, source_is_ipv6=False,
|
||||
verbose=False, cert='', key='', ssl_only=None,
|
||||
daemon=False, record='', web='', file_only=False, no_parent=False,
|
||||
|
|
@ -214,8 +217,7 @@ Sec-WebSocket-Accept: %s\r
|
|||
if os.fork() > 0: os._exit(0) # Parent exits
|
||||
|
||||
# Signal handling
|
||||
def terminate(a,b): os._exit(0)
|
||||
signal.signal(signal.SIGTERM, terminate)
|
||||
signal.signal(signal.SIGTERM, signal.SIG_IGN)
|
||||
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
||||
|
||||
# Close open files
|
||||
|
|
@ -666,6 +668,9 @@ Sec-WebSocket-Accept: %s\r
|
|||
#self.vmsg("Running poll()")
|
||||
pass
|
||||
|
||||
def terminate(self):
|
||||
raise self.Terminate()
|
||||
|
||||
def fallback_SIGCHLD(self, sig, stack):
|
||||
# Reap zombies when using os.fork() (python 2.4)
|
||||
self.vmsg("Got SIGCHLD, reaping zombies")
|
||||
|
|
@ -679,7 +684,11 @@ Sec-WebSocket-Accept: %s\r
|
|||
|
||||
def do_SIGINT(self, sig, stack):
|
||||
self.msg("Got SIGINT, exiting")
|
||||
sys.exit(0)
|
||||
self.terminate()
|
||||
|
||||
def do_SIGTERM(self, sig, stack):
|
||||
self.msg("Got SIGTERM, exiting")
|
||||
self.terminate()
|
||||
|
||||
def top_new_client(self, startsock, address):
|
||||
""" Do something with a WebSockets client connection. """
|
||||
|
|
@ -709,7 +718,7 @@ Sec-WebSocket-Accept: %s\r
|
|||
|
||||
self.ws_connection = True
|
||||
self.new_client()
|
||||
except self.CClose:
|
||||
except self.CClose, WebSocketServer.Terminate:
|
||||
# Close the client
|
||||
_, exc, _ = sys.exc_info()
|
||||
if self.client:
|
||||
|
|
@ -719,6 +728,8 @@ Sec-WebSocket-Accept: %s\r
|
|||
# Connection was not a WebSockets connection
|
||||
if exc.args[0]:
|
||||
self.msg("%s: %s" % (address[0], exc.args[0]))
|
||||
except WebSocketServer.Terminate:
|
||||
raise
|
||||
except Exception:
|
||||
_, exc, _ = sys.exc_info()
|
||||
self.msg("handler exception: %s" % str(exc))
|
||||
|
|
@ -752,11 +763,18 @@ Sec-WebSocket-Accept: %s\r
|
|||
|
||||
self.started() # Some things need to happen after daemonizing
|
||||
|
||||
# Allow override of SIGINT
|
||||
# Allow override of signals
|
||||
original_signals = {
|
||||
signal.SIGINT: signal.getsignal(signal.SIGINT),
|
||||
signal.SIGTERM: signal.getsignal(signal.SIGTERM),
|
||||
signal.SIGCHLD: signal.getsignal(signal.SIGCHLD),
|
||||
}
|
||||
signal.signal(signal.SIGINT, self.do_SIGINT)
|
||||
signal.signal(signal.SIGTERM, self.do_SIGTERM)
|
||||
signal.signal(signal.SIGCHLD, self.fallback_SIGCHLD)
|
||||
|
||||
last_active_time = self.launch_time
|
||||
try:
|
||||
while True:
|
||||
try:
|
||||
try:
|
||||
|
|
@ -796,6 +814,8 @@ Sec-WebSocket-Accept: %s\r
|
|||
startsock, address = lsock.accept()
|
||||
else:
|
||||
continue
|
||||
except self.Terminate:
|
||||
raise
|
||||
except Exception:
|
||||
_, exc, _ = sys.exc_info()
|
||||
if hasattr(exc, 'errno'):
|
||||
|
|
@ -836,13 +856,9 @@ Sec-WebSocket-Accept: %s\r
|
|||
# parent process
|
||||
self.handler_id += 1
|
||||
|
||||
except KeyboardInterrupt:
|
||||
except (self.Terminate, SystemExit, KeyboardInterrupt):
|
||||
_, exc, _ = sys.exc_info()
|
||||
print("In KeyboardInterrupt")
|
||||
pass
|
||||
except SystemExit:
|
||||
_, exc, _ = sys.exc_info()
|
||||
print("In SystemExit")
|
||||
print("In exit")
|
||||
break
|
||||
except Exception:
|
||||
_, exc, _ = sys.exc_info()
|
||||
|
|
@ -853,12 +869,16 @@ Sec-WebSocket-Accept: %s\r
|
|||
finally:
|
||||
if startsock:
|
||||
startsock.close()
|
||||
|
||||
finally:
|
||||
# Close listen port
|
||||
self.vmsg("Closing socket listening at %s:%s"
|
||||
% (self.listen_host, self.listen_port))
|
||||
lsock.close()
|
||||
|
||||
# Restore signals
|
||||
for sig, func in original_signals.items():
|
||||
signal.signal(sig, func)
|
||||
|
||||
|
||||
# HTTP handler with WebSocket upgrade support
|
||||
class WSRequestHandler(SimpleHTTPRequestHandler):
|
||||
|
|
|
|||
Loading…
Reference in New Issue