This commit is contained in:
croneter 2018-02-11 12:12:16 +01:00
parent 055aadc048
commit 406c2b9f63

View file

@ -504,7 +504,8 @@ class WebSocket(object):
self.connected = True self.connected = True
def _validate_header(self, headers, key): @staticmethod
def _validate_header(headers, key):
for k, v in _HEADERS_TO_CHECK.iteritems(): for k, v in _HEADERS_TO_CHECK.iteritems():
r = headers.get(k, None) r = headers.get(k, None)
if not r: if not r:
@ -598,7 +599,7 @@ class WebSocket(object):
return value: string(byte array) value. return value: string(byte array) value.
""" """
opcode, data = self.recv_data() _, data = self.recv_data()
return data return data
def recv_data(self): def recv_data(self):
@ -620,7 +621,6 @@ class WebSocket(object):
self._cont_data[1] += frame.data self._cont_data[1] += frame.data
else: else:
self._cont_data = [frame.opcode, frame.data] self._cont_data = [frame.opcode, frame.data]
if frame.fin: if frame.fin:
data = self._cont_data data = self._cont_data
self._cont_data = None self._cont_data = None
@ -740,7 +740,7 @@ class WebSocket(object):
def _recv(self, bufsize): def _recv(self, bufsize):
try: try:
bytes = self.sock.recv(bufsize) bytes_ = self.sock.recv(bufsize)
except socket.timeout as e: except socket.timeout as e:
raise WebSocketTimeoutException(e.args[0]) raise WebSocketTimeoutException(e.args[0])
except SSLError as e: except SSLError as e:
@ -748,17 +748,17 @@ class WebSocket(object):
raise WebSocketTimeoutException(e.args[0]) raise WebSocketTimeoutException(e.args[0])
else: else:
raise raise
if not bytes: if not bytes_:
raise WebSocketConnectionClosedException() raise WebSocketConnectionClosedException()
return bytes return bytes_
def _recv_strict(self, bufsize): def _recv_strict(self, bufsize):
shortage = bufsize - sum(len(x) for x in self._recv_buffer) shortage = bufsize - sum(len(x) for x in self._recv_buffer)
while shortage > 0: while shortage > 0:
bytes = self._recv(shortage) bytes_ = self._recv(shortage)
self._recv_buffer.append(bytes) self._recv_buffer.append(bytes_)
shortage -= len(bytes) shortage -= len(bytes_)
unified = "".join(self._recv_buffer) unified = "".join(self._recv_buffer)
if shortage == 0: if shortage == 0:
self._recv_buffer = [] self._recv_buffer = []
@ -783,7 +783,7 @@ class WebSocketApp(object):
Higher level of APIs are provided. Higher level of APIs are provided.
The interface is like JavaScript WebSocket object. The interface is like JavaScript WebSocket object.
""" """
def __init__(self, url, header=[], def __init__(self, url, header=None,
on_open=None, on_message=None, on_error=None, on_open=None, on_message=None, on_error=None,
on_close=None, keep_running=True, get_mask_key=None): on_close=None, keep_running=True, get_mask_key=None):
""" """
@ -807,7 +807,7 @@ class WebSocketApp(object):
docstring for more information docstring for more information
""" """
self.url = url self.url = url
self.header = header self.header = [] if header is None else header
self.on_open = on_open self.on_open = on_open
self.on_message = on_message self.on_message = on_message
self.on_error = on_error self.on_error = on_error
@ -830,12 +830,12 @@ class WebSocketApp(object):
close websocket connection. close websocket connection.
""" """
self.keep_running = False self.keep_running = False
if(self.sock != None): if self.sock != None:
self.sock.close() self.sock.close()
def _send_ping(self, interval): def _send_ping(self, interval):
while True: while True:
for i in range(interval): for _ in range(interval):
time.sleep(1) time.sleep(1)
if not self.keep_running: if not self.keep_running:
return return
@ -879,7 +879,6 @@ class WebSocketApp(object):
if data is None or self.keep_running == False: if data is None or self.keep_running == False:
break break
self._callback(self.on_message, data) self._callback(self.on_message, data)
except Exception, e: except Exception, e:
#print str(e.args[0]) #print str(e.args[0])
if "timed out" not in e.args[0]: if "timed out" not in e.args[0]: