简体   繁体   中英

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?

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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