Size of raw response in bytes

后端 未结 2 1419
小鲜肉
小鲜肉 2021-02-01 03:14

I need to make a HTTP request and determine the response size in bytes. I have always used request for simple HTTP requests, but I am wondering if I can achieve th

相关标签:
2条回答
  • 2021-02-01 04:00

    Just take the len() of the content of the response:

    >>> response = requests.get('https://github.com/')
    >>> len(response.content)
    51671
    

    If you want to keep the streaming, for instance if the content is (too) large you can iterate over chunks of the data and sum their sizes:

    >>> with requests.get('https://github.com/', stream=True) as response:
    ...     size = sum(len(chunk) for chunk in response.iter_content(8196))
    >>> size
    51671
    
    0 讨论(0)
  • 2021-02-01 04:18

    r.raw is an instance of urllib3.response.HTTPResponse. We can count the length of response by looking up the response's header Content-length or use built-in function len().

    0 讨论(0)
提交回复
热议问题