你知道吗输入文件地址:
I am Hungry
I am going home
I am going to Germany
完整代码:
"""Represent a part in a multipart messsage"""
def __init__(self, name, contentType, data, paramName=None):
super(Part, self).__init__()
self.name = name
self.paramName = paramName
self.contentType = contentType
self.data = data
def encode(self):
body = BytesIO()
if self.paramName:
body.write('Content-Disposition: form-data; name="%s"; paramName="%s"\r\n' % (self.name, self.paramName))
else:
body.write('Content-Disposition: form-data; name="%s"\r\n' % (self.name,))
body.write("Content-Type: %s\r\n" % (self.contentType,))
body.write("\r\n")
body.write(self.data)
return body.getvalue()
class Request(object):
"""A handy class for creating a request"""
def __init__(self):
super(Request, self).__init__()
self.parameters = []
def add_json_parameter(self, name, paramName, data):
self.parameters.append(Part(name=name, paramName=paramName, contentType="application/json; charset=utf-8", data=data))
def add_audio_parameter(self, name, paramName, data):
self.parameters.append(Part(name=name, paramName=paramName, contentType="audio/x-wav;codec=pcm;bit=16;rate=16000", data=data))
def encode(self):
boundary = uuid.uuid4().hex
body = BytesIO()
for parameter in self.parameters:
body.write("--%s\r\n" % (boundary,))
body.write(parameter.encode())
body.write("\r\n")
body.write("--%s--\r\n" % (boundary,))
return body.getvalue(), boundary
host = "mtldev08.nuance.com"
port = 443
uri = "/NmspServlet/"
Error_Status = 0
RequestData = """{
"appKey": "9c9fa7201e90d3d96718bc3f36ce4cfe1781f2e82f4e5792996623b3b474fee2c77699eb5354f2136063e1ff19c378f0f6dd984471a38ca5c393801bffb062d6",
"appId": "NMDPTRIAL_AutomotiveTesting_NCS61HTTP",
"uId": "Alexander",
"inCodec": "PCM_16_8K",
"outCodec": "PCM_16_8K",
"cmdName": "NVC_TTS_CMD",
"appName": "Python",
"appVersion": "1",
"language": "eng-GB",
"carrier": "carrier",
"deviceModel": "deviceModel",
"cmdDict": {
"tts_voice": "Serena",
"tts_language": "eng-GB",
"locale": "canada",
"application_name": "Testing Python Script",
"organization_id": "NUANCE",
"phone_OS": "4.0",
"phone_network": "wifi",
"audio_source": "SpeakerAndMicrophone",
"location": "47.4925, 19.0513",
"application_session_id": "1234567890",
"utterance_number": "5",
"ui_langugage": "en",
"phone_submodel": "nmPhone2,1",
"application_state_id": "45"
}
}"""
TEXT_TO_READ = """{
"tts_type": "text",
"tts_input": "DUMMY"
}"""
scriptPath = os.path.abspath(__file__)
scriptPath = os.path.dirname(scriptPath)
fileInput = os.path.join(scriptPath, "input.txt")
if not os.path.exists(fileInput):
print "error message"
Error_Status = 1
sys.exit(Error_Status)
try:
content = open(fileInput, "r")
except IOError:
print "error message"
Error_Status = 1
sys.exit(Error_Status)
for line in content.readlines():
line = line.strip('\n')
if len(line):
TEXT_TO_READ = json.loads(TEXT_TO_READ)
TEXT_TO_READ["tts_input"]=line
TEXT_TO_READ = json.dumps(TEXT_TO_READ)
print json.dumps(TEXT_TO_READ)
#TEXT_TO_READ = json.loads(TEXT_TO_READ)
#print TEXT_TO_READ
request = Request()
request.add_json_parameter("RequestData", None, RequestData)
request.add_json_parameter("TtsParameter", "TEXT_TO_READ", TEXT_TO_READ)
body, boundary = request.encode()
try:
h = httplib.HTTPSConnection(host, port)
except (httplib.HTTPException, socket.error) as ex:
print "Error: %s" % ex
h.set_debuglevel(0)
headers = {
"Content-Type": "multipart/form-data; boundary=%s" % (boundary,),
"Connection": "Keep-Alive",
}
h.request('POST', uri, body, headers)
res = h.getresponse()
data = """MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=--Nuance_NMSP_vutc5w1XobDdefsYG3wq
""" + res.read()
msg = email.message_from_string(data)
for index, part in enumerate(msg.walk(), start=1):
content_type = part.get_content_type()
payload = part.get_payload()
if content_type == "audio/x-wav" and len(payload):
with open('line.pcm'.format(index), 'w') as f_pcm:
f_pcm.write(payload)
elif content_type == "application/json":
with open('line.txt'.format(index), 'w') as f_json:
f_json.write(payload)
sys.exit(Error_Status)
我想看报纸输入.txt逐行将其作为请求发送到服务器,然后分别保存响应。我只读取我需要的信息并保存在pcm文件和.txt文件中,但我逐行读取,但输出不生成行.txt以及线路.pcm文件。它只适用于一个输入示例(输入文件):我要去德国 但是我的输入.txt包含-这将是请求
I am Hungry
I am going home
I am going to Germany
我想,我在保存回复时犯了错误。谁来帮帮我。它不会产生行.txt以及线路.pcm三个输入。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