srodriguex srodriguex - 10 months ago 62
Python Question

Is there a way to get response headers without download the url content in Python?

I'm using Python to write a manager that will download some files given some conditions. The problem is that the conditions are to be performed against the response headers.

The example below is a simplified version of what I'm doing now. I first download the file and then test whether its name, contained in the headers, is in a list defined previously.

I'd like to know if is there a way to get the response without download the content, which takes a huge time in my real case.

import requests

# The line below download the file, but I'd like not to do it.
req = requests.get('')

# Get the name of the file to test if it's the right file.
r ='filename="(.*)";', req.headers['Content-Disposition'])

filename = None

# If the filename is present in the headers...
if r.groups():
filename = r.groups()[0]

# If the filename is in an authorized list...
if filename in [...]:
# Process req.content

Answer Source

You can use requests.head() instead of requests.get().