Python Requests/urllib — monitoring bandwidth usage

我们两清 提交于 2019-12-18 19:53:42

问题


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?


回答1:


You can access the r.request object to calculate outgoing bytes, and you can determine incoming bytes (compressed or not) by looking at the content-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:

 def header_size(headers):
     return sum(len(key) + len(value) + 4 for key, value in headers.items()) + 2

There is also the initial line; that's {method} {path_url} HTTP/1.1{CRLF} for requests, and HTTP/1.x {status_code} {reason}{CRLF} for the response. Those lengths are all also available to you.

Total size then is:

 request_line_size = len(r.request.method) + len(r.request.path_url) + 12
 request_size = request_line_size + header_size(r.request.headers) + int(r.request.headers.get('content-length', 0))
 response_line_size = len(r.response.reason) + 15
 response_size = response_line_size + header_size(r.headers) + int(r.headers.get('content-length', 0))
 total_size = request_size + response_size


来源:https://stackoverflow.com/questions/33064891/python-requests-urllib-monitoring-bandwidth-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!