示例#1
0
    def connect(self, serv_addr):
        """Connect to server""" 
        sock = connect(serv_addr)

        # HTTP Handshake
        request_data = messages.make_http_request('GET', '/createsession')
        data = ''
        success = True
        try:
            sock.send(request_data)
            payload = self._wait_response(sock, wait=2.0)
            if not payload:
                success = False
            else:
                response_headers, body = payload
                if response_headers['code'] != 200:
                    success = False
                else:
                    msg = json.loads(body)
                    self._worker_addr= (serv_addr[0], msg['port'])
                    self._sid = msg['sid'] 
                    self._longpoll()
            return success
        except socket.error as e:            
            return False
        finally:
            sock.close()
示例#2
0
    def connect(self, serv_addr):
        """
        Override connect to implement websocket handshake.
        """
        Connection.connect(self, serv_addr)

        # WebSocket Handshake
        request_headers = messages.make_ws_request_headers(serv_addr)
        self._ws_key = request_headers["sec-websocket-key"]
        request_data = messages.make_http_request('GET', '/',
                                                  headers=request_headers)

        self._sock.send(request_data)
        data = self._sock.recv(1024)            
        msg, data = messages.http_parse(data)
        if msg == None:
            self._sock.close()
            self._sock = None
            self._is_connected = False
            return

        response_headers, body = msg
        # Verify handshake response
        verified = messages.verify_ws_response_headers(self._ws_key, 
                                                       response_headers) 
        if verified:
            self._is_connected = True
        else:
            self._is_connected = False
            self._sock.close()
            self._sock = None

        return self._is_connected
示例#3
0
 def _do_request(self, op, msg=None):
     if op not in ['op', 'lp']:
         return False
     if not msg: 
         msg = {}
     msg['op'] = op
     msg['sid'] = self._sid
     path = '/?data=' + urllib.quote(json.dumps(msg))
     http_request = messages.make_http_request('GET', path)
     try:
         sock = connect(self._worker_addr)
         sock.send(http_request)
         if op == 'op':
             f = lambda : self._wait_op(sock)
         else:
             self._lp_sock = sock
             f = lambda : self._wait_lp(sock)
         t = threading.Thread(target=f)
         t.setDaemon(True)
         t.start()
     except:
         traceback.exc()
         return False
     return True