def test_websocket_close_all(self, MockSelectPoller): m = WebSocketManager(poller=MockSelectPoller()) ws = MagicMock() m.add(ws) m.close_all() ws.close.assert_called_once_with(code=1001, reason='Server is shutting down')
def test_websocket_close_all(self, MockSelectPoller): m = WebSocketManager(poller=MockSelectPoller()) ws = MagicMock() m.add(ws) m.close_all() ws.terminate.assert_call_once_with(1001, 'Server is shutting down')
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 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 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 test_broadcast(self, MockSelectPoller): m = WebSocketManager(poller=MockSelectPoller()) ws = MagicMock() ws.terminated = False m.add(ws) m.broadcast(b'hello there') ws.send.assert_call_once_with(b'hello there')
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_add_and_remove_websocket(self, MockSelectPoller): m = WebSocketManager(poller=MockSelectPoller()) ws = MagicMock() ws.sock.fileno.return_value = 1 m.add(ws) m.poller.register.assert_call_once_with(ws) m.remove(ws) m.poller.unregister.assert_call_once_with(ws)
def test_cannot_add_websocket_more_than_once(self, MockSelectPoller): m = WebSocketManager(poller=MockSelectPoller()) ws = MagicMock() ws.sock.fileno.return_value = 1 m.add(ws) self.assertEqual(len(m), 1) m.add(ws) self.assertEqual(len(m), 1)
def test_broadcast_failure_must_not_break_caller(self, MockSelectPoller): m = WebSocketManager(poller=MockSelectPoller()) ws = MagicMock() ws.terminated = False ws.send.side_effect = RuntimeError m.add(ws) try: m.broadcast(b'hello there') except: self.fail("Broadcasting shouldn't have failed")
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 __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 _configure_server(restarting=False): global websocket_plugin # Configure server error log cherrypy.config.update({'log.error_file': 'cherrypy.error.log'}) # Configure server url cherrypy.config.update({'server.socket_host': utils.s2n(autosubliminal.WEBSERVERIP), 'server.socket_port': int(autosubliminal.WEBSERVERPORT) }) # Disable engine plugins (no need for autoreload plugin) cherrypy.config.update({'engine.autoreload.on': False}) # Configure authentication in if a username and password is set by the user if autosubliminal.USERNAME and autosubliminal.PASSWORD: users = {utils.s2n(autosubliminal.USERNAME): utils.s2n(autosubliminal.PASSWORD)} cherrypy.config.update({'tools.auth_digest.on': True, 'tools.auth_digest.realm': 'Auto-Subliminal website', 'tools.auth_digest.get_ha1': auth_digest.get_ha1_dict_plain(users), 'tools.auth_digest.key': 'yek.tsegid_htua.lanimilbuS-otuA' # Can be any random string }) if not restarting: # Enable websocket plugin websocket_plugin = WebSocketPlugin(cherrypy.engine) websocket_plugin.subscribe() cherrypy.tools.websocket = WebSocketTool() else: # When restarting we need to create a new websocket manager thread (you cannot start the same thread twice!) websocket_plugin.manager = WebSocketManager() # When restarting we need to clear the httpserver to force the creation of a new one (needed for ip/port change) cherrypy.server.httpserver = None
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 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 test_cannot_remove_unregistered_websocket(self, MockSelectPoller): m = WebSocketManager(poller=MockSelectPoller()) ws = MagicMock() ws.sock.fileno.return_value = 1 m.remove(ws) self.assertEqual(len(m), 0) self.assertFalse(m.poller.unregister.called) m.add(ws) self.assertEqual(len(m), 1) m.remove(ws) self.assertEqual(len(m), 0) m.poller.unregister.assert_called_once_with(1) m.poller.reset_mock() m.remove(ws) self.assertEqual(len(m), 0) self.assertFalse(m.poller.unregister.called)
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
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()
def test_broadcast(self, MockSelectPoller): m = WebSocketManager(poller=MockSelectPoller()) ws = MagicMock() ws.terminated = False m.add(ws) m.broadcast(b'hello there') ws.send.assert_called_once_with(b'hello there', False)
def test_cannot_remove_unregistered_websocket(self, MockSelectPoller): m = WebSocketManager(poller=MockSelectPoller()) ws = MagicMock() ws.sock.fileno.return_value = 1 m.remove(ws) self.assertEqual(len(m), 0) self.assertFalse(m.poller.unregister.called) m.add(ws) self.assertEqual(len(m), 1) m.remove(ws) self.assertEqual(len(m), 0) m.poller.unregister.assert_call_once_with(ws) m.poller.reset_mock() m.remove(ws) self.assertEqual(len(m), 0) self.assertFalse(m.poller.unregister.called)
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 _configure_server(restarting=False): global websocket_plugin # Configure server error log cherrypy.config.update({'log.error_file': 'cherrypy.error.log'}) # Configure server url cherrypy.config.update({ 'server.socket_host': s2n(autosubliminal.WEBSERVERIP), 'server.socket_port': int(autosubliminal.WEBSERVERPORT) }) # Disable engine plugins (no need for autoreload plugin) cherrypy.config.update({'engine.autoreload.on': False}) # Read and store cherrypy server version (if not set, it returns CherryPy/Unknown because it's not installed) server_header = 'CherryPy/%s' % get_library_version('cherrypy') cherrypy.config.update({'response.headers.server': server_header}) # Configure authentication in if a username and password is set by the user if autosubliminal.USERNAME and autosubliminal.PASSWORD: users = {s2n(autosubliminal.USERNAME): s2n(autosubliminal.PASSWORD)} cherrypy.config.update({ 'tools.auth_digest.on': True, 'tools.auth_digest.realm': 'Auto-Subliminal website', 'tools.auth_digest.get_ha1': auth_digest.get_ha1_dict_plain(users), 'tools.auth_digest.key': 'yek.tsegid_htua.lanimilbuS-otuA' # Can be any random string }) # Configure our custom json_out_handler (Uncomment if it should be used for any @cherrypy.tools.json_out()) # cherrypy.config.update({'tools.json_out.handler': json_out_handler}) if not restarting: # Enable websocket plugin websocket_plugin = WebSocketPlugin(cherrypy.engine) websocket_plugin.subscribe() cherrypy.tools.websocket = WebSocketTool() else: # When restarting we need to create a new websocket manager thread (you cannot start the same thread twice!) websocket_plugin.manager = WebSocketManager() # When restarting we need to clear the httpserver to force the creation of a new one (needed for ip/port change) cherrypy.server.httpserver = None
def test_add_and_remove_websocket(self, MockSelectPoller): m = WebSocketManager(poller=MockSelectPoller()) ws = MagicMock() ws.sock.fileno.return_value = 1 m.add(ws) m.poller.register.assert_called_once_with(1) m.remove(ws) m.poller.unregister.assert_called_once_with(1)
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 execute(self, commands, environment={}, encoding=None, decode=True, stdin_payload=None, stdin_encoding="utf-8", stdout_handler=None, stderr_handler=None): """Execute a command on the container. In pylxd 2.2, this method will be renamed `execute` and the existing `execute` method removed. :param commands: The command and arguments as a list of strings :type commands: [str] :param environment: The environment variables to pass with the command :type environment: {str: str} :param encoding: The encoding to use for stdout/stderr if the param decode is True. If encoding is None, then no override is performed and whatever the existing encoding from LXD is used. :type encoding: str :param decode: Whether to decode the stdout/stderr or just return the raw buffers. :type decode: bool :param stdin_payload: Payload to pass via stdin :type stdin_payload: Can be a file, string, bytearray, generator or ws4py Message object :param stdin_encoding: Encoding to pass text to stdin (default utf-8) :param stdout_handler: Callable than receive as first parameter each message recived via stdout :type stdout_handler: Callable[[str], None] :param stderr_handler: Callable than receive as first parameter each message recived via stderr :type stderr_handler: Callable[[str], None] :raises ValueError: if the ws4py library is not installed. :returns: The return value, stdout and stdin :rtype: _ContainerExecuteResult() namedtuple """ 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) with managers.web_socket_manager(WebSocketManager()) as manager: stdin = _StdinWebsocket(self.client.websocket_url, payload=stdin_payload, encoding=stdin_encoding) stdin.resource = '{}?secret={}'.format(parsed.path, fds['0']) stdin.connect() stdout = _CommandWebsocketClient(manager, self.client.websocket_url, encoding=encoding, decode=decode, handler=stdout_handler) stdout.resource = '{}?secret={}'.format(parsed.path, fds['1']) stdout.connect() stderr = _CommandWebsocketClient(manager, self.client.websocket_url, encoding=encoding, decode=decode, handler=stderr_handler) stderr.resource = '{}?secret={}'.format(parsed.path, fds['2']) stderr.connect() manager.start() # watch for the end of the command: while True: operation = self.client.operations.get(operation_id) if 'return' in operation.metadata: break time.sleep(.5) # pragma: no cover while len(manager.websockets.values()) > 0: time.sleep(.1) # pragma: no cover manager.stop() manager.join() return _ContainerExecuteResult(operation.metadata['return'], stdout.data, stderr.data)
from ws4py.client import WebSocketBaseClient from ws4py.manager import WebSocketManager from ws4py import format_addresses, configure_logger import json import gpio from adc import analog_read logger = configure_logger() userid = "1407629972" m = WebSocketManager() class EchoClient(WebSocketBaseClient): def handshake_ok(self): logger.info("Opening %s" % format_addresses(self)) m.add(self) def received_message(self, msg): 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__':
""" Sets up websockets for Dashboard """ # pylint: disable-msg=C0103 import json import os from ws4py.client import WebSocketBaseClient from ws4py.manager import WebSocketManager from ws4py import format_addresses, configure_logger import queue import requests LOGGER = configure_logger() MANAGER = WebSocketManager() coin_queue = queue.Queue() #FIXME: Change these global to local variable BTC_USD = 0.0 XRB_BTC = 0.0 STORM_USD = 0.0 class SetQueue(): """ Custom Queue that uses a set instead of a regular queue. This keeps a set of coin names as well as a dict of it's coin:value :param queue.Queue: """ def __init__(self): self.queue = set() self.values = dict({})
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", ]
def init(self): """ Initializes the PB WS Client""" self.pb = Pushbullet(cfg.PUSHBULLET_KEY, cfg.PUSHBULLET_SECRET) self.manager = WebSocketManager() self.alarm = None
def __init__(self,start, poller=None): threading.Thread.__init__(self) WebSocketManager.__init__(self) self.timeout = start + (60*30)
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 __init__(self, bus): plugins.SimplePlugin.__init__(self, bus) self.manager = WebSocketManager()
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)
import os import sys import time import thread import logging import json import boto3 import botocore import pymongo from sys import stdout from pymongo import MongoClient from sqs_client import SQSClient from ws4py.client.threadedclient import WebSocketClient from ws4py.manager import WebSocketManager manager = WebSocketManager() timeout = 60 logging.basicConfig(filename='queue.log', filemode='w', level=logging.INFO) ############################### Get the service resources sqs = boto3.resource('sqs') s3 = boto3.resource('s3') bucketName = 'spreza-audio' queueIn = 'transcribe' queueOut = 'complete' #URI = '54.205.82.96' URI = '54.221.51.233' ASR_URI = 'ws://' + URI + '/client/ws/speech' STATUS_URI = 'ws://' + URI + '/client/ws/status' baseS3 = 'https://' + bucketName + '.s3.amazonaws.com/'
#coding:utf-8 ''' -- 简化版本,客户端发送模拟用户不用组给服务端,客户端记录发送成功与否并记录时间 -- 不需要服务端返回的数据! ''' from ws4py.client import WebSocketBaseClient from ws4py.manager import WebSocketManager from ws4py import format_addresses, configure_logger from PendingInstantOrder import CreateData from Balance_Delete_Order import main from datainfo import user_list, api_url import json import time logger = configure_logger() m = WebSocketManager() class EchoClient(WebSocketBaseClient): def handshake_ok(self): m.add(self) try: t = time.time() self.send(payload) self.send(Instant_Pending) t1 = time.time() self.close() except Exception as e: print("Failed!") with open(
def execute( self, commands, environment=None, encoding=None, decode=True, stdin_payload=None, stdin_encoding="utf-8", stdout_handler=None, stderr_handler=None, ): """Execute a command on the instance. stdout and stderr are buffered if no handler is given. :param commands: The command and arguments as a list of strings :type commands: [str] :param environment: The environment variables to pass with the command :type environment: {str: str} :param encoding: The encoding to use for stdout/stderr if the param decode is True. If encoding is None, then no override is performed and whatever the existing encoding from LXD is used. :type encoding: str :param decode: Whether to decode the stdout/stderr or just return the raw buffers. :type decode: bool :param stdin_payload: Payload to pass via stdin :type stdin_payload: Can be a file, string, bytearray, generator or ws4py Message object :param stdin_encoding: Encoding to pass text to stdin (default utf-8) :param stdout_handler: Callable than receive as first parameter each message received via stdout :type stdout_handler: Callable[[str], None] :param stderr_handler: Callable than receive as first parameter each message received via stderr :type stderr_handler: Callable[[str], None] :raises ValueError: if the ws4py library is not installed. :returns: A tuple of `(exit_code, stdout, stderr)` :rtype: _InstanceExecuteResult() namedtuple """ if not _ws4py_installed: raise ValueError( "This feature requires the optional ws4py library.") if isinstance(commands, str): raise TypeError("First argument must be a list.") if environment is None: environment = {} response = self.api["exec"].post( json={ "command": commands, "environment": environment, "wait-for-websocket": True, "interactive": False, }) fds = response.json()["metadata"]["metadata"]["fds"] operation_id = Operation.extract_operation_id( response.json()["operation"]) parsed = parse.urlparse( self.client.api.operations[operation_id].websocket._api_endpoint) with managers.web_socket_manager(WebSocketManager()) as manager: stdin = _StdinWebsocket( self.client.websocket_url, payload=stdin_payload, encoding=stdin_encoding, ) stdin.resource = "{}?secret={}".format(parsed.path, fds["0"]) stdin.connect() stdout = _CommandWebsocketClient( manager, self.client.websocket_url, encoding=encoding, decode=decode, handler=stdout_handler, ) stdout.resource = "{}?secret={}".format(parsed.path, fds["1"]) stdout.connect() stderr = _CommandWebsocketClient( manager, self.client.websocket_url, encoding=encoding, decode=decode, handler=stderr_handler, ) stderr.resource = "{}?secret={}".format(parsed.path, fds["2"]) stderr.connect() manager.start() # watch for the end of the command: while True: operation = self.client.operations.get(operation_id) if "return" in operation.metadata: break time.sleep(0.5) # pragma: no cover try: stdin.close() except BrokenPipeError: pass stdout.finish_soon() stderr.finish_soon() try: manager.close_all() except BrokenPipeError: pass while not stdout.finished or not stderr.finished: time.sleep(0.1) # progma: no cover manager.stop() manager.join() return _InstanceExecuteResult(operation.metadata["return"], stdout.data, stderr.data)
#!/usr/bin/python from ws4py.client import WebSocketBaseClient from ws4py.manager import WebSocketManager import time WS_TIMEOUT = 3 # Seconds before we give up connecting ws_manager = WebSocketManager() class DataReceiver(WebSocketBaseClient): def handshake_ok(self): print("Connected to '{}'!".format(self.url)) ws_manager.add(self) self.FILE_NAME = "data_{}.txt".format(self.bind_addr[0]) self.file_handle = open(self.FILE_NAME, 'w') def received_message(self, msg): s = str(msg.data) if s.startswith('['): s = s[1:-1] # Remove brackets if necessary self.file_handle.write(s + '\n') print("Received data! {}".format(msg if False else '')) def close(self, code=1000, reason=''): self.file_handle.close() print("Closed socket! Data was saved at '{}'".format(self.FILE_NAME)) def start_data_collection(conn_info): for (ip, port) in conn_info: