class WSGIServer(_WSGIServer): def initialize_websockets_manager(self): """ Call thos to start the underlying websockets manager. Make sure to call it once your server is created. """ self.manager = WebSocketManager() self.manager.start() def shutdown_request(self, request): """ The base class would close our socket if we didn't override it. """ pass def link_websocket_to_server(self, ws): """ Call this from your WSGI handler when a websocket has been created. """ self.manager.add(ws) def server_close(self): """ Properly initiate closing handshakes on all websockets when the WSGI server terminates. """ if hasattr(self, 'manager'): self.manager.close_all() self.manager.stop() self.manager.join() delattr(self, 'manager') _WSGIServer.server_close(self)
def test_mainloop_can_be_stopped_when_no_websocket_were_registered(self, MockSelectPoller): m = WebSocketManager(poller=MockSelectPoller()) self.assertFalse(m.running) m.start() self.assertTrue(m.running) m.stop() self.assertFalse(m.running)
def test_mainloop_can_be_stopped_when_no_websocket_were_registered( self, MockSelectPoller): m = WebSocketManager(poller=MockSelectPoller()) self.assertFalse(m.running) m.start() self.assertTrue(m.running) m.stop() self.assertFalse(m.running)
def test_mainloop_can_be_stopped(self, MockSelectPoller): m = WebSocketManager(poller=MockSelectPoller()) def poll(): yield 1 m.stop() yield 2 m.poller.poll.return_value = poll() self.assertFalse(m.running) m.start() # just make sure it had the time to finish time.sleep(0.1) self.assertFalse(m.running)
def test_websocket_terminated_from_mainloop(self, MockSelectPoller): m = WebSocketManager(poller=MockSelectPoller()) m.poller.poll.return_value = [1] ws = MagicMock() ws.terminated = False ws.sock.fileno.return_value = 1 ws.once.return_value = False m.add(ws) m.start() ws.terminate.assert_call_once_with() m.stop()
class WebSocketPlugin(plugins.SimplePlugin): def __init__(self, bus): plugins.SimplePlugin.__init__(self, bus) self.manager = WebSocketManager() def start(self): self.bus.log("Starting WebSocket processing") self.bus.subscribe('stop', self.cleanup) self.bus.subscribe('handle-websocket', self.handle) self.bus.subscribe('websocket-broadcast', self.broadcast) self.manager.start() def stop(self): self.bus.log("Terminating WebSocket processing") self.bus.unsubscribe('stop', self.cleanup) self.bus.unsubscribe('handle-websocket', self.handle) self.bus.unsubscribe('websocket-broadcast', self.broadcast) def handle(self, ws_handler, peer_addr): """ Tracks the provided handler. :param ws_handler: websocket handler instance :param peer_addr: remote peer address for tracing purpose """ self.manager.add(ws_handler) def cleanup(self): """ Terminate all connections and clear the pool. Executed when the engine stops. """ self.manager.close_all() self.manager.stop() self.manager.join() def broadcast(self, message, binary=False): """ Broadcasts a message to all connected clients known to the server. :param message: a message suitable to pass to the send() method of the connected handler. :param binary: whether or not the message is a binary one """ self.manager.broadcast(message, binary)
def run(script_options): global logger level = logging.DEBUG if script_options.verbose else logging.INFO logger = configure_logger(level = level) mgr = WebSocketManager() try: mgr.start() clients = [] # Connect for connection_idx in range(script_options.concurrency): client = EchoClient(script_options.url, mgr, script_options.ca, script_options.key, script_options.cert) client.connect() clients.append(client) logger.info("%d clients are connected" % (connection_idx + 1)) # Send msg = getMessage(script_options) if msg: msg = json.write(msg) logger.info("Sending messages (num=%d):\n%s", script_options.num, msg) for client in clients: for _ in range(script_options.num): client.send(msg) time.sleep(SEND_INTERVAL) logger.info("Done sending") # Sleep before disconnecting logger.info("Sleeping for %d s before disconnecting", script_options.interval) time.sleep(script_options.interval) except KeyboardInterrupt: logger.info("Interrupted by user") finally: logger.info("Disconnecting!") mgr.close_all(code = 1000, message = "Client is closing the connection") mgr.stop() mgr.join()
def execute(self, commands, environment={}): """Execute a command on the container. In pylxd 2.2, this method will be renamed `execute` and the existing `execute` method removed. """ if not _ws4py_installed: raise ValueError( 'This feature requires the optional ws4py library.') if isinstance(commands, six.string_types): raise TypeError("First argument must be a list.") response = self.api['exec'].post(json={ 'command': commands, 'environment': environment, 'wait-for-websocket': True, 'interactive': False, }) fds = response.json()['metadata']['metadata']['fds'] operation_id = response.json()['operation'].split('/')[-1] parsed = parse.urlparse( self.client.api.operations[operation_id].websocket._api_endpoint) manager = WebSocketManager() stdin = _StdinWebsocket(self.client.websocket_url) stdin.resource = '{}?secret={}'.format(parsed.path, fds['0']) stdin.connect() stdout = _CommandWebsocketClient(manager, self.client.websocket_url) stdout.resource = '{}?secret={}'.format(parsed.path, fds['1']) stdout.connect() stderr = _CommandWebsocketClient(manager, self.client.websocket_url) stderr.resource = '{}?secret={}'.format(parsed.path, fds['2']) stderr.connect() manager.start() while len(manager.websockets.values()) > 0: time.sleep(.1) operation = self.client.operations.get(operation_id) return _ContainerExecuteResult( operation.metadata['return'], stdout.data, stderr.data)
def execute(self, commands, environment={}): """Execute a command on the container.""" if not _ws4py_installed: raise ValueError( 'This feature requires the optional ws4py library.') if isinstance(commands, six.string_types): raise TypeError("First argument must be a list.") response = self.api['exec'].post( json={ 'command': commands, 'environment': environment, 'wait-for-websocket': True, 'interactive': False, }) fds = response.json()['metadata']['metadata']['fds'] operation_id = response.json()['operation'].split('/')[-1] parsed = parse.urlparse( self.client.api.operations[operation_id].websocket._api_endpoint) manager = WebSocketManager() stdin = _StdinWebsocket(manager, self.client.websocket_url) stdin.resource = '{}?secret={}'.format(parsed.path, fds['0']) stdin.connect() stdout = _CommandWebsocketClient(manager, self.client.websocket_url) stdout.resource = '{}?secret={}'.format(parsed.path, fds['1']) stdout.connect() stderr = _CommandWebsocketClient(manager, self.client.websocket_url) stderr.resource = '{}?secret={}'.format(parsed.path, fds['2']) stderr.connect() manager.start() while True: # pragma: no cover for websocket in manager.websockets.values(): if not websocket.terminated: break else: break time.sleep(1) return stdout.data, stderr.data
def execute(self, commands, environment={}): """Execute a command on the container.""" if isinstance(commands, six.string_types): raise TypeError("First argument must be a list.") response = self.api['exec'].post(json={ 'command': commands, 'environment': environment, 'wait-for-websocket': True, 'interactive': False, }) fds = response.json()['metadata']['metadata']['fds'] operation_id = response.json()['operation'].split('/')[-1] parsed = parse.urlparse( self.client.api.operations[operation_id].websocket._api_endpoint) manager = WebSocketManager() stdin = _StdinWebsocket(manager, self.client.websocket_url) stdin.resource = '{}?secret={}'.format(parsed.path, fds['0']) stdin.connect() stdout = _CommandWebsocketClient(manager, self.client.websocket_url) stdout.resource = '{}?secret={}'.format(parsed.path, fds['1']) stdout.connect() stderr = _CommandWebsocketClient(manager, self.client.websocket_url) stderr.resource = '{}?secret={}'.format(parsed.path, fds['2']) stderr.connect() manager.start() while True: # pragma: no cover for websocket in manager.websockets.values(): if not websocket.terminated: break else: break time.sleep(1) return stdout.data, stderr.data
class PushBulletWSClient(WebSocketBaseClient): name = "pushbullet" def __init__(self, interface, url): """ Initializes the PB WS Client""" super().__init__(url) self.pb = Pushbullet(cfg.PUSHBULLET_KEY) self.manager = WebSocketManager() self.interface = interface self.device = None for i, device in enumerate(self.pb.devices): if device.nickname == 'pai': logger.debug("Device found") self.device = device break else: logger.exception("Device not found. Creating 'pai' device") self.device = self.pb.new_device(nickname='pai', icon='system') def stop(self): self.terminate() self.manager.stop() def handshake_ok(self): """ Callback trigger when connection succeeded""" logger.info("Handshake OK") self.manager.add(self) self.manager.start() for chat in self.pb.chats: logger.debug("Associated contacts: {}".format(chat)) # Receiving pending messages self.received_message(json.dumps({ "type": "tickle", "subtype": "push" })) self.send_message("Active") def received_message(self, message): """ Handle Pushbullet message. It should be a command """ logger.debug("Received Message {}".format(message)) try: message = json.loads(str(message)) except: logger.exception("Unable to parse message") return if message['type'] == 'tickle' and message['subtype'] == 'push': now = time.time() pushes = self.pb.get_pushes(modified_after=int(now) - 20, limit=1, filter_inactive=True) for p in pushes: # Ignore messages send by us if p.get('direction') == 'self' and p.get('title') == 'pai': #logger.debug('Ignoring message sent') continue if p.get('direction') == 'outgoing' or p.get('dismissed'): #logger.debug('Ignoring outgoing dismissed') continue if p.get('sender_email_normalized' ) in cfg.PUSHBULLET_CONTACTS or p.get( 'direction') == 'self': ret = self.interface.handle_command(p.get('body')) m = "PB {}: {}".format(p.get('sender_email_normalized'), ret) logger.info(m) else: m = "PB {} (UNK): {}".format( p.get('sender_email_normalized'), p.get('body')) logger.warning(m) self.send_message(m) ps.sendNotification( Notification(sender=self.name, message=m, level=EventLevel.INFO)) def unhandled_error(self, error): logger.error("{}".format(error)) try: self.terminate() except Exception: logger.exception("Closing Pushbullet WS") self.close() def send_message(self, msg, dstchat=None): if dstchat is None: dstchat = self.pb.chats if not isinstance(dstchat, list): dstchat = [dstchat] # Push to self self.device.push_note("pai", msg) for chat in dstchat: if chat.email in cfg.PUSHBULLET_CONTACTS: try: self.pb.push_note("pai", msg, chat=chat) except Exception: logger.exception("Sending message") time.sleep(5)
class Script: POLL_TIMEOUT = timedelta(milliseconds=100) class NullWebSocket(WebSocketBaseClient): def handshake_ok(self): self.close() class WebSocket(WebSocketBaseClient): def __init__(self, manager: WebSocketManager, handler: BufferHandler, *args, **kwargs) -> None: self.manager = manager self.handler = handler super(Script.WebSocket, self).__init__(*args, **kwargs) def handshake_ok(self) -> None: self.manager.add(self) def received_message(self, message: ws4py.messaging.TextMessage) -> None: if len(message.data) == 0: self.close() self.manager.remove(self) if message.encoding: decoded = message.data.decode(message.encoding) else: decoded = message.data.decode('utf-8') self.handler.handle_message(decoded) def __init__(self, job: Job, repository_path: Path, lxd_client: pylxd.Client, lxd_profiles: List[str]) -> None: self._job = job self._lxd_client = lxd_client self._repository_path = repository_path self._lxd_profiles = lxd_profiles def __enter__(self): self._container = None self._container_name = 'piper' + uuid.uuid4().hex container_config = { 'name': self._container_name, 'profiles': self._lxd_profiles, 'source': self._job.lxd_source, 'devices': { 'piper_repository': { 'type': 'disk', 'path': '/piper', 'source': str(self._repository_path), } } } try: self._container = self._lxd_client.containers.create( container_config, wait=True) except LXDAPIException as e: raise PScriptException('Failed to create LXD container. Raw: ' + str(e)) try: self._container.start(wait=True) except LXDAPIException as e: raise PScriptException('Failed to start LXD container. Raw: ' + str(e)) env = {k: str(v) for k, v in self._job.env.items()} config = { 'command': ['/bin/sh', '-c', self._job.script], 'wait-for-websocket': True, 'interactive': False, 'environment': env, } try: response = self._lxd_client.api.containers[ self._container_name].exec.post(json=config) except LXDAPIException as e: raise PScriptException('Failed to execute command. Raw: ' + str(e)) fds = response.json()['metadata']['metadata']['fds'] self.operation_id = response.json()['operation'].split('/')[-1] websocket_url = '/1.0/operations/{}/websocket'.format( self.operation_id) stdin_url = '{}?secret={}'.format(websocket_url, fds['0']) stdout_url = '{}?secret={}'.format(websocket_url, fds['1']) stderr_url = '{}?secret={}'.format(websocket_url, fds['2']) stdin = self.NullWebSocket(self._lxd_client.websocket_url) stdin.resource = stdin_url stdin.connect() self.manager = WebSocketManager() self.manager.start() self._handler = BufferHandler() stdout = self.WebSocket(self.manager, self._handler, self._lxd_client.websocket_url) stdout.resource = stdout_url stdout.connect() stderr = self.WebSocket(self.manager, self._handler, self._lxd_client.websocket_url) stderr.resource = stderr_url stderr.connect() return self def __exit__(self, exc_type, exc_val, exc_tb): if self._container is not None: try: self._container.stop(wait=True) except pylxd.exceptions.LXDAPIException: pass try: self._container.delete() except pylxd.exceptions.LXDAPIException as e: message = 'Failed to delete LXD container "{}". Raw: '.format( self._container_name) + str(e) raise PScriptException(message) def poll(self, timeout: timedelta) -> str: while timeout > timedelta(0): if len(self.manager.websockets.values()) == 0: break if timeout > self.POLL_TIMEOUT: sleep(self.POLL_TIMEOUT.total_seconds()) else: sleep(timeout.total_seconds()) timeout -= self.POLL_TIMEOUT return self._handler.pop() @property def status(self) -> Optional[int]: if not self.operation_id: return None result = self._lxd_client.operations.get(self.operation_id) return result.status_code
print str(msg) pins = eval(str(msg)) for pin in pins: gpio.pinMode(pin, gpio.OUTPUT) gpio.digitalWrite(pin,int(pins[pin])) def tempread(pin): value = analog_read(pin) tempr = (value * 3.3)/4096*100 return tempr if __name__ == '__main__': import time try: m.start() client = EchoClient('ws://tairy.me:8888/ws') client.connect() sendmes = {"userid":userid} while True: for ws in m.websockets.itervalues(): if not ws.terminated: sendmes["data"] = {"temp":tempread(2)} ws.send(json.JSONEncoder().encode(sendmes)) break else: break time.sleep(3) except KeyboardInterrupt: m.close_all()
get_storm_usd() def Timers(): get_btc_usd() get_xrb_btc() get_storm_usd() threading.Timer( 1, Timers).start() # Set update Price thread to run every X seconds my_coins = setup_secret.PORTFOLIO prev_total_val = 0. #For total price difference calculation try: MANAGER.start() for coin in my_coins.values(): client = CryptoClient( 'wss://stream.binance.com:9443/ws/{0}@kline_1m'.format( coin['code'])) client.set_info(from_crypto=coin['name'], to_crypto=coin['conv'], amount=coin['quan']) coin, btc_val = client.get_btc_value() coin_queue.values[coin] = btc_val client.connect() while True: Timers() for ws in MANAGER.websockets.values(): try:
with open( "/test-api/websocket-client/return_data/return_login/Login_error.txt", '+a') as f: f.write('The {}user login failed!\n'.format(count)) self.close() def closed(self, code, reason): print("closed!") if __name__ == '__main__': import time Instant_Pending = json.dumps(CreateData()) count = 0 try: m.start() start_time = time.time() for i in user_list: payload = json.dumps({"Type": 7, "Data": i}) print(type(payload)) client = EchoClient(api_url) client.connect() count += 1 logger.info("%s clients are connected" % count) while True: for ws in m.websockets.values(): if not ws.terminated: dur_time = time.time() - start_time print(dur_time) break
class FileServer(object): """ Serves static files from a directory. """ def __init__(self, path): """ path is directory where static files are stored """ self.path = path self.wsapp = WebSocketWSGIApplication(handler_cls=EchoWebSocket) self.manager = WebSocketManager() self.manager.start() def __call__(self, environ, start_response): """ WSGI entry point """ # Upgrade header means websockets... upgrade_header = environ.get('HTTP_UPGRADE', '').lower() if upgrade_header: environ['ws4py.socket'] = get_connection(environ['wsgi.input']) # This will make a websocket, hopefully! ret = self.wsapp(environ, start_response) if 'ws4py.websocket' in environ: self.manager.add(environ.pop('ws4py.websocket')) return ret # Find path to file to server path_info = environ["PATH_INFO"] if not path_info or path_info == "/": path_info = "/index.html" file_path = os.path.join(self.path, path_info[1:]) # If file does not exist, return 404 if not os.path.exists(file_path): return self._not_found(start_response) # Guess mimetype of file based on file extension mimetype = mimetypes.guess_type(file_path)[0] # If we can't guess mimetype, return a 403 Forbidden if mimetype is None: return self._forbidden(start_response) # Get size of file size = os.path.getsize(file_path) # Create headers and start response headers = [ ("Content-type", mimetype), ("Content-length", str(size)), ] start_response("200 OK", headers) # Send file return self._send_file(file_path, size) def _send_file(self, file_path, size): """ A generator function which returns the blocks in a file, one at a time. """ with open(file_path) as f: block = f.read(BLOCK_SIZE) while block: yield block block = f.read(BLOCK_SIZE) def _not_found(self, start_response): start_response("404 NOT FOUND", [("Content-type", "text/plain")]) return ["Not found", ] def _forbidden(self, start_response): start_response("403 FORBIDDEN", [("Content-type", "text/plain")]) return ["Forbidden", ]
class PushBulletWSClient(WebSocketBaseClient): def init(self, interface): """ Initializes the PB WS Client""" self.logger = logging.getLogger('PAI').getChild(__name__) self.pb = Pushbullet(cfg.PUSHBULLET_KEY, cfg.PUSHBULLET_SECRET) self.manager = WebSocketManager() self.alarm = None self.interface = interface def stop(self): self.terminate() self.manager.stop() def set_alarm(self, alarm): """ Sets the paradox alarm object """ self.alarm = alarm def handshake_ok(self): """ Callback trigger when connection succeeded""" self.logger.info("Handshake OK") self.manager.add(self) self.manager.start() for chat in self.pb.chats: self.logger.debug("Associated contacts: {}".format(chat)) # Receiving pending messages self.received_message(json.dumps({"type": "tickle", "subtype": "push"})) self.send_message("Active") def received_message(self, message): """ Handle Pushbullet message. It should be a command """ self.logger.debug("Received Message {}".format(message)) try: message = json.loads(str(message)) except: self.logger.exception("Unable to parse message") return if self.alarm is None: return if message['type'] == 'tickle' and message['subtype'] == 'push': now = time.time() pushes = self.pb.get_pushes(modified_after=int(now) - 20, limit=1, filter_inactive=True) self.logger.debug("got pushes {}".format(pushes)) for p in pushes: self.pb.dismiss_push(p.get("iden")) self.pb.delete_push(p.get("iden")) if p.get('direction') == 'outgoing' or p.get('dismissed'): continue if p.get('sender_email_normalized') in cfg.PUSHBULLET_CONTACTS: ret = self.interface.send_command(p.get('body')) if ret: self.logger.info("From {} ACCEPTED: {}".format(p.get('sender_email_normalized'), p.get('body'))) else: self.logger.warning("From {} UNKNOWN: {}".format(p.get('sender_email_normalized'), p.get('body'))) else: self.logger.warning("Command from INVALID SENDER {}: {}".format(p.get('sender_email_normalized'), p.get('body'))) def unhandled_error(self, error): self.logger.error("{}".format(error)) try: self.terminate() except Exception: self.logger.exception("Closing Pushbullet WS") self.close() def send_message(self, msg, dstchat=None): if dstchat is None: dstchat = self.pb.chats if not isinstance(dstchat, list): dstchat = [dstchat] for chat in dstchat: if chat.email in cfg.PUSHBULLET_CONTACTS: try: self.pb.push_note("paradox", msg, chat=chat) except Exception: self.logger.exception("Sending message") time.sleep(5) def notify(self, source, message, level): try: if level.value >= EventLevel.WARN.value: self.send_message("{}".format(message)) except Exception: logging.exception("Pushbullet notify")
def start_data_collection(conn_info): for (ip, port) in conn_info: ws_url = "ws://{}:{}/geophone".format(ip, port) print("Connecting to '{}'...".format(ws_url)) try: ws = DataReceiver(ws_url) ws.connect() except Exception as e: print("Unable to connect to '{}' (it probably timed out after {}s). Reason: {}".format(ws_url, WS_TIMEOUT, e)) if __name__ == '__main__': try: ws_manager.start() # start_data_collection([('10.0.0.110', 81)]) start_data_collection([('192.168.0.99', 81),('192.168.0.100', 81),('192.168.0.105', 81),('192.168.0.108', 81),('192.168.0.109', 81),('192.168.0.114', 81),('192.168.0.115', 81),('192.168.0.116', 81),('192.168.0.117', 81),('192.168.0.118', 81)]) while True: for ws in ws_manager.websockets.itervalues(): if not ws.terminated: break else: break time.sleep(3) except KeyboardInterrupt: ws_manager.close_all() ws_manager.stop() ws_manager.join()