refactoring. logger and some pep8.
This commit is contained in:
@@ -29,12 +29,13 @@ import traceback
|
|||||||
import sys
|
import sys
|
||||||
import select
|
import select
|
||||||
import six
|
import six
|
||||||
import logging
|
|
||||||
|
|
||||||
from ._core import WebSocket, getdefaulttimeout, logger
|
from ._core import WebSocket, getdefaulttimeout
|
||||||
from ._exceptions import *
|
from ._exceptions import *
|
||||||
|
from ._logging import *
|
||||||
from websocket._abnf import ABNF
|
from websocket._abnf import ABNF
|
||||||
|
|
||||||
|
|
||||||
class WebSocketApp(object):
|
class WebSocketApp(object):
|
||||||
"""
|
"""
|
||||||
Higher level of APIs are provided.
|
Higher level of APIs are provided.
|
||||||
@@ -61,15 +62,17 @@ class WebSocketApp(object):
|
|||||||
The passing 2nd arugment is exception object.
|
The passing 2nd arugment is exception object.
|
||||||
on_close: callable object which is called when closed the connection.
|
on_close: callable object which is called when closed the connection.
|
||||||
this function has one argument. The arugment is this class object.
|
this function has one argument. The arugment is this class object.
|
||||||
on_cont_message: callback object which is called when recieve continued frame data.
|
on_cont_message: callback object which is called when recieve continued
|
||||||
|
frame data.
|
||||||
on_message has 3 arguments.
|
on_message has 3 arguments.
|
||||||
The 1st arugment is this class object.
|
The 1st arugment is this class object.
|
||||||
The passing 2nd arugment is utf-8 string which we get from the server.
|
The passing 2nd arugment is utf-8 string which we get from the server.
|
||||||
The 3rd arugment is continue flag. if 0, the data continue to next frame data
|
The 3rd arugment is continue flag. if 0, the data continue
|
||||||
keep_running: a boolean flag indicating whether the app's main loop should
|
to next frame data
|
||||||
keep running, defaults to True
|
keep_running: a boolean flag indicating whether the app's main loop
|
||||||
get_mask_key: a callable to produce new mask keys, see the WebSocket.set_mask_key's
|
should keep running, defaults to True
|
||||||
docstring for more information
|
get_mask_key: a callable to produce new mask keys,
|
||||||
|
see the WebSocket.set_mask_key's docstring for more information
|
||||||
subprotocols: array of available sub protocols. default is None.
|
subprotocols: array of available sub protocols. default is None.
|
||||||
"""
|
"""
|
||||||
self.url = url
|
self.url = url
|
||||||
@@ -86,12 +89,13 @@ class WebSocketApp(object):
|
|||||||
self.get_mask_key = get_mask_key
|
self.get_mask_key = get_mask_key
|
||||||
self.sock = None
|
self.sock = None
|
||||||
self.last_ping_tm = 0
|
self.last_ping_tm = 0
|
||||||
self.subprotocols =subprotocols
|
self.subprotocols = subprotocols
|
||||||
|
|
||||||
def send(self, data, opcode=ABNF.OPCODE_TEXT):
|
def send(self, data, opcode=ABNF.OPCODE_TEXT):
|
||||||
"""
|
"""
|
||||||
send message.
|
send message.
|
||||||
data: message to send. If you set opcode to OPCODE_TEXT, data must be utf-8 string or unicode.
|
data: message to send. If you set opcode to OPCODE_TEXT,
|
||||||
|
data must be utf-8 string or unicode.
|
||||||
opcode: operation code of data. default is OPCODE_TEXT.
|
opcode: operation code of data. default is OPCODE_TEXT.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@@ -112,16 +116,20 @@ class WebSocketApp(object):
|
|||||||
if self.sock:
|
if self.sock:
|
||||||
self.sock.ping()
|
self.sock.ping()
|
||||||
|
|
||||||
def run_forever(self, sockopt=None, sslopt=None, ping_interval=0, ping_timeout=None,
|
def run_forever(self, sockopt=None, sslopt=None,
|
||||||
http_proxy_host=None, http_proxy_port=None, http_no_proxy=None, http_proxy_auth=None,
|
ping_interval=0, ping_timeout=None,
|
||||||
skip_utf8_validation=False):
|
http_proxy_host=None, http_proxy_port=None,
|
||||||
|
http_no_proxy=None, http_proxy_auth=None,
|
||||||
|
skip_utf8_validation=False):
|
||||||
"""
|
"""
|
||||||
run event loop for WebSocket framework.
|
run event loop for WebSocket framework.
|
||||||
This loop is infinite loop and is alive during websocket is available.
|
This loop is infinite loop and is alive during websocket is available.
|
||||||
sockopt: values for socket.setsockopt.
|
sockopt: values for socket.setsockopt.
|
||||||
sockopt must be tuple and each element is argument of sock.setscokopt.
|
sockopt must be tuple
|
||||||
|
and each element is argument of sock.setscokopt.
|
||||||
sslopt: ssl socket optional dict.
|
sslopt: ssl socket optional dict.
|
||||||
ping_interval: automatically send "ping" command every specified period(second)
|
ping_interval: automatically send "ping" command
|
||||||
|
every specified period(second)
|
||||||
if set to 0, not send automatically.
|
if set to 0, not send automatically.
|
||||||
ping_timeout: timeout(second) if the pong message is not recieved.
|
ping_timeout: timeout(second) if the pong message is not recieved.
|
||||||
http_proxy_host: http proxy host name.
|
http_proxy_host: http proxy host name.
|
||||||
@@ -130,7 +138,7 @@ class WebSocketApp(object):
|
|||||||
skip_utf8_validation: skip utf8 validation.
|
skip_utf8_validation: skip utf8 validation.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if not ping_timeout or ping_timeout<=0:
|
if not ping_timeout or ping_timeout <= 0:
|
||||||
ping_timeout = None
|
ping_timeout = None
|
||||||
if sockopt is None:
|
if sockopt is None:
|
||||||
sockopt = []
|
sockopt = []
|
||||||
@@ -142,18 +150,20 @@ class WebSocketApp(object):
|
|||||||
close_frame = None
|
close_frame = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.sock = WebSocket(self.get_mask_key, sockopt=sockopt, sslopt=sslopt,
|
self.sock = WebSocket(self.get_mask_key,
|
||||||
|
sockopt=sockopt, sslopt=sslopt,
|
||||||
fire_cont_frame=self.on_cont_message and True or False,
|
fire_cont_frame=self.on_cont_message and True or False,
|
||||||
skip_utf8_validation=skip_utf8_validation)
|
skip_utf8_validation=skip_utf8_validation)
|
||||||
self.sock.settimeout(getdefaulttimeout())
|
self.sock.settimeout(getdefaulttimeout())
|
||||||
self.sock.connect(self.url, header=self.header, cookie=self.cookie,
|
self.sock.connect(self.url, header=self.header, cookie=self.cookie,
|
||||||
http_proxy_host=http_proxy_host, http_proxy_port=http_proxy_port,
|
http_proxy_host=http_proxy_host,
|
||||||
|
http_proxy_port=http_proxy_port,
|
||||||
http_no_proxy=http_no_proxy, http_proxy_auth=http_proxy_auth,
|
http_no_proxy=http_no_proxy, http_proxy_auth=http_proxy_auth,
|
||||||
subprotocols=self.subprotocols)
|
subprotocols=self.subprotocols)
|
||||||
self._callback(self.on_open)
|
self._callback(self.on_open)
|
||||||
|
|
||||||
if ping_interval:
|
if ping_interval:
|
||||||
event = threading.Event()
|
event = threading.Event()
|
||||||
thread = threading.Thread(target=self._send_ping, args=(ping_interval, event))
|
thread = threading.Thread(target=self._send_ping, args=(ping_interval, event))
|
||||||
thread.setDaemon(True)
|
thread.setDaemon(True)
|
||||||
thread.start()
|
thread.start()
|
||||||
@@ -194,7 +204,7 @@ class WebSocketApp(object):
|
|||||||
*self._get_close_args(close_frame.data if close_frame else None))
|
*self._get_close_args(close_frame.data if close_frame else None))
|
||||||
self.sock = None
|
self.sock = None
|
||||||
|
|
||||||
def _get_close_args(self,data):
|
def _get_close_args(self, data):
|
||||||
""" this functions extracts the code, reason from the close body
|
""" this functions extracts the code, reason from the close body
|
||||||
if they exists, and if the self.on_close except three arguments """
|
if they exists, and if the self.on_close except three arguments """
|
||||||
import inspect
|
import inspect
|
||||||
@@ -202,19 +212,19 @@ class WebSocketApp(object):
|
|||||||
if not self.on_close or len(inspect.getargspec(self.on_close).args) != 3:
|
if not self.on_close or len(inspect.getargspec(self.on_close).args) != 3:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
if data and len(data) >=2:
|
if data and len(data) >= 2:
|
||||||
code = 256*six.byte2int(data[0:1]) + six.byte2int(data[1:2])
|
code = 256*six.byte2int(data[0:1]) + six.byte2int(data[1:2])
|
||||||
reason = data[2:].decode('utf-8')
|
reason = data[2:].decode('utf-8')
|
||||||
return [code,reason]
|
return [code, reason]
|
||||||
|
|
||||||
return [None,None]
|
return [None, None]
|
||||||
|
|
||||||
def _callback(self, callback, *args):
|
def _callback(self, callback, *args):
|
||||||
if callback:
|
if callback:
|
||||||
try:
|
try:
|
||||||
callback(self, *args)
|
callback(self, *args)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(e)
|
error(e)
|
||||||
if logger.isEnabledFor(logging.DEBUG):
|
if isEnableForDebug():
|
||||||
_, _, tb = sys.exc_info()
|
_, _, tb = sys.exc_info()
|
||||||
traceback.print_tb(tb)
|
traceback.print_tb(tb)
|
||||||
|
Reference in New Issue
Block a user