Example #1
0
    def __connect(self, wsURL):
        """Connect to the websocket in a thread."""
        logger.debug('Starting thread')

        self.ws = websocket.WebSocketApp(wsURL,
                                         on_message=self.__on_message,
                                         on_close=self.__on_close,
                                         on_open=self.__on_open,
                                         on_error=self.__on_error
                                         )

        self.wst = threading.Thread(target=lambda: self.ws.run_forever())
        self.wst.daemon = True
        self.wst.start()
        logger.debug('Started thread')

        # Wait for connect before continuing
        conn_timeout = 5
        while not self.ws.sock or not self.ws.sock.connected and conn_timeout:
            sleep(1)
            conn_timeout -= 1
        if not conn_timeout:
            logger.error('Couldnt connect to WS! Exiting.')
            self.exit()
            raise websocket.WebSocketTimeoutException('Couldnt connect to WS! Exiting.')
    def __connect(self, wsURL, symbol):
        '''Connect to the websocket in a thread.'''
        self.logger.debug("Starting thread")

        self.ws = websocket.WebSocketApp(wsURL,
                                         on_message=self.__on_message,
                                         on_close=self.__on_close,
                                         on_open=self.__on_open,
                                         on_error=self.__on_error,
                                         header=self.__get_auth())

        self.wst = threading.Thread(target=lambda: self.ws.run_forever())
        self.wst.daemon = True
        self.wst.start()
        self.logger.debug("Started thread")

        # Wait for connect before continuing
        conn_timeout = 5
        while not self.ws.sock or not self.ws.sock.connected and conn_timeout:
            time.sleep(1)
            conn_timeout -= 1
        if not conn_timeout:
            self.logger.error("Couldn't connect to WS! Exiting.")
            self.exit()
            raise websocket.WebSocketTimeoutException(
                'Couldn\'t connect to WS! Exiting.')
Example #3
0
    def __connect(self, endpoint):
        '''Connect to the websocket in a thread.'''
        self.logger.debug("Starting thread")

        options = {"ping_interval": 45, "ping_timeout": self.socket_timeout}

        if self.insecure_ssl:
            options["sslopt"] = {"cert_reqs": ssl.CERT_NONE}

        self.ws = websocket.WebSocketApp(endpoint,
                                         on_message=self.__on_message,
                                         on_close=self.__on_close,
                                         on_open=self.__on_open,
                                         on_error=self.__on_error)

        self.wst = threading.Thread(
            target=lambda: self.ws.run_forever(**options))
        self.wst.daemon = True
        self.wst.start()
        self.logger.debug("Started thread")

        # Wait for connect before continuing
        conn_timeout = 5
        while (not self.ws.sock
               or not self.ws.sock.connected) and conn_timeout:
            sleep(1)
            conn_timeout -= 1
        if not conn_timeout:
            self.logger.error("Couldn't connect to WS! Exiting.")
            self.exit()
            raise websocket.WebSocketTimeoutException(
                'Couldn\'t connect to WS! Exiting.')
Example #4
0
    def __connect(self, wsURL):
        '''Connect to the websocket in a thread.'''
        self.logger.debug("Starting thread")

        self.ws = websocket.WebSocketApp(wsURL,
                                         on_message=self.__on_message,
                                         on_close=self.__on_close,
                                         on_open=self.__on_open,
                                         on_error=self.__on_error,
                                         header=self.__get_auth(),
                                         on_ping=self.__on_ping,
                                         on_pong=self.__on_pong)
        ws_run_args = {'ping_timeout': 20, 'ping_interval': 60}

        self.wst = threading.Thread(target=self.ws.run_forever,
                                    kwargs=ws_run_args)
        self.wst.daemon = True
        self.wst.start()
        self.logger.debug("Started thread")

        # Wait for connect before continuing
        conn_timeout = 5
        while not self.ws.sock or not self.ws.sock.connected and conn_timeout:
            sleep(1)
            conn_timeout -= 1
        if not conn_timeout:
            self.logger.error("Couldn't connect to WS! Exiting.")
            self.exit()
            raise websocket.WebSocketTimeoutException(
                'Couldn\'t connect to WS! Exiting.')
