@@ -50,11 +50,13 @@ import os
|
|||||||
import struct
|
import struct
|
||||||
import uuid
|
import uuid
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import threading
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
# websocket modules
|
# websocket modules
|
||||||
from ._exceptions import *
|
from ._exceptions import *
|
||||||
from ._abnf import ABNF
|
from ._abnf import ABNF
|
||||||
|
from ._utils import NoLock
|
||||||
|
|
||||||
"""
|
"""
|
||||||
websocket python client.
|
websocket python client.
|
||||||
@@ -197,11 +199,14 @@ def create_connection(url, timeout=None, **options):
|
|||||||
"cookie" -> cookie value.
|
"cookie" -> cookie value.
|
||||||
"http_proxy_host" - http proxy host name.
|
"http_proxy_host" - http proxy host name.
|
||||||
"http_proxy_port" - http proxy port. If not set, set to 80.
|
"http_proxy_port" - http proxy port. If not set, set to 80.
|
||||||
|
"enable_multithread" -> enable lock for multithread.
|
||||||
"""
|
"""
|
||||||
sockopt = options.get("sockopt", [])
|
sockopt = options.get("sockopt", [])
|
||||||
sslopt = options.get("sslopt", {})
|
sslopt = options.get("sslopt", {})
|
||||||
fire_cont_frame = options.get("fire_cont_frame", False)
|
fire_cont_frame = options.get("fire_cont_frame", False)
|
||||||
websock = WebSocket(sockopt=sockopt, sslopt=sslopt, fire_cont_frame = fire_cont_frame)
|
enable_multithread = options.get("enable_multithread", False)
|
||||||
|
websock = WebSocket(sockopt=sockopt, sslopt=sslopt,
|
||||||
|
fire_cont_frame = fire_cont_frame, enable_multithread=enable_multithread)
|
||||||
websock.settimeout(timeout if timeout is not None else default_timeout)
|
websock.settimeout(timeout if timeout is not None else default_timeout)
|
||||||
websock.connect(url, **options)
|
websock.connect(url, **options)
|
||||||
return websock
|
return websock
|
||||||
@@ -314,10 +319,11 @@ class WebSocket(object):
|
|||||||
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: dict object for ssl socket option.
|
sslopt: dict object for ssl socket option.
|
||||||
fire_cont_frame: fire recv event for each cont frame. default is False
|
fire_cont_frame: fire recv event for each cont frame. default is False
|
||||||
|
enable_multithread: if set to True, lock send method.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, get_mask_key=None, sockopt=None, sslopt=None,
|
def __init__(self, get_mask_key=None, sockopt=None, sslopt=None,
|
||||||
fire_cont_frame=False):
|
fire_cont_frame=False, enable_multithread=False):
|
||||||
"""
|
"""
|
||||||
Initalize WebSocket object.
|
Initalize WebSocket object.
|
||||||
"""
|
"""
|
||||||
@@ -338,6 +344,10 @@ class WebSocket(object):
|
|||||||
# These buffer over the build-up of a single frame.
|
# These buffer over the build-up of a single frame.
|
||||||
self._frame_buffer = _FrameBuffer()
|
self._frame_buffer = _FrameBuffer()
|
||||||
self._cont_data = None
|
self._cont_data = None
|
||||||
|
if enable_multithread:
|
||||||
|
self.lock = threading.Lock()
|
||||||
|
else:
|
||||||
|
self.lock = NoLock()
|
||||||
|
|
||||||
def fileno(self):
|
def fileno(self):
|
||||||
return self.sock.fileno()
|
return self.sock.fileno()
|
||||||
@@ -582,9 +592,12 @@ class WebSocket(object):
|
|||||||
length = len(data)
|
length = len(data)
|
||||||
if traceEnabled:
|
if traceEnabled:
|
||||||
logger.debug("send: " + repr(data))
|
logger.debug("send: " + repr(data))
|
||||||
|
|
||||||
|
with self.lock:
|
||||||
while data:
|
while data:
|
||||||
l = self._send(data)
|
l = self._send(data)
|
||||||
data = data[l:]
|
data = data[l:]
|
||||||
|
|
||||||
return length
|
return length
|
||||||
|
|
||||||
|
|
||||||
|
29
websocket/_utils.py
Normal file
29
websocket/_utils.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
"""
|
||||||
|
websocket - WebSocket client library for Python
|
||||||
|
|
||||||
|
Copyright (C) 2010 Hiroki Ohtani(liris)
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class NoLock(object):
|
||||||
|
def __enter__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __exit__(self,type, value, traceback):
|
||||||
|
pass
|
||||||
|
|
Reference in New Issue
Block a user