Ejemplo n.º 1
0
 def reconnect(self):
     # Instead of trying to reconnect in a retry loop with backoff, run an API call that will do the same
     # and block while it retries.
     time.sleep(1)
     dxpy.describe(self.job_id)
     WebSocketBaseClient.__init__(self, self.url, protocols=None, extensions=None)
     self.connect()
Ejemplo n.º 2
0
    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):
                    ioloop.IOLoop.instance().stop()

            ws = MyClient('ws://localhost:9000/echo', protocols=['http-only', 'chat'])
            ws.connect()

            ioloop.IOLoop.instance().start()
        """
        WebSocketBaseClient.__init__(self, url, protocols, extensions,
                                     ssl_options=ssl_options, headers=headers)
        if self.scheme == "wss":
            self.sock = ssl.wrap_socket(self.sock, do_handshake_on_connect=False, **self.ssl_options)
            self.io = iostream.SSLIOStream(self.sock, io_loop, ssl_options=self.ssl_options)
        else:
            self.io = iostream.IOStream(self.sock, io_loop)
        self.io_loop = io_loop
    def __init__(self, url, protocols=None, extensions=None, ssl_options=None):
        """
        WebSocket client that executes the
        :meth:`run() <ws4py.websocket.WebSocket.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:
                  print str(m)
               else:
                  break

          def outgoing():
            for i in range(0, 40, 5):
               ws.send("*" * i)

          greenlets = [
             gevent.spawn(incoming),
             gevent.spawn(outgoing),
          ]
          gevent.joinall(greenlets)
        """
        WebSocketBaseClient.__init__(self, url, protocols, extensions, ssl_options=ssl_options)
        self._th = Greenlet(self.run)

        self.messages = Queue()
        """
Ejemplo n.º 4
0
    def __init__(self, url, protocols=None, extensions=None, heartbeat_freq=None,
                 ssl_options=None, headers=None):
        """
        .. code-block:: python

           from ws4py.client.threadedclient import WebSocketClient

           class EchoClient(WebSocketClient):
               def opened(self):
                  for i in range(0, 200, 25):
                     self.send("*" * i)

               def closed(self, code, reason):
                  print(("Closed down", code, reason))

               def received_message(self, m):
                  print("=> %d %s" % (len(m), str(m)))

           try:
               ws = EchoClient('ws://localhost:9000/echo', protocols=['http-only', 'chat'])
               ws.connect()
           except KeyboardInterrupt:
              ws.close()

        """
        WebSocketBaseClient.__init__(self, url, protocols, extensions, heartbeat_freq,
                                     ssl_options, headers=headers)
        self._th = threading.Thread(target=self.run, name='WebSocketClient')
        self._th.daemon = True
Ejemplo n.º 5
0
	def __init__(self, currency, apiKey, apiSecret):
		currencies = [currency]
		url = 'ws://websocket.mtgox.com/mtgox?Currency=%s' % (",".join(currencies))
		WebSocketBaseClient.__init__(self, url)
		self.__nonce = None
		self.__apiKey = apiKey
		self.__apiSecret = apiSecret
Ejemplo n.º 6
0
 def __init__(self, url, protocols=None, extensions=None, io_loop=None):
     WebSocketBaseClient.__init__(self, url, protocols, extensions)
     if self.scheme == "wss":
         self.sock = ssl.wrap_socket(self.sock, do_handshake_on_connect=False)
         self.io = iostream.SSLIOStream(self.sock, io_loop)
     else:
         self.io = iostream.IOStream(self.sock, io_loop)
     self.sender = self.io.write
     self.io_loop = io_loop
Ejemplo n.º 7
0
 def __init__(self, url, protocols=None, extensions=None, io_loop=None,custom_headers=[]):
     WebSocketBaseClient.__init__(self, url, protocols, extensions, custom_headers)
     parts = urlsplit(self.url)
     if parts.scheme == "wss":
         self.sock = ssl.wrap_socket(self.sock,
                 do_handshake_on_connect=False)
         self.io = iostream.SSLIOStream(self.sock, io_loop)
     else:
         self.io = iostream.IOStream(self.sock, io_loop)
     self.sender = self.io.write
     self.io_loop = io_loop
Ejemplo n.º 8
0
    def __init__(self, url, sock=None, protocols=None, version='8'):
        WebSocketBaseClient.__init__(self, url, protocols=protocols, version=version)
        if not sock:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
        sock.settimeout(3)
        self.sock = sock

        self.running = True

        self._lock = threading.Lock()
        self._th = threading.Thread(target=self._receive)
Ejemplo n.º 9
0
    def __init__(self, url, protocols=None, extensions=None, heartbeat_freq=None,
                 ssl_options=None, headers=None):

        WebSocketBaseClient.__init__(self, url, protocols, extensions, heartbeat_freq,
                                     ssl_options, headers=headers)

        self.callbacks = {}
        self._connected = False

        # create an unique ID
        self._id = WebSocketClient._instance_count
        WebSocketClient._instance_count += 1
Ejemplo n.º 10
0
 def __init__(self, job_id, input_params={}, msg_output_format="{job} {level} {msg}", msg_callback=None,
              print_job_info=True):
     self.seen_jobs = {}
     self.input_params = input_params
     self.msg_output_format = msg_output_format
     self.msg_callback = msg_callback
     self.print_job_info = print_job_info
     self.closed_code, self.closed_reason = None, None
     ws_proto = 'wss' if dxpy.APISERVER_PROTOCOL == 'https' else 'ws'
     url = "{protocol}://{host}:{port}/{job_id}/streamLog/websocket".format(protocol=ws_proto,
                                                                     host=dxpy.APISERVER_HOST,
                                                                     port=dxpy.APISERVER_PORT,
                                                                     job_id=job_id)
     WebSocketBaseClient.__init__(self, url, protocols=None, extensions=None)
Ejemplo n.º 11
0
    def __init__(self, url, sock=None, protocols=None, version='8'):
        WebSocketBaseClient.__init__(self,
                                     url,
                                     protocols=protocols,
                                     version=version)
        if not sock:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
        sock.settimeout(3)
        self.sock = sock

        self.running = True

        self._lock = threading.Lock()
        self._th = threading.Thread(target=self._receive)
Ejemplo n.º 12
0
    def __init__(self, *args, **kwargs):
        """Constructor.

        Warning: there is a know issue regarding resolving localhost to
        IPv6 address.

        Args:
            ip (str, optional): Rosbridge instance IPv4/Host address.
            Defaults to 'localhost'.
            port (int, optional): Rosbridge instance listening port number.
            Defaults to 9090.
        """
        ExecutorBase.__init__(self, *args, **kwargs)
        WebSocketBaseClient.__init__(self, self._uri)
Ejemplo n.º 13
0
    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() <ws4py.websocket.WebSocket.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:
                  print str(m)
               else:
                  break

          def outgoing():
            for i in range(0, 40, 5):
               ws.send("*" * i)

          greenlets = [
             gevent.spawn(incoming),
             gevent.spawn(outgoing),
          ]
          gevent.joinall(greenlets)
        """
        WebSocketBaseClient.__init__(self,
                                     url,
                                     protocols,
                                     extensions,
                                     heartbeat_freq,
                                     ssl_options=ssl_options,
                                     headers=headers,
                                     exclude_headers=exclude_headers)
        self._th = Greenlet(self.run)

        self.messages = Queue()
        """
Ejemplo n.º 14
0
    def reconnect(self):
        """
        Reconnects to the server.
        """

        WebSocketBaseClient.__init__(self, self.url, self.protocols,
                                     self.extensions, self.heartbeat_freq,
                                     self.ssl_options, self.extra_headers)

        if self._local:
            # check the local host address is still valid
            with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
                sock.bind((self.host, 0))

        self.connect()
Ejemplo n.º 15
0
 def __init__(self,
              url,
              protocols=None,
              extensions=None,
              heartbeat_freq=None,
              ssl_options=None,
              headers=None):
     WebSocketBaseClient.__init__(self,
                                  url,
                                  protocols,
                                  extensions,
                                  heartbeat_freq,
                                  ssl_options,
                                  headers=headers)
     self._th = threading.Thread(target=self.run, name='WebSocketClient')
     self._th.daemon = True
Ejemplo n.º 16
0
    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):
                    ioloop.IOLoop.instance().stop()

            ws = MyClient('ws://localhost:9000/echo', protocols=['http-only', 'chat'])
            ws.connect()

            ioloop.IOLoop.instance().start()
        """
        WebSocketBaseClient.__init__(self,
                                     url,
                                     protocols,
                                     extensions,
                                     ssl_options=ssl_options,
                                     headers=headers,
                                     exclude_headers=exclude_headers)
        if self.scheme == "wss":
            self.sock = ssl.wrap_socket(self.sock,
                                        do_handshake_on_connect=False,
                                        **self.ssl_options)
            self._is_secure = True
            self.io = iostream.SSLIOStream(self.sock,
                                           io_loop,
                                           ssl_options=self.ssl_options)
        else:
            self.io = iostream.IOStream(self.sock, io_loop)
        self.io_loop = io_loop
Ejemplo n.º 17
0
    def run(self):
        # Run forever until event_stop tells us to stop
        while not self.data_collection.event_stop.is_set():
            # Initialize the websocket
            WebSocketBaseClient.__init__(self, self.url, *self.init_args, **self.init_kwargs)
            self.sock.settimeout(self.TIMEOUT)  # Set the socket timeout so if a host is unreachable it doesn't take 60s (default) to figure out
            logger.notice("Connecting to '{}'...".format(self.url))
            try:
                self.connect()  # Attempt to connect to the Arduino
            except Exception as e:
                logger.error("Unable to connect to '{}' (probably timed out). Reason: {}".format(self.url, e))
            else:  # If we were able to connect, then run the websocket (received_message will get called appropriately)
                while self.once():
                    pass  # self.once() will return False on error/close -> Only stop when the connection is lost or self.close() is called
            self.terminate()
            time.sleep(2)  # Wait for a couple of seconds for Arduino to reboot/connect or just to avoid network overload

        logger.success("Thread in charge of '{}' exited :)".format(self.url))
Ejemplo n.º 18
0
Archivo: ws.py Proyecto: dqbd/cheap-pos
 def __init__(self,
              url,
              protocols=[],
              extensions=None,
              heartbeat_freq=None,
              ssl_options=None,
              headers=None,
              inputQueue=None,
              outputQueue=None):
     WebSocketBaseClient.__init__(self,
                                  url,
                                  protocols,
                                  extensions,
                                  heartbeat_freq,
                                  ssl_options,
                                  headers=headers)
     self.isClosed = False
     self._input = inputQueue
     self._output = outputQueue
Ejemplo n.º 19
0
    def __init__(self,
                 url,
                 protocols=None,
                 extensions=None,
                 ssl_options=None,
                 headers=None):
        """WebSocket client that executes into a eventlet green thread."""
        WebSocketBaseClient.__init__(
            self,
            url,
            protocols,
            extensions,
            ssl_options=ssl_options,
            headers=headers,
        )
        self._th = threading.Thread(target=self.run, name="WebSocketClient")
        self._th.daemon = True

        self.messages = queue.Queue()
Ejemplo n.º 20
0
    def reconnect(self):
        """
        Reconnects to the server.
        """

        WebSocketBaseClient.__init__(self,
                                     self.url,
                                     self.protocols,
                                     self.extensions,
                                     self.heartbeat_freq,
                                     self.ssl_options,
                                     self.extra_headers)

        if self._local:
            # check the local host address is still valid
            with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
                    sock.bind((self.host, 0))

        self.connect()
Ejemplo n.º 21
0
    def __init__(self, url, protocols=None, extensions=None, heartbeat_freq=None,
                 ssl_options=None, headers=None):

        WebSocketBaseClient.__init__(self, url, protocols, extensions, heartbeat_freq,
                                     ssl_options, headers=headers)

        self.callbacks = {}
        self._connected = False
        self._local = False
        self._version = ""
        self._fd_notifier = None

        # create an unique ID
        self._id = WebSocketClient._instance_count
        WebSocketClient._instance_count += 1

        # set a default timeout of 10 seconds
        # for connecting to a server
        socket.setdefaulttimeout(10)
Ejemplo n.º 22
0
    def __init__(self,
                 url,
                 protocols=None,
                 extensions=None,
                 heartbeat_freq=None,
                 ssl_context=None,
                 headers=None,
                 exclude_headers=None):
        """
        .. code-block:: python

           from ws4py.client.threadedclient import WebSocketClient

           class EchoClient(WebSocketClient):
               def opened(self):
                  for i in range(0, 200, 25):
                     self.send("*" * i)

               def closed(self, code, reason):
                  print(("Closed down", code, reason))

               def received_message(self, m):
                  print("=> %d %s" % (len(m), str(m)))

           try:
               ws = EchoClient('ws://localhost:9000/echo', protocols=['http-only', 'chat'])
               ws.connect()
           except KeyboardInterrupt:
              ws.close()

        """
        WebSocketBaseClient.__init__(self,
                                     url,
                                     protocols,
                                     extensions,
                                     heartbeat_freq,
                                     ssl_context,
                                     headers=headers,
                                     exclude_headers=exclude_headers)
        self._th = threading.Thread(target=self.run, name='WebSocketClient')
        self._th.daemon = True
Ejemplo n.º 23
0
    def __init__(self, url, protocols=None, extensions=None, 
                 heartbeat_freq=None, ssl_options=None, headers=None, instance_id=None):

        WebSocketBaseClient.__init__(self, url,
                                     protocols,
                                     extensions,
                                     heartbeat_freq,
                                     ssl_options,
                                     headers)

        self.callbacks = {}
        self._connected = False
        self._local = False
        self._cloud = False
        self._version = ""
        self._fd_notifier = None
        self._heartbeat_timer = None
        self._tunnel = None
        self._instance_id = instance_id

        # create an unique ID
        self._id = WebSocketClient._instance_count
        WebSocketClient._instance_count += 1
Ejemplo n.º 24
0
 def __init__(self,
              job_id,
              input_params=None,
              msg_output_format="{job} {level} {msg}",
              msg_callback=None,
              print_job_info=True):
     self.job_id = job_id
     self.seen_jobs = {}
     self.input_params = input_params
     self.msg_output_format = msg_output_format
     self.msg_callback = msg_callback
     self.print_job_info = print_job_info
     self.closed_code, self.closed_reason = None, None
     ws_proto = 'wss' if dxpy.APISERVER_PROTOCOL == 'https' else 'ws'
     self.url = "{protocol}://{host}:{port}/{job_id}/getLog/websocket".format(
         protocol=ws_proto,
         host=dxpy.APISERVER_HOST,
         port=dxpy.APISERVER_PORT,
         job_id=job_id)
     WebSocketBaseClient.__init__(self,
                                  self.url,
                                  protocols=None,
                                  extensions=None)
Ejemplo n.º 25
0
    def __init__(self,
                 url,
                 protocols=None,
                 extensions=None,
                 heartbeat_freq=None,
                 ssl_options=None,
                 headers=None):

        WebSocketBaseClient.__init__(self, url, protocols, extensions,
                                     heartbeat_freq, ssl_options, headers)

        self.callbacks = {}
        self._connected = False
        self._local = False
        self._version = ""
        self._fd_notifier = None

        # create an unique ID
        self._id = WebSocketClient._instance_count
        WebSocketClient._instance_count += 1

        # set a default timeout of 10 seconds
        # for connecting to a server
        socket.setdefaulttimeout(10)
Ejemplo n.º 26
0
 def __init__(self, dispatch, url):
     WebSocketBaseClient.__init__(self,
                                  url,
                                  protocols=['http-only', 'chat'])
     self.dispatch = dispatch
     self.keep_alive = None
Ejemplo n.º 27
0
 def __init__(self, url, mgr, ca_certs = None, keyfile = None, certfile = None):
     self._mgr = mgr
     WebSocketBaseClient.__init__(self, url, ssl_options = {
         "ca_certs" : ca_certs,
         "keyfile"  : keyfile,
         "certfile" : certfile })
Ejemplo n.º 28
0
 def __init__(self, url, protocols=None, extensions=None, io_loop=None):
     WebSocketBaseClient.__init__(self, url, protocols, extensions)
     self.io = iostream.IOStream(self.sock, io_loop)
     self.sender = self.io.write
Ejemplo n.º 29
0
 def __init__(self, url, protocols=None, version='8', io_loop=None):
     WebSocketBaseClient.__init__(self, url, protocols=protocols, version=version)
     self.io = iostream.IOStream(socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0), io_loop)
Ejemplo n.º 30
0
    def __init__(self, url, protocols=None, extensions=None):
        WebSocketBaseClient.__init__(self, url, protocols, extensions)
        self._th = Greenlet(self.run)

        self.messages = Queue()
Ejemplo n.º 31
0
 def __init__(self, ws_uri, resource, *args, **kwargs):
     WebSocketBaseClient.__init__(self, ws_uri, *args, **kwargs)
     self.resource = resource
Ejemplo n.º 32
0
    def __init__(self, url, protocols=None, extensions=None):
        WebSocketBaseClient.__init__(self, url, protocols, extensions)
        self._th = Greenlet(self.run)

        self.messages = Queue()
Ejemplo n.º 33
0
 def __init__(self, url, protocols=None, extensions=None):
     WebSocketBaseClient.__init__(self, url, protocols, extensions)
     self._th = threading.Thread(target=self.run, name='WebSocketClient')
     self._th.daemon = True
Ejemplo n.º 34
0
	def __init__(self, url, protocols=None, extensions=None):
		WebSocketBaseClient.__init__(self, url, protocols, extensions)
		self._th = threading.Thread(target=self.run, name='WebSocketClient')
		self._th.daemon = True
Ejemplo n.º 35
0
 def __init__(self, url, protocols=None, version='8'):
     WebSocketBaseClient.__init__(self, url, protocols=protocols, version=version)
     self.io = iostream.IOStream(socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0))
Ejemplo n.º 36
0
 def __init__(self, dispatch, url):
     WebSocketBaseClient.__init__(self, url,
                                  protocols=['http-only', 'chat'])
     self.dispatch = dispatch
     self.keep_alive = None