示例#1
0
文件: houndify.py 项目: tuxkp/ay250
    def start(self, listener):
        """
      This method is used to make the actual connection to the server and prepare
      for audio streaming.

      listener is a HoundListener (or derived class) object
      """
        self.audioFinished = False
        self.buffer = bytes()

        self.conn = http.client.HTTPSConnection(self.hostname)
        self.conn.putrequest('POST', VOICE_ENDPOINT)

        headers = self._generateHeaders(self.HoundRequestInfo)
        headers['Transfer-Encoding'] = 'chunked'

        for header in headers:
            self.conn.putheader(header, headers[header])
        self.conn.endheaders()

        audio_header = self._wavHeader(self.sampleRate)
        if self.useSpeex:
            audio_header = pySHSpeex.Init(self.sampleRate == 8000)
        self._send(audio_header)

        self.callbackTID = threading.Thread(target=self._callback,
                                            args=(listener, ))
        self.callbackTID.start()
示例#2
0
    def start(self, listener=HoundListener()):
      """
      This method is used to make the actual connection to the server and prepare
      for audio streaming.

      listener is a HoundListener (or derived class) object
      """
      self.audioFinished = False
      self.lastResult = None
      self.buffer = bytes()

      if self.proxyHost:
        self.conn = http.client.HTTPSConnection(self.proxyHost, self.proxyPort)
        self.conn.set_tunnel(self.hostname, headers = self.proxyHeaders) 
      else:
        self.conn = http.client.HTTPSConnection(self.hostname)

      self.conn.putrequest("POST", VOICE_ENDPOINT)

      headers = self._generateHeaders(self.HoundRequestInfo)
      headers["Transfer-Encoding"] = "chunked";
      if self.gzip:
        headers["Hound-Response-Accept-Encoding"] = "gzip";

      for header in headers:
        self.conn.putheader(header, headers[header])
      self.conn.endheaders()
      
      self.callbackTID = threading.Thread(target = self._callback, args = (listener,))
      self.callbackTID.start()

      audio_header = self._wavHeader(self.sampleRate)
      if self.useSpeex:
        audio_header = pySHSpeex.Init(self.sampleRate == 8000)
      self._send(audio_header)
示例#3
0
    def start(self, listener=HoundListener()):
        """
      This method is used to make the actual connection to the server and prepare
      for audio streaming.

      listener is a HoundListener (or derived class) object
      """
        self.audioFinished = False
        self.lastResult = None
        self.lastException = None
        self.killCallbackThread = False
        self.buffer = bytes()

        try:
            if self.proxyHost:
                self.conn = http.client.HTTPSConnection(self.proxyHost,
                                                        self.proxyPort,
                                                        timeout=self.timeout)
                self.conn.set_tunnel(self.hostname, headers=self.proxyHeaders)
            elif self.is_https:
                self.conn = http.client.HTTPSConnection(self.hostname,
                                                        timeout=self.timeout)
            else:
                self.conn = http.client.HTTPConnection(self.hostname,
                                                       timeout=self.timeout)

            self.conn.putrequest("POST", self.path)

            headers, requestInfoString = self._generateHeaders(
                self.HoundRequestInfo)
            headers["Transfer-Encoding"] = "chunked"
            if self.gzip:
                headers["Hound-Response-Accept-Encoding"] = "gzip"

            for header in headers:
                self.conn.putheader(header, headers[header])
            self.conn.endheaders()

            self.callbackTID = threading.Thread(target=self._callback,
                                                args=(listener, ))
            self.callbackTID.start()

            self._send(requestInfoString)

            audio_header = self._wavHeader(self.sampleRate)
            if self.useSpeex:
                audio_header = pySHSpeex.Init(self.sampleRate == 8000)
            self._send(audio_header)

        except socket.timeout as e:
            raise HoundifyTimeoutError(e) from e

        except Exception as e:
            raise HoundifySDKError() from e
示例#4
0
    def start(self, listener):
        """
      This method is used to make the actual connection to the server and prepare
      for audio streaming.

      listener is a HoundListener (or derived class) object
      """
        self.audioFinished = False
        self.buffer = ''
        self.HoundRequestInfo['RequestID'] = str(uuid.uuid4())
        self.HoundRequestInfo['TimeStamp'] = int(time.time())
        self.conn = HTPConnection(self.hostname, self.port)
        htpMsg = self.conn.ReadMessage()
        challengeMsg = json.loads(htpMsg.data)
        if not challengeMsg.has_key(
                'status') or challengeMsg['status'] != 'ok':
            raise Exception("Error reading challenge message")

        nonce = challengeMsg['nonce']
        signature = self._authenticate(nonce)

        ## Startup the listening thread (above)
        self.callbackTID = threading.Thread(target=self._callback,
                                            args=(listener, ))
        self.callbackTID.start()

        self.conn.SendMessage(
            HTPMessage(
                HTPMessage.HTP_TYPE_JSON,
                json.dumps({
                    'access_id': self.clientID,
                    'signature': signature,
                    'version': '1.1'
                })))
        HoundRequestInfo = json.dumps(self.HoundRequestInfo)
        gzip_compressor = zlib.compressobj(9, zlib.DEFLATED,
                                           zlib.MAX_WBITS | 16)
        msg = gzip_compressor.compress(
            HoundRequestInfo) + gzip_compressor.flush()
        self.conn.SendMessage(HTPMessage(HTPMessage.HTP_TYPE_BINARY, msg))

        header = self._wavHeader(self.sampleRate)
        if self.useSpeex:
            header = pySHSpeex.Init(self.sampleRate == 8000)

        self.conn.SendMessage(HTPMessage(HTPMessage.HTP_TYPE_BINARY, header))