本文整理匯總了Python中transport.Transport.recvProtobufIfAny方法的典型用法代碼示例。如果您正苦於以下問題:Python Transport.recvProtobufIfAny方法的具體用法?Python Transport.recvProtobufIfAny怎麽用?Python Transport.recvProtobufIfAny使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類transport.Transport
的用法示例。
在下文中一共展示了Transport.recvProtobufIfAny方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: ServerConnection
# 需要導入模塊: from transport import Transport [as 別名]
# 或者: from transport.Transport import recvProtobufIfAny [as 別名]
#.........這裏部分代碼省略.........
response = self.send_init_request()
if response.responseCode != 200:
error_text = 'Wrong response from server, status_code={0}'.format(
response.responseCode)
if response.HasField("message"):
error_text += ', message is "{0}"'.format(response.message)
raise ServerError(error_text)
self.session_id = response.sessionId
self.log("session_id={0}".format(self.session_id))
return self.session_id
def send_init_request(self):
request = ConnectionRequest(
speechkitVersion='Not Speechkit',
serviceName=self.service,
uuid=self.uuid,
apiKey=self.key,
applicationName=self.app,
device='desktop',
coords='0, 0',
topic=self.topic,
lang=self.lang,
format=self.format,
punctuation=self.punctuation)
self.t.sendProtobuf(request)
return self.t.recvProtobuf(ConnectionResponse)
def upgrade_connection(self):
logger = logging.getLogger('arslib')
request = ('GET /asr_partial_checked HTTP/1.1\r\n'
'User-Agent: {user_agent}\r\n'
'Host: {host}:{port}\r\n'
'Upgrade: {service}\r\n\r\n').format(
user_agent=self.app,
host=self.host,
port=self.port,
service=self.service)
self.t.send(request)
check = 'HTTP/1.1 101 Switching Protocols'
buffer = ''
# possible infinite loop here?
while True:
buffer += self.t.recv(1)
if buffer.startswith(check) and buffer.endswith('\r\n\r\n'):
return True
if len(buffer) > 300:
logger.warning(buffer)
return False
def close(self):
self.session_id = ""
self.t.close()
def reconnect(self, delay=None):
self.log('Reconnecting!')
self.close()
if delay is not None:
self.log('Going to sleep for {0} seconds'.format(delay))
time.sleep(delay)
self.connect()
def add_data(self, chunk):
if chunk is None:
self.t.sendProtobuf(AddData(lastChunk=True))
else:
self.t.sendProtobuf(AddData(lastChunk=False, audioData=chunk))
def get_utterance_if_ready(self):
response = self.t.recvProtobufIfAny(AddDataResponse)
if response is not None:
if response.responseCode != 200:
error_text = 'Wrong response from server, status_code={0}'.format(
response.responseCode)
if response.HasField("message"):
error_text += ', message is "{0}"'.format(response.message)
raise ServerError(error_text)
self.log("got response: endOfUtt={0}; len(recognition)={1}".format(response.endOfUtt, len(response.recognition)))
if len(response.recognition) == 0:
return "", response.messagesCount
text = response.recognition[0].normalized.encode('utf-8')
merged = response.messagesCount
self.log("partial result: {0}; merged={1}".format(text, merged))
if response.endOfUtt:
return text, merged
else:
return "", response.messagesCount
return None, 0
示例2: ServerConnection
# 需要導入模塊: from transport import Transport [as 別名]
# 或者: from transport.Transport import recvProtobufIfAny [as 別名]
#.........這裏部分代碼省略.........
def connect(self):
self.t = Transport(self.host, self.port, timeout=None, verbose=False, enable_ssl=(self.port==443), ipv4=self.ipv4)
if not self.upgrade_connection():
raise ServerError('Unable to upgrade connection')
self.log("Connected to {0}:{1}.".format(self.host, self.port))
response = self.send_init_request()
if response.responseCode != 200:
error_text = 'Wrong response from server, status_code={0}'.format(
response.responseCode)
if response.HasField("message"):
error_text += ', message is "{0}"'.format(response.message)
raise ServerError(error_text)
self.session_id = response.sessionId
self.log("session_id={0}".format(self.session_id))
return self.session_id
def send_init_request(self):
request = ConnectionRequest(
speechkitVersion='',
serviceName=self.service,
uuid=self.uuid,
apiKey=self.key,
applicationName=self.app,
device='desktop',
coords='0, 0',
topic=self.topic,
lang=self.lang,
format=self.format,
punctuation=self.punctuation,
advancedASROptions=AdvancedASROptions(
utterance_silence=int(self.inter_utt_silence),
cmn_latency=self.cmn_latency,
capitalize=self.capitalize,
expected_num_count=self.expected_num_count,
biometry="children",
)
)
self.t.sendProtobuf(request)
return self.t.recvProtobuf(ConnectionResponse)
def upgrade_connection(self):
logger = logging.getLogger('arslib')
request = ('GET /asr_partial_checked HTTP/1.1\r\n'
'User-Agent: {user_agent}\r\n'
'Host: {host}:{port}\r\n'
'Upgrade: {service}\r\n\r\n').format(
user_agent=self.app,
host=self.host,
port=self.port,
service=self.service)
self.t.send(request)
check = 'HTTP/1.1 101 Switching Protocols'
buffer = ''
# possible infinite loop here?
while True:
buffer += self.t.recv(1)
if buffer.startswith(check) and buffer.endswith('\r\n\r\n'):
return True
if len(buffer) > 300:
logger.warning(buffer)
return False
def close(self):
self.session_id = ""
self.t.close()
def reconnect(self, delay=None):
self.log('Reconnecting!')
self.close()
if delay is not None:
self.log('Going to sleep for {0} seconds'.format(delay))
time.sleep(delay)
self.connect()
def add_data(self, chunk):
if chunk is None:
self.t.sendProtobuf(AddData(lastChunk=True))
else:
self.t.sendProtobuf(AddData(lastChunk=False, audioData=chunk))
def get_response_if_ready(self):
response = self.t.recvProtobufIfAny(AddDataResponse)
if response is not None:
if response.responseCode != 200:
error_text = 'Wrong response from server, status_code={0}'.format(
response.responseCode)
if response.HasField("message"):
error_text += ', message is "{0}"'.format(response.message)
raise ServerError(error_text)
return response