Example #5
0
    def connect(self, ws_url):
        """Connect to the websocket in a thread."""
        self.logger.debug("Starting websocket thread.")

        self.ws = websocket.WebSocketApp(
            ws_url,
            on_message=self.on_message,
            on_close=self.on_close,
            on_open=self.on_open,
            on_error=self.on_error,
        )

        self.wst = threading.Thread(target=lambda: self.ws.run_forever())
        self.wst.daemon = True
        self.wst.start()
        self.logger.debug("Started websocket thread.")

        # Wait for connect before continuing
        conn_timeout = 5
        while (not self.ws.sock
               or not self.ws.sock.connected) and conn_timeout:
            time.sleep(1)
            conn_timeout -= 1
        if not conn_timeout:
            self.logger.error("Couldn't connect to websocket! Exiting.")
            self.exit()
            raise websocket.WebSocketTimeoutException(
                "Couldn't connect to websocket! Exiting.")

        self.exited = False
Example #6
0
    def __connect(self, wsURL, symbol):
        '''Connect to the websocket in a thread.'''
        '''*********NEED TO CHANGE THIS PART WHENT PUT ON CLOUD!!!*********'''
        self.logger.debug("Starting thread")

        self.ws = websocket.WebSocketApp(wsURL,
                                         on_message=self.__on_message,
                                         on_close=self.__on_close,
                                         on_open=self.__on_open,
                                         on_error=self.__on_error,
                                         header=self.__get_auth())

        # self.wst = threading.Thread(target=lambda: self.ws.run_forever())
        self.wst = threading.Thread(target=lambda :self.ws.run_forever(http_proxy_host="127.0.0.1", http_proxy_port=7890))
        self.wst.daemon = True
        self.wst.start()
        self.logger.debug("Started thread")

        # Wait for connect before continuing
        conn_timeout = 5
        while not self.ws.sock or not self.ws.sock.connected and conn_timeout:
            sleep(1)
            conn_timeout -= 1
        if not conn_timeout:
            self.logger.error("Couldn't connect to WS! Exiting.")
            self.exit()
            raise websocket.WebSocketTimeoutException("Couldn't connect to WS! Exiting.")
Example #7
0
    def start(self):
        '''
        This is used to start the socket.
        '''
        if not(self.socketRunning):
            logging.info('SOCKET: Setting up socket connection.')
            self.create_socket()
            logging.info('SOCKET: Setup socket.')

            ## -------------------------------------------------------------- ##
            # This block is used to test connectivity to the socket.
            conn_timeout = 5
            while not self.ws.sock or not self.ws.sock.connected and conn_timeout:
                time.sleep(1)
                conn_timeout -= 1

                if not conn_timeout:
                    # If the timeout limit is reached then the websocket is force closed.
                    self.ws.close()
                    raise websocket.WebSocketTimeoutException('Couldn\'t connect to WS! Exiting.')
            ## -------------------------------------------------------------- ##

            self.socketRunning = True
            logging.info('SOCKET: Sucessfully created socket connection.')

        logging.info('SOCKET: Sucessfully started socket.')
Example #8
0
    def start(self):
        '''
        This is used to start the socket.
        '''
        if self.socketRunning:
            return('SOCKET_STILL_RUNNING_PLEASE_RESTART')

        ## -------------------------------------------------------------- ##
        ## Here the sockets URL is set so it can be connected to.NO_STREAMS_SET
        logging.debug('[SOCKET_MASTER] Setting up socket stream URL.')
        if self.query == '':
            if self.build_query() == 'NO_STREAMS_SET':
                return('UNABLE_TO_START_NO_STREAMS_SET')
        self.destURL = self.query

        ## -------------------------------------------------------------- ##
        ## Here the 'create_socket' function is called to attempt a connection to the socket.
        logging.debug('[SOCKET_MASTER] Setting up socket connection.')
        self._create_socket()

        ## -------------------------------------------------------------- ##
        # This block is used to test connectivity to the socket.
        conn_timeout = 5
        while not self.ws.sock or not self.ws.sock.connected and conn_timeout:
            time.sleep(5)
            conn_timeout -= 1

            if not conn_timeout:
                ## If the timeout limit is reached then the websocket is force closed.
                self.ws.close()
                raise websocket.WebSocketTimeoutException('Couldn\'t connect to WS! Exiting.')

        self.socketRunning = True
        logging.info('[SOCKET_MASTER] Sucessfully established the socket.')
