import os
import sys
from io import BytesIO
import uuid
import httplib
rootdir = 'C:\Users\Desktop\Audio\'
class Part(object):
"""Represent a part in a multipart messsage"""
def __init__(rootdir, name, contentType, data, paramName=None):
super(Part, rootdir).__init__()
rootdir.name = name
rootdir.paramName = paramName
rootdir.contentType = contentType
rootdir.data = data
def encode(rootdir):
body = BytesIO()
if rootdir.paramName:
body.write('Content-Disposition: form-data; name="%s"; paramName="%s"\r\n' % (rootdir.name, rootdir.paramName))
else:
body.write('Content-Disposition: form-data; name="%s"\r\n' % (rootdir.name,))
body.write("Content-Type: %s\r\n" % (rootdir.contentType,))
body.write("\r\n")
body.write(rootdir.data)
return body.getvalue()
class Request(object):
"""A handy class for creating a request"""
def __init__(rootdir):
super(Request, rootdir).__init__()
rootdir.parameters = []
def add_json_parameter(rootdir, name, paramName, data):
rootdir.parameters.append(Part(name=name, paramName=paramName, contentType="application/json; charset=utf-8", data=data))
def add_audio_parameter(rootdir, name, paramName, data):
rootdir.parameters.append(Part(name=name, paramName=paramName, contentType="audio/x-wav;codec=pcm;bit=16;rate=16000", data=data))
def encode(rootdir):
boundary = uuid.uuid4().hex
body = BytesIO()
for parameter in rootdir.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/"
RequestData = """{
"appKey": "c6e2917da6dcc5e5dc5a57849eba1637ec92f98383c926aed8562154d63eb7fcd51bb57bd1907235ac2a553ab65fa3a29af2a7772829b890d6bc8e1f5a313bf1",
"appId": "AUDI_SDS_2017_EXT_20151203",
"uId": "nuance_internal_audi_obender",
"inCodec": "PCM_16_16K",
"outCodec": "PCM_16_16K",
"cmdName": "DRAGON_NLU_ASR_CMD",
"appName": "Python",
"appVersion": "1",
"language": "eng-GBR",
"carrier": "carrier",
"deviceModel": "deviceModel",
"cmdDict": {
"dictation_type": "ccpoi_dragondrive_specialized",
"dictation_language": "eng-GBR",
"locale": "germany",
"application": "AUDI_2017",
"organization_id": "Audi",
"phone_OS": "4.0",
"phone_network": "wifi",
"audio_source": "SpeakerAndMicrophone",
"location": "<47.4925, 19.0513> +/- 10.00m",
"application_session_id": "1234567890",
"utterance_number": "5",
"ui_langugage": "de",
"phone_submodel": "nmPhone2,1",
"application_state_id": "45"
}
}"""
REQUEST_INFO = """{"text" : "",
"start" : 0,
"end" : 0,
"binary_results": 0,
"nbest_text_results": 1,
"enable_intermediate_response": 0,
"enable_auto_punctuation": 0,
"appserver_data":
{
"appDataDict":"",
"time": "",
"timezone":"",
"return_nlu_results":"1",
"speaker": "Unknown",
"type": "conversation",
"action_mode": "verbose"
}
}"""
def myplayfunction(filename):
request = Request()
request.add_json_parameter("RequestData", None, RequestData)
request.add_json_parameter("DictParameter", "REQUEST_INFO", REQUEST_INFO)
with open(rootdir, "rb") as audioFile:
audioChunk = audioFile.read(100000)
while audioChunk:
request.add_audio_parameter("ConcludingAudioParameter", "AUDIO_INFO", audioChunk)
audioChunk = audioFile.read(100000)
body, boundary = request.encode()
for subdir, dirs, files in os.walk(rootdir):
for file in files:
myplayfunction(rootdir)
print os.path.join(subdir, file)
#print body
h = httplib.HTTPSConnection(host, port)
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()
print res.read()
I am reading the folder, which as sound file from the C:\Users\Desktop\Audio\ and later trying to send this as a http request to the server. But I am not able to send the http request to the server. Can some tell me what is the mistake that I am making here. I made some mistake within that ** ---- **. I am also getting the error as : Traceback (most recent call last):
Traceback (most recent call last):
File “C:/Users/Desktop/test”, line 136, in
“Content-Type”: “multipart/form-data; boundary=%s” % (boundary,),
NameError: name ‘boundary’ is not defined