Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import time
import json
import multiprocessing
import pytest
from tornado import ioloop
from ws4py.client.tornadoclient import TornadoWebSocketClient
from omnibus.daemon import spawn_omnibusd
from omnibus.pubsub import PubSub
from .helpers import AssertEventuallyTest
class WebsocketClient(TornadoWebSocketClient):
def opened(self):
print('opened, send authenticate')
self.send('!authenticate:123456')
time.sleep(.1)
def received_message(self, message):
print('received message', message, message.data)
type, payload = message.data.split(':', 1)
if type == '!authenticate' and json.loads(payload).get('success', False):
self.send('!subscribe:channel')
elif type == '!subscribe' and json.loads(payload).get('success', False):
self.send('channel:{"type": "type", "sender": null, "payload": {"test": "works!"}}')
time.sleep(.2)
self.send('channel:{"type": "type", "sender": null, "payload": {"test": "works2!"}}')
def test_authenticated_user_should_manage_gate(self):
token = ApiView._create_token('admin')
self.view.authenticate(token)
result = self.view.list_gates()
self.assertTrue(result[0]['managed'])
def test_authenticated_user_make_an_access_request(self):
token = ApiView._create_token('admin')
self.view.authenticate(token)
self.view.open(1)
self.assertEqual(1, AccessRequest.objects.all().count())
class AsyncWSClient(TornadoWebSocketClient):
def __init__(self, url, ioLoop=None, **kwargs):
TornadoWebSocketClient.__init__(self, url, io_loop=ioLoop, **kwargs)
self._callback = None
self._message = None
def opened(self):
"""called when web socket opened"""
self.send(self._message, binary=False)
def received_message(self, message):
"""Received a message"""
self.close()
if self._callback:
self._callback(message.data)
def __init__(self, url, io_loop, callback, message):
TornadoWebSocketClient.__init__(self, url, io_loop=io_loop)
self._callback = callback
self._message = message
self.connect()
def __init__(self, url, io_loop, callback, message):
TornadoWebSocketClient.__init__(self, url, io_loop=io_loop)
self._callback = callback
self._message = message
self.connect()
def stop(self):
if self.__callback is not None:
self.__callback.stop()
# Override to send the keep alive msg.
def sendKeepAlive(self):
raise NotImplementedError()
# Return True if the response belongs to a keep alive message, False otherwise.
def handleResponse(self, msg):
raise NotImplementedError()
# Base clase for websocket clients.
# To use it call connect and startClient, and stopClient.
class WebSocketClientBase(tornadoclient.TornadoWebSocketClient):
def __init__(self, url):
super().__init__(url)
self.__keepAliveMgr = None
self.__connected = False
# This is to avoid a stack trace because TornadoWebSocketClient is not implementing _cleanup.
def _cleanup(self):
ret = None
try:
ret = super()._cleanup()
except Exception:
pass
return ret
def __init__(self, url):
tornadoclient.TornadoWebSocketClient.__init__(self, url)
self.__keepAliveMgr = None
self.__connected = False
from tornado import ioloop
from ws4py.client.tornadoclient import TornadoWebSocketClient
from pytun import TunTapDevice, IFF_TAP, IFF_NO_PI
import sys
io_loop = ioloop.IOLoop.instance()
tap = TunTapDevice(flags=IFF_TAP|IFF_NO_PI, name='vpn-ws%d')
class VpnWSClient(TornadoWebSocketClient):
def received_message(self, m):
tap.write(str(m))
def closed(self, code, reason=None):
print "ooops"
ws = VpnWSClient(sys.argv[1])
ws.connect()
def tap_callback(fd, event):
ws.send(tap.read(tap.mtu), binary=True)
io_loop.add_handler(tap.fileno(), tap_callback, io_loop.READ)
io_loop.start()
def stop(self):
if self.__callback is not None:
self.__callback.stop()
# Override to send the keep alive msg.
def sendKeepAlive(self):
raise NotImplementedError()
# Return True if the response belongs to a keep alive message, False otherwise.
def handleResponse(self, msg):
raise NotImplementedError()
# Base clase for websocket clients.
# To use it call connect and startClient, and stopClient.
class WebSocketClientBase(tornadoclient.TornadoWebSocketClient):
def __init__(self, url):
super(WebSocketClientBase, self).__init__(url)
self.__keepAliveMgr = None
self.__connected = False
# This is to avoid a stack trace because TornadoWebSocketClient is not implementing _cleanup.
def _cleanup(self):
ret = None
try:
ret = super(WebSocketClientBase, self)._cleanup()
except Exception:
pass
return ret
def getIOLoop(self):
return tornado.ioloop.IOLoop.instance()
if self.__callback is not None:
self.__callback.stop()
# Override to send the keep alive msg.
def sendKeepAlive(self):
raise NotImplementedError()
# Return True if the response belongs to a keep alive message, False otherwise.
def handleResponse(self, msg):
raise NotImplementedError()
# Base clase for websocket clients.
# To use it call connect and startClient, and stopClient.
# Note that this class has thread affinity, so build it and use it from the same thread.
class WebSocketClientBase(tornadoclient.TornadoWebSocketClient):
def __init__(self, url):
super(WebSocketClientBase, self).__init__(url)
self.__keepAliveMgr = None
self.__connected = False
# This is to avoid a stack trace because TornadoWebSocketClient is not implementing _cleanup.
def _cleanup(self):
ret = None
try:
ret = super(WebSocketClientBase, self)._cleanup()
except Exception:
pass
return ret
def getIOLoop(self):
return tornado.ioloop.IOLoop.instance()
# MtGox sends back the invalid message response as a remark.
ret = False
if data["op"] == "remark":
if data.get("debug", {}).get("data_raw") == KeepAliveMgr.HEART_BEAT_MSG:
self.__msgsWithoutResponse = 0
ret = True
return ret
def start(self):
self.__callback.start()
def stop(self):
self.__callback.stop()
class WebSocketClientBase(tornadoclient.TornadoWebSocketClient):
KEEP_ALIVE_FREQUENCY = 5*1000
KEEP_ALIVE_MAX_MSG = 5
def __init__(self, url):
tornadoclient.TornadoWebSocketClient.__init__(self, url)
self.__keepAliveMgr = None
self.__connected = False
# This is to avoid a stack trace because TornadoWebSocketClient is not implementing _cleanup.
def _cleanup(self):
ret = None
try:
ret = tornadoclient.TornadoWebSocketClient._cleanup(self)
except Exception:
pass
return ret