Example #9
0
    def __connect(self, wsURL):
        '''Connect to the websocket in a thread.'''
        self.logger.debug("Starting thread")

        self.ws = websocket.WebSocketApp(wsURL,
                                         on_message=self.__on_message,
                                         on_close=self.__on_close,
                                         on_open=self.__on_open,
                                         on_error=self.__on_error,
                                         keep_running=True)

        self.wst = threading.Thread(target=lambda: self.ws.run_forever())
        self.wst.daemon = True
        self.wst.start()
        self.logger.debug("Started thread")

        # Wait for connect before continuing
        retry_times = 5
        while not self.ws.sock or not self.ws.sock.connected and retry_times:
            sleep(1)
            retry_times -= 1
        if retry_times == 0 and not self.ws.sock.connected:
            self.logger.error("Couldn't connect to WebSocket! Exiting.")
            self.exit()
            raise websocket.WebSocketTimeoutException(
                'Error!Couldn not connect to WebSocket!.')

        if self.api_key and self.api_secret:
            self.__do_auth()
Example #10
0
    def __connect(self):
        """Connect to the websocket in a thread."""
        self.lastUsedUrlIdx +=1
        if self.lastUsedUrlIdx >= len(self.wsURLs):
            self.lastUsedUrlIdx= 0
        usedUrl= self.wsURLs[self.lastUsedUrlIdx]
        self.logger.info("Connecting to %s" % usedUrl)
        self.logger.debug("Starting thread")
        self.exited = False
        self.auth = False

        self.ws = websocket.WebSocketApp(usedUrl,
                                         on_message=self.on_message,
                                         on_close=self.__on_close,
                                         on_open=self.__on_open,
                                         on_error=self.on_error,
                                         keep_running=True)

        self.wst = threading.Thread(target=lambda: self.ws.run_forever(ping_interval=5))
        self.wst.daemon = True
        self.wst.start()
        self.logger.debug("Started thread")

        # Wait for connect before continuing
        retry_times = 5
        while (self.ws.sock is None or not self.ws.sock.connected) and retry_times > 0:
            sleep(1)
            retry_times -= 1
        if retry_times == 0 and (self.ws.sock is None or not self.ws.sock.connected):
            self.logger.error("Couldn't connect to WebSocket! Exiting.")
            self.exit()
            raise websocket.WebSocketTimeoutException('Error!Couldnt not connect to WebSocket!.')

        if self.api_key and self.api_secret:
            self.do_auth()
Example #11
0
    def _connect(self):
        """
        Connect to the L2 Orderbook of XBTUSD from Bitmex. 
        Callbacks are defined and the websocket is opened.
        """
        self.logger.debug("Starting thread")
        self.ws = websocket.WebSocketApp('wss://www.bitmex.com/realtime',
                                         on_message=self._on_message,
                                         on_close=self._on_close,
                                         on_open=self._on_open,
                                         on_error=self._on_error)

        self.wst = threading.Thread(target=lambda: self.ws.run_forever())
        self.wst.setDaemon(True)
        self.wst.start()
        self.logger.debug("Started thread")

        conn_timeout = 5
        while not self.ws.sock or not self.ws.sock.connected and conn_timeout:
            sleep(1)
            conn_timeout -= 1
        if not conn_timeout:
            self.logger.error("Couldn't connect to WS! Exiting.")
            self.exit()
            raise websocket.WebSocketTimeoutException(
                'Couldn\'t connect to WS! Exiting.')
Example #12
0
    def close(self):
        """
        Closes the WebSocket connection
        """
        self.logger.info("Closing WebSocket Connection")

        self._wss.close()
        self._isOpen = False
        if not self._close_event.wait(10) and self._wss.sock.connected:
            raise websocket.WebSocketTimeoutException(
                "Failed to received Websocket Close event")
Example #13
0
 def validate_socket(self, ws):
     # Wait for connect before continuing
     conn_timeout = 5
     while not ws.sock or not ws.sock.connected and conn_timeout:
         sleep(1)
         conn_timeout -= 1
     if not conn_timeout:
         logger.error("Couldn't connect to WS! Exiting.")
         self.exit(ws)
         raise websocket.WebSocketTimeoutException(
             'Couldn\'t connect to WS! Exiting.')
Example #14
0
 def _wait_for_ws_connect(self):
     conn_timeout = 5
     while (
         (not self.ws.sock or not self.ws.sock.connected)
         and conn_timeout
         and not self._error
     ):
         time.sleep(1)
         conn_timeout -= 1
     if not conn_timeout:  # i.e. if conn_timeout = 0
         logging.error("Couldn't connect to websocket! Exiting.")
         raise webs.WebSocketTimeoutException(
             "Couldn't connect to websocket! Exiting."
         )
Example #15
0
    def start(self, markets, query):
        '''
        This is used to start the socket.
        '''

        if self.ws != None and self.socketRunning:
            self.stop()

        ## -------------------------------------------------------------- ##
        ## Here the socket buffer is setup to store the data for each symbol.
        logging.debug('SOCKET: Setting up buffer.')
        socketBuffer = {}
        for market in markets:
            socketBuffer.update(
                {market: {
                    'depth': {
                        'bids': [],
                        'asks': []
                    },
                    'candle': {}
                }})
        self.socketBuffer = socketBuffer

        ## -------------------------------------------------------------- ##
        ## Here the sockets URL is set so it can be connected to.
        logging.debug('SOCKET: Setting up socket stream URL.')
        if query[-1] == '/':
            query = query[:-1]
        self.destURL = '{0}/stream?streams={1}'.format(SOCKET_BASE, query)

        ## -------------------------------------------------------------- ##
        ## Here the 'create_socket' function is called to attempt a connection to the socket.
        logging.debug('SOCKET: Setting up socket connection.')
        self.create_socket()

        ## -------------------------------------------------------------- ##
        # This block is used to test connectivity to the socket.
        conn_timeout = 5
        while not self.ws.sock or not self.ws.sock.connected and conn_timeout:
            time.sleep(5)
            conn_timeout -= 1

            if not conn_timeout:
                ## If the timeout limit is reached then the websocket is force closed.
                self.ws.close()
                raise websocket.WebSocketTimeoutException(
                    'Couldn\'t connect to WS! Exiting.')

        self.socketRunning = True
        logging.info('SOCKET: Sucessfully established the socket.')
Example #16
0
    def _wait_for_authentication(self):
        """Waits until we have received message confirming authentication"""
        timeout = 50
        while not self.authenticated and timeout:
            time.sleep(0.1)
            timeout -= 1

        if not timeout:
            logging.error("Couldn't authenticate connection! Exiting.")
            raise webs.WebSocketTimeoutException(
                "Couldn't authenticate connection! Exiting."
            )

        logging.info("Successfully authenticated")
Example #17
0
 def _thread_connect(self):
     self.close(reset_symbol=False)
     print('websocket connecting to %s...' % self.url)
     self.ws = websocket.WebSocketApp(self.url, on_message=self.on_message, on_error=self.on_error)
     self.thread_io = Thread(target=self.ws.run_forever)
     self.thread_io.daemon = True
     self.thread_io.start()
     for _ in range(100):
         if self.ws.sock and self.ws.sock.connected:
             break
         sleep(0.1)
     else:
         self.close()
         raise websocket.WebSocketTimeoutException('websocket connection failed')
     self.subscribe(self.symbol, self.interval)
     print('websocket connected')
Example #18
0
    def _connect_websocket(self):
        self.ws = WebSocketApp(url=self.ws_url,
                               on_open=self.__on_open,
                               on_message=self.__on_message,
                               on_close=self.__on_close)

        self.wst = threading.Thread(target=lambda: self.ws.run_forever())
        self.wst.daemon = True
        self.wst.start()
        self.wst.join()

        conn_timeout = 10
        while not self.ws.sock or not self.ws.sock.connected and conn_timeout:
            time.sleep(1)
            conn_timeout -= 1
        if not conn_timeout:
            raise websocket.WebSocketTimeoutException(
                "Couldn't connect to WS! Exiting. Please check your host settings. ")
