Home > Article > Backend Development > How to Send a HEAD HTTP Request in Python 2?
To determine the MIME type of a given URL without downloading its content, you can send a HEAD request. Here's how to do it in Python 2:
urllib2 provides an easy way to send a HEAD request. Instead of manually splitting the URL into host name and path, it parses it for you.
<code class="python">import urllib2 class HeadRequest(urllib2.Request): def get_method(self): return "HEAD" response = urllib2.urlopen(HeadRequest("http://google.com/index.html"))</code>
The response object provides access to the headers:
<code class="python">response.info()</code>
Additionally, you can retrieve the URL that you were redirected to:
<code class="python">response.geturl()</code>
The above is the detailed content of How to Send a HEAD HTTP Request in Python 2?. For more information, please follow other related articles on the PHP Chinese website!