Cleanup websocket.py

This commit is contained in:
tomkat83 2016-09-04 16:37:01 +02:00
parent bc1a9138e6
commit 5615bad026

View file

@ -74,7 +74,7 @@ STATUS_INVALID_EXTENSION = 1010
STATUS_UNEXPECTED_CONDITION = 1011 STATUS_UNEXPECTED_CONDITION = 1011
STATUS_TLS_HANDSHAKE_ERROR = 1015 STATUS_TLS_HANDSHAKE_ERROR = 1015
logger = logging.getLogger() log = logging.getlog("PLEX."+__name__)
class WebSocketException(Exception): class WebSocketException(Exception):
@ -110,9 +110,9 @@ def enableTrace(tracable):
global traceEnabled global traceEnabled
traceEnabled = tracable traceEnabled = tracable
if tracable: if tracable:
if not logger.handlers: if not log.handlers:
logger.addHandler(logging.StreamHandler()) log.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG) log.setLevel(logging.DEBUG)
def setdefaulttimeout(timeout): def setdefaulttimeout(timeout):
@ -484,9 +484,9 @@ class WebSocket(object):
header_str = "\r\n".join(headers) header_str = "\r\n".join(headers)
self._send(header_str) self._send(header_str)
if traceEnabled: if traceEnabled:
logger.debug("--- request header ---") log.debug("--- request header ---")
logger.debug(header_str) log.debug(header_str)
logger.debug("-----------------------") log.debug("-----------------------")
status, resp_headers = self._read_headers() status, resp_headers = self._read_headers()
if status != 101: if status != 101:
@ -522,7 +522,7 @@ class WebSocket(object):
status = None status = None
headers = {} headers = {}
if traceEnabled: if traceEnabled:
logger.debug("--- response header ---") log.debug("--- response header ---")
while True: while True:
line = self._recv_line() line = self._recv_line()
@ -530,7 +530,7 @@ class WebSocket(object):
break break
line = line.strip() line = line.strip()
if traceEnabled: if traceEnabled:
logger.debug(line) log.debug(line)
if not status: if not status:
status_info = line.split(" ", 2) status_info = line.split(" ", 2)
status = int(status_info[1]) status = int(status_info[1])
@ -543,7 +543,7 @@ class WebSocket(object):
raise WebSocketException("Invalid header") raise WebSocketException("Invalid header")
if traceEnabled: if traceEnabled:
logger.debug("-----------------------") log.debug("-----------------------")
return status, headers return status, headers
@ -563,7 +563,7 @@ class WebSocket(object):
data = frame.format() data = frame.format()
length = len(data) length = len(data)
if traceEnabled: if traceEnabled:
logger.debug("send: " + repr(data)) log.debug("send: " + repr(data))
while data: while data:
l = self._send(data) l = self._send(data)
data = data[l:] data = data[l:]
@ -706,10 +706,10 @@ class WebSocket(object):
self.sock.settimeout(3) self.sock.settimeout(3)
try: try:
frame = self.recv_frame() frame = self.recv_frame()
if logger.isEnabledFor(logging.ERROR): if log.isEnabledFor(logging.ERROR):
recv_status = struct.unpack("!H", frame.data)[0] recv_status = struct.unpack("!H", frame.data)[0]
if recv_status != STATUS_NORMAL: if recv_status != STATUS_NORMAL:
logger.error("close status: " + repr(recv_status)) log.error("close status: " + repr(recv_status))
except: except:
pass pass
self.sock.settimeout(timeout) self.sock.settimeout(timeout)
@ -895,8 +895,8 @@ class WebSocketApp(object):
try: try:
callback(self, *args) callback(self, *args)
except Exception, e: except Exception, e:
logger.error(e) log.error(e)
if True:#logger.isEnabledFor(logging.DEBUG): if True:#log.isEnabledFor(logging.DEBUG):
_, _, tb = sys.exc_info() _, _, tb = sys.exc_info()
traceback.print_tb(tb) traceback.print_tb(tb)