Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requests module and compression

I've been trying to find the default behavior of the requests module about the compression, but I couldn't find anything, so I'm asking here:

Does the requests module asks the server for compression by default, or not ? Do we have to specify it in the header ?

I found a page here talking about urllib, where it is said that the header must ask for compression to get it:

http://www.diveintopython.net/http_web_services/gzip_compression.html

Is it the same thing for requests ?

like image 641
JPFrancoia Avatar asked Oct 25 '15 20:10

JPFrancoia


Video Answer


1 Answers

You can test it yourself. Try the following:

import requests
req = requests.get("http://google.com")
print(req.request.headers)

This will print the headers sent to the server (the default ones, since no other headers are defined). On my system I get:

{'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.7.0 CPython/2.7.8 Linux/4.1.8-100.fc21.x86_64'}

So, gzip and deflate are accepted compression types. The server will choose one of those.

like image 167
Sven Festersen Avatar answered Oct 21 '22 01:10

Sven Festersen