Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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('http://some_url.com/some_file')

# Get the name of the file to test if it's the right file.
r = re.search(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
like image 226
srodriguex Avatar asked Oct 24 '25 01:10

srodriguex


1 Answers

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

like image 58
eddiem Avatar answered Oct 26 '25 16:10

eddiem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!