python 通过python发送chunked http请求

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 通过python发送chunked http请求相关的知识,希望对你有一定的参考价值。

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()

以上是关于python 通过python发送chunked http请求的主要内容,如果未能解决你的问题,请参考以下文章

python [python] chunk处理

win10+python3.5,使用requests抓取信息遇到chunked乱码的诡异问题。python2.7则不乱码

Python之HTTP响应报文

python http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in

Python列表神技能

如何用python通过163发送邮件