Example #19
0
    def __connect(self, wsURL, symbol):
        self.ws = websocket.WebSocketApp(wsURL,
                                         on_message=self.__on_message,
                                         on_close=self.__on_close,
                                         on_open=self.__on_open,
                                         on_error=self.__on_error,
                                         )
        self.wst = threading.Thread(target=lambda: self.ws.run_forever(ping_interval=10))
        # 线程保护
        self.wst.daemon = True
        self.wst.start()
        print("Started thread")

        conn_timeout = 5
        while not self.ws.sock or not self.ws.sock.connected and conn_timeout:
            sleep(1)
            conn_timeout -= 1
        if not conn_timeout:
            print("无法连接到bitmex.")
            self.exit()
            raise websocket.WebSocketTimeoutException('Couldn\'t connect to WS! Exiting.')
    def _connect(self, wsURL):
        self.ws = websocket.WebSocketApp(wsURL,
                                         on_message=self._on_message,
                                         on_close=self._on_close,
                                         on_open=self._on_open,
                                         on_error=self._on_error,
                                         header=self._get_auth())

        self.wst = threading.Thread(target=lambda: self.ws.run_forever())
        self.wst.daemon = True
        self.wst.start()

        conn_timeout = 10
        while not self.ws.sock or not self.ws.sock.connected and conn_timeout:
            sleep(1)
            conn_timeout -= 1
        if not conn_timeout:
            self.exit()
            raise websocket.WebSocketTimeoutException('Couldn\'t connect to WS! Exiting.')
        
        return True
Example #21
0
            local_date = datetime.utcnow()

            delta = local_date - remote_date

            delta_seconds = delta.total_seconds()

            if delta_seconds < self.min_diff:
                self.min_diff = delta_seconds

            # print(data[-1]["timestamp"])
            # self.write(data)


tt = TimeTester()

url = "wss://www.bitmex.com/realtime?subscribe=trade:XBTUSD"
ws = websocket.WebSocketApp(url, on_message=tt.receive)
wst = threading.Thread(target=lambda: ws.run_forever())
wst.daemon = True
wst.start()

sleep(3)
if not ws.sock or not ws.sock.connected:
    raise websocket.WebSocketTimeoutException()

for i in range(1, 90):
    print tt.min_diff
    sleep(10)

print "done"
    def run_forever(self,
                    sockopt=None,
                    sslopt=None,
                    ping_interval=0,
                    ping_timeout=None,
                    http_proxy_host=None,
                    http_proxy_port=None,
                    http_no_proxy=None,
                    http_proxy_auth=None,
                    skip_utf8_validation=False,
                    host=None,
                    origin=None):
        """
        Patched version which handles more exceptions compared to original
        """

        if not ping_timeout or ping_timeout <= 0:
            ping_timeout = None
        if sockopt is None:
            sockopt = []
        if sslopt is None:
            sslopt = {}
        if self.sock:
            raise websocket.WebSocketException("socket is already opened")
        thread = None
        close_frame = None

        try:
            self.sock = websocket.WebSocket(
                self.get_mask_key,
                sockopt=sockopt,
                sslopt=sslopt,
                fire_cont_frame=self.on_cont_message and True or False,
                skip_utf8_validation=skip_utf8_validation)
            self.sock.settimeout(websocket.getdefaulttimeout())
            self.sock.connect(self.url,
                              header=self.header,
                              cookie=self.cookie,
                              http_proxy_host=http_proxy_host,
                              http_proxy_port=http_proxy_port,
                              http_no_proxy=http_no_proxy,
                              http_proxy_auth=http_proxy_auth,
                              subprotocols=self.subprotocols,
                              host=host,
                              origin=origin)
            self._callback(self.on_open)

            if ping_interval:
                event = threading.Event()
                thread = threading.Thread(target=self._send_ping,
                                          args=(ping_interval, event))
                thread.setDaemon(True)
                thread.start()

            while self.sock.connected:
                r, w, e = select.select((self.sock.sock, ), (), (),
                                        ping_timeout)
                if not self.keep_running:
                    break
                if ping_timeout and self.last_ping_tm and time.time(
                ) - self.last_ping_tm > ping_timeout:
                    self.last_ping_tm = 0
                    raise websocket.WebSocketTimeoutException("ping timed out")

                if r:
                    op_code, frame = self.sock.recv_data_frame(True)
                    if op_code == websocket.ABNF.OPCODE_CLOSE:
                        close_frame = frame
                        break
                    elif op_code == websocket.ABNF.OPCODE_PING:
                        self._callback(self.on_ping, frame.data)
                    elif op_code == websocket.ABNF.OPCODE_PONG:
                        self._callback(self.on_pong, frame.data)
                    elif op_code == websocket.ABNF.OPCODE_CONT and self.on_cont_message:
                        self._callback(self.on_data, data, frame.opcode,
                                       frame.fin)
                        self._callback(self.on_cont_message, frame.data,
                                       frame.fin)
                    else:
                        data = frame.data
                        if six.PY3 and frame.opcode == websocket.ABNF.OPCODE_TEXT:
                            data = data.decode("utf-8")
                        self._callback(self.on_data, data, frame.opcode, True)
                        self._callback(self.on_message, data)
        except (Exception, KeyboardInterrupt, SystemExit) as e:
            self._callback(self.on_error, e)
            if isinstance(e, SystemExit):
                # propagate SystemExit further
                raise
        finally:
            if thread:
                event.set()
                thread.join()
                self.keep_running = False
            self.sock.close()
            self._callback(
                self.on_close,
                *self._get_close_args(
                    close_frame.data if close_frame else None))
            self.sock = None
Example #23
0
    def open(self, open_socket_time_out=30):
        """
        Opens a WebSocket connection
        Note: There is a sub-protocol, that needs to be passed known as 'Gabbo' protocol
        :param open_socket_time_out: How long to wait, in s, for opening the connection to take
        :return: None
        """
        self._open_event.clear()
        self._close_event.clear()
        self.logger.debug(
            "Opening Web Socket connection to the device with IP: %s",
            self._ip_address)
        self._jsonIn[:] = []

        _clientName = datetime.datetime.now().isoformat()
        if self._ip_address not in ['127.0.0.1', 'localhost']:
            tflag = self.check_before_open()
            self.logger.info("The value of tflag: %s", tflag)
            if not tflag:
                raise websocket.WebSocketTimeoutException(
                    "FrontDoor not Listening on port 8082")
            if int(self._port) == 8082:
                self.fd_url = "wss://" + self._ip_address + ":" + str(
                    self._port) + "/?product=EddieTest" + _clientName
                self.logger.info("The websocket url is: {}".format(
                    self.fd_url))
                self._wss = websocket.WebSocketApp(self.fd_url,
                                                   subprotocols=['eco2'],
                                                   on_message=self._onMessage,
                                                   on_error=self._onError,
                                                   on_close=self._onClose)

            if int(self._port) == 8086:
                self.fd_url = "ws://" + self._ip_address + ":" + str(
                    self._port) + "/?product=EddieTest" + _clientName
                self.logger.info("The websocket url is: {}".format(
                    self.fd_url))
                self._wss = websocket.WebSocketApp(self.fd_url,
                                                   subprotocols=['eco2'],
                                                   on_message=self._onMessage,
                                                   on_error=self._onError,
                                                   on_close=self._onClose)
        else:
            self._wss = websocket.WebSocketApp("ws://127.0.0.1:" +
                                               str(self._port),
                                               on_message=self._onMessage,
                                               on_error=self._onError,
                                               on_close=self._onClose)
        self.json_in_event = threading.Event()
        self.json_in_event.clear()
        self._wss.on_open = self._onOpen
        self._run()

        open_socket_start_time = time.time()
        self.logger.debug(
            "Waiting for WebSocket connection to be established.")
        while not self._wss.sock:
            if (time.time() - open_socket_start_time) > open_socket_time_out:
                raise websocket.WebSocketTimeoutException(
                    'Failed to acquire a socket after {}s'.format(
                        open_socket_time_out))

        self.logger.debug("Connecting to WebSocket Object: %s", self._wss.sock)

        connected_start_time = time.time()
        while not self._wss.sock.connected:
            if (time.time() - connected_start_time) > self._web_socket_timeout:
                # It's taking too long, better close the connection
                self.logger.error("WebSocket Timeout - No Route to host")
                self._wss.close()
                break

        if not self._open_event.wait(10):
            raise websocket.WebSocketTimeoutException(
                "Failed to received Websocket Open event")

        total_open_time = time.time() - open_socket_start_time

        self.logger.debug(
            "Total time for connection to WebSocket: {:.3f}".format(
                total_open_time))
        self.logger.info("Is the WebSocket connection established? %s",
                         self._wss.sock.connected)