Home  >  Article  >  Backend Development  >  How to Send a HEAD HTTP Request in Python 2?

How to Send a HEAD HTTP Request in Python 2?

DDD
DDDOriginal
2024-11-01 23:46:29821browse

How to Send a HEAD HTTP Request in Python 2?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn