Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

python how to decode http response

i am using the below code to login and retrieve data from an api endpoint but seems the response see encoded and i am not able to read the content. i am using request requestes-0.0.1

import requests
import json
import os


http_proxy  = "http://192.168.10.20:8888"
https_proxy = "https://192.168.10.20:8888"

proxyDict = {
              "http"  : http_proxy,
              "https" : https_proxy,
            }
session = requests.Session()

payloadopt = 'user_id=tom&password=xxxxx'
s = session.post('https://login.milock.com/api/login',data=payloadopt, proxies=proxyDict, verify=False, headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0', 'Accept':'application/json, text/plain, */*', 'Accept-Language':'en-US,en;q=0.5', 'Accept-Encoding':'gzip, deflate, br', 'Content-Type':'application/x-www-form-urlencoded'}, stream=True)

response when i print the same on console

??7?E`??YD??k???q??f,??G?(U?Uv?4???w&??!Ψ????E5Q?_??{?F??<.????4??> p{?k9???9??

Can someone tell me how can decode and read the data from the response

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The response is encoded with brotli compression. This compression method isn't supported by the standard library. You can install a third party package from pypi to decompress it - a number of packages are available.

For example

$ pip install brotli
>>> import brotli
>>> decompressed = brotli.decompress(response.content)
>>> dict_ = json.loads(decompressed)

If you'd prefer to avoid installing a third party module, remove 'br' from the 'accept-encoding' header in the request:

'Accept-Encoding':'gzip, deflate, br' -> 'Accept-Encoding':'gzip, deflate'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...