Can I POST data with python requests lib with http-gzip or deflate compression?

后端 未结 5 1770
深忆病人
深忆病人 2021-02-13 03:39

I use the request-module of python 2.7 to post a bigger chunk of data to a service I can\'t change. Since the data is mostly text, it is large but would compress quite well. The

5条回答
  •  攒了一身酷
    2021-02-13 04:21

    I can't get this to work, but you might be able to insert the gzip data into a prepared request:

    #UNPROVEN
    r=requests.Request('POST', 'http://httpbin.org/post', data={"hello":"goodbye"})
    p=r.prepare()
    s=StringIO.StringIO()
    g=gzip.GzipFile(fileobj=s,mode='w')
    g.write(p.body)
    g.close()
    p.body=s.getvalue()
    p.headers['content-encoding']='gzip'
    p.headers['content-length'] = str(len(p.body))  # Not sure about this
    r=requests.Session().send(p)
    

提交回复
热议问题