What I'm trying to do here is get the headers of a given URL so I can determine the MIME type. I want to be able to see if http://somedomain/foo/
will return an HTML document or a JPEG image for example. Thus, I need to figure out how to send a HEAD request so that I can read the MIME type without having to download the content. Does anyone know of an easy way of doing this?
Python – How to send a HEAD HTTP request in Python 2
content-typehttphttp-headerspythonpython-2.7
Related Topic
- Python – How to change the size of figures drawn with Matplotlib
- How to send a header using a HTTP request through a cURL call
- Python – How to get the current time in Python
- Rest – HTTP GET with request body
- Python – How to concatenate two lists in Python
- Java – How to use java.net.URLConnection to fire and handle HTTP requests
- How are parameters sent in an HTTP POST request
Best Answer
urllib2 can be used to perform a HEAD request. This is a little nicer than using httplib since urllib2 parses the URL for you instead of requiring you to split the URL into host name and path.
Headers are available via response.info() as before. Interestingly, you can find the URL that you were redirected to: