Skip to content

Instantly share code, notes, and snippets.

@yinchunxiang
Created February 8, 2017 11:22
Show Gist options
  • Save yinchunxiang/0bf7f3a04bc9159f86ff028cc3ae35b0 to your computer and use it in GitHub Desktop.
Save yinchunxiang/0bf7f3a04bc9159f86ff028cc3ae35b0 to your computer and use it in GitHub Desktop.
send chunked http request by python
import httplib
import time
chunk1 = "custname=bob&custtel=11111&custemail=bob%40email.com&si"
chunk2 = "ze=medium&topping=bacon&delivery=11%3A00&comments=if+you%27re+late+we+get+it+free"
if __name__ == "__main__":
#conn = httplib.HTTPConnection('httpbin.org')
#conn = httplib.HTTPConnection('requestb.in')
conn = httplib.HTTPConnection('ros.roobo.net')
url = "/voice/v1/query?agentId=2&token=f7caaf310da3dcb24bacdc7944456210&sessionId=xxx"
conn.connect()
conn.putrequest('POST', url)
conn.putheader('Transfer-Encoding', 'chunked')
conn.putheader('Content-Type', 'audio/pcm;bit=16;rate=8000')
conn.endheaders()
output = ""
with open('./tq.pcm') as f:
output = f.read()
step = 200
n = len(output)
for i in range(0, 5, step):
if i + step >= n:
conn.send("%s\r\n" % hex(len(output[i:]))[2:])
conn.send("%s\r\n" % output[i:])
else:
conn.send("%s\r\n" % hex(step)[2:])
conn.send("%s\r\n" % output[i: i+step])
#conn.send("%s\r\n" % hex(len(chunk1))[2:])
#conn.send("%s\r\n" % chunk1)
#time.sleep(1)
#conn.send("%s\r\n" % hex(len(chunk2))[2:])
#conn.send("%s\r\n" % chunk2)
conn.send("0\r\n\r\n")
r = conn.getresponse()
print r.status, r.reason, r.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment