I want to log the total bytes downloaded and uploaded by my Python script.
total_downloaded_bytes = 0
def bandwidth_hook(r, *args, **kwargs):
global total_downloaded_bytes
total_downloaded_bytes += len(r.content)
req = requests.session()
req.hooks = {'response': bandwidth_hook}
The above code doesn't take into account HTTP compression (if I'm right) and the size of headers.
Is there a way to count total uploaded and downloaded bytes from a requests.session? If not, what about a script-wide count?
You can access the
r.request
object to calculate outgoing bytes, and you can determine incoming bytes (compressed or not) by looking at thecontent-length
header for the incoming request. This should suffice for 99% of all requests you normally would make.Calculating the byte size of headers is easy enough; just add up key and value lenghts, add 4 bytes for the colon and whitespace, plus 2 more for the blank line:
There is also the initial line; that's
{method} {path_url} HTTP/1.1{CRLF}
for requests, andHTTP/1.x {status_code} {reason}{CRLF}
for the response. Those lengths are all also available to you.Total size then is: