Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_connect_and_close(self, sock):
s = MagicMock()
sock.socket.return_value = s
sock.getaddrinfo.return_value = [(socket.AF_INET, socket.SOCK_STREAM, 0, "",
("127.0.0.1", 80, 0, 0))]
c = WebSocketBaseClient(url="ws://127.0.0.1/?token=value")
s.recv.return_value = b"\r\n".join([
b"HTTP/1.1 101 Switching Protocols",
b"Connection: Upgrade",
b"Sec-Websocket-Version: 13",
b"Content-Type: text/plain;charset=utf-8",
b"Sec-Websocket-Accept: " + b64encode(sha1(c.key + WS_KEY).digest()),
b"Upgrade: websocket",
b"Date: Sun, 26 Jul 2015 12:32:55 GMT",
b"Server: ws4py/test",
b"\r\n"
])
c.connect()
s.connect.assert_called_once_with(("127.0.0.1", 80))
def test_parse_unix_schemes(self):
c = WebSocketBaseClient(url="ws+unix:///my.socket")
self.assertEqual(c.scheme, "ws+unix")
self.assertEqual(c.host, "localhost")
self.assertIsNone(c.port)
self.assertEqual(c.unix_socket_path, "/my.socket")
self.assertEqual(c.resource, "/")
self.assertEqual(c.bind_addr, "/my.socket")
c = WebSocketBaseClient(url="wss+unix:///my.socket")
self.assertEqual(c.scheme, "wss+unix")
self.assertEqual(c.host, "localhost")
self.assertIsNone(c.port)
self.assertEqual(c.unix_socket_path, "/my.socket")
self.assertEqual(c.resource, "/")
self.assertEqual(c.bind_addr, "/my.socket")
def test_parse_ws_scheme(self):
c = WebSocketBaseClient(url="ws://127.0.0.1/")
self.assertEqual(c.scheme, "ws")
self.assertEqual(c.host, "127.0.0.1")
self.assertEqual(c.port, 80)
self.assertEqual(c.resource, "/")
self.assertEqual(c.bind_addr, ("127.0.0.1", 80))
def test_parse_ws_scheme_with_query_string(self):
c = WebSocketBaseClient(url="ws://127.0.0.1/?token=value")
self.assertEqual(c.scheme, "ws")
self.assertEqual(c.host, "127.0.0.1")
self.assertEqual(c.port, 80)
self.assertEqual(c.resource, "/?token=value")
self.assertEqual(c.bind_addr, ("127.0.0.1", 80))
def test_parse_wss_scheme_with_query_string(self):
c = WebSocketBaseClient(url="wss://127.0.0.1/?token=value")
self.assertEqual(c.scheme, "wss")
self.assertEqual(c.host, "127.0.0.1")
self.assertEqual(c.port, 443)
self.assertEqual(c.resource, "/?token=value")
self.assertEqual(c.bind_addr, ("127.0.0.1", 443))
def test_parse_ws_scheme_when_missing_resource(self):
c = WebSocketBaseClient(url="ws://127.0.0.1")
self.assertEqual(c.scheme, "ws")
self.assertEqual(c.host, "127.0.0.1")
self.assertEqual(c.port, 80)
self.assertEqual(c.resource, "/")
self.assertEqual(c.bind_addr, ("127.0.0.1", 80))
# -*- coding: utf-8 -*-
import copy
import gevent
from gevent import Greenlet
from gevent.queue import Queue
from ws4py.client import WebSocketBaseClient
__all__ = ['WebSocketClient']
class WebSocketClient(WebSocketBaseClient):
def __init__(self, url, protocols=None, extensions=None, heartbeat_freq=None, ssl_options=None, headers=None, exclude_headers=None):
"""
WebSocket client that executes the
:meth:`run() ` into a gevent greenlet.
.. code-block:: python
ws = WebSocketClient('ws://localhost:9000/echo', protocols=['http-only', 'chat'])
ws.connect()
ws.send("Hello world")
def incoming():
while True:
m = ws.receive()
if m is not None:
# -*- coding: utf-8 -*-
import ssl
from tornado import iostream, escape
from ws4py.client import WebSocketBaseClient
from ws4py.exc import HandshakeError
__all__ = ['TornadoWebSocketClient']
class TornadoWebSocketClient(WebSocketBaseClient):
def __init__(self, url, protocols=None, extensions=None,
io_loop=None, ssl_options=None, headers=None, exclude_headers=None):
"""
.. code-block:: python
from tornado import ioloop
class MyClient(TornadoWebSocketClient):
def opened(self):
for i in range(0, 200, 25):
self.send("*" * i)
def received_message(self, m):
print((m, len(str(m))))
def closed(self, code, reason=None):
# -*- coding: utf-8 -*-
import ssl
from tornado import iostream, escape
from ws4py.client import WebSocketBaseClient
from ws4py.exc import HandshakeError
__all__ = ['TornadoWebSocketClient']
class TornadoWebSocketClient(WebSocketBaseClient):
def __init__(self, url, protocols=None, extensions=None,
io_loop=None, ssl_options=None, headers=None):
"""
.. code-block:: python
from tornado import ioloop
class MyClient(TornadoWebSocketClient):
def opened(self):
for i in range(0, 200, 25):
self.send("*" * i)
def received_message(self, m):
print((m, len(str(m))))
def closed(self, code, reason=None):