JavaSa
JavaSa

Reputation: 6241

urllib get request with payload

I'm having a python code which is using requests module.
I'm querying some api in order to fetch a file which come back in response.content

My question is if there is any way to calculate the content in Bytes before writing it to a file.

I suspect that I get sometimes truncated data, and want to filter out writing to file stage which may also be an issue.

Upvotes: 0

Views: 517

Answers (1)

Niels
Niels

Reputation: 194

import requests

The following will get the header information

r = requests.head(url)

With that you can access information like this for example:

print r.headers
{'accept-ranges': 'bytes',
'cache-control': 'max-age=467354',
'connection': 'keep-alive',
'content-length': '37475248',
'content-type': 'video/mp4',
'date': 'Sun, 01 Sep 2013 07:26:21 GMT',
'expires': 'Fri, 06 Sep 2013 17:15:35 GMT',
'last-modified': 'Fri, 09 Aug 2013 18:51:33 GMT'}

You can then find the field you are interested in and do the following

file_size = r.headers.get('content-length')

I believe that is what you are looking for - you can then perform any conversions on the length needed to get the size information you are after.

Upvotes: 2

Related Questions