简体   繁体   English

通用Python HTTP响应解码器

[英]Universal Python HTTP response decoder

I may be poor at googling, but so far I have come up dry. 我可能没有很好的谷歌搜索功能,但到目前为止我已经干了。 Is there no such thing as a universal decoder for HTTP responses, where you give it the body and the headers, and it returns the decoded data? 没有用于HTTP响应的通用解码器这种东西,您可以在其中为其提供正文和标头,并返回解码后的数据?

For example: 例如:

response = requests.get("...")
body = clever_package.decode(response.body, response.headers)

This is using the requests package to get the data, though this isn't strictly necessary. 这是使用请求包来获取数据,尽管这并非绝对必要。 Is there no universal decoder which takes the contentType and isBase64Encoded headers and works its magic? 是否没有通用的解码器采用contentType和isBase64Encoded标头并发挥其魔力?

Perhaps I'm not seeing an obvious flaw in such a package, which explains why I can't find it anywhere. 也许我没有在这样的程序包中看到明显的缺陷,这可以解释为什么我在任何地方都找不到它。

Cheers! 干杯!

What do you mean with decoding? 解码是什么意思? Just bytes to string data? 只是字节到字符串数据? In that case, python-chardet would be what you are looking for for cases where the header doesn't specify the encoding (if the headers specify the decoding, just decode it from the encoding specified in the header). 在这种情况下,如果标头未指定编码(如果标头指定了解码,只需从标头中指定的编码进行解码),就可以找到python-chardet

If you want to parse XML, JSON, ... in different ways, you'd probably use the respective libraries (built-in json module, yaml module, etc..) after having decoded the data into a unicode string. 如果要以不同的方式解析XML,JSON等,则在将数据解码为Unicode字符串后,可能会使用各自的库(内置json模块, yaml模块等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM