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
147 views
in Technique[技术] by (71.8m points)

python - python如何解码http响应(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.

(我正在使用以下代码从api端点登录和检索数据,但似乎响应已编码,因此我无法读取内容。)

i am using request requestes-0.0.1

(我正在使用请求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

(有人可以告诉我如何解码和读取响应中的数据)

  ask by mikeraj2019 translate from so

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

1 Reply

0 votes
by (71.8m points)

The response is encoded with brotli compression.

(响应使用brotli压缩进行编码。)

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.

(您可以从pypi安装第三方软件包以将其解压缩-有许多可用的软件包。)

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”标头中删除'br' :)

'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

...