Reputation: 576
In python, I'd like to recover the number of bytes during a request.
I did something like this (with requests module):
r = requests.get("https://google.fr/")
Not really this but this looks like. I'd like to know how many bytes the request requires and returns.
Thanks !!
Upvotes: 2
Views: 6865
Reputation: 400
The below code works for https:
import requests
url = 'https://google.fr/'
# use the 'auth' parameter to send requests with HTTP Basic Auth:
r = requests.get(url, auth=('user', 'pass'))
a = len(r.content)
print(a)
Upvotes: 0
Reputation: 884
This is described in requests documentation shown here
http://docs.python-requests.org/en/master/user/advanced/
Be careful with your example though. python and requests in general have problems with SSL certificates. So the above
r=requests.get("https://google.fr/")
will give you
SSLError: ("bad handshake: Error([('SSL routines', 'ssl3_get_server_certificate', 'certificate verify failed')],)",)
you could do
r=requests.get("https://google.fr/",verify=False)
which is not recommended ... but ok atleast you will get something
but you will not get a good content-length value in r.headers
for the above site. Maybe some google specific issue.
in any case
len(r.content)
should give you the length of the bytes object if 'Content-Length' is missing
If you use http it will work
r=requests.get("http://google.fr")
r.headers['Content-Length']
You will get '5433'
or something like that
Instead try all kinds of request on http://httpbin.org
r=requests.get("http://httpbin.org/get")
r.headers['Content-Length']
you will get 256
which is the byte size that is returned
For info about the request sent to the server you use
r.requests.headers
Upvotes: 1