How to Send a HEAD HTTP Request in Python 2?

DDD
Release: 2024-11-01 23:46:29
Original
756 people have browsed it

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>
Copy after login

The response object provides access to the headers:

<code class="python">response.info()</code>
Copy after login

Additionally, you can retrieve the URL that you were redirected to:

<code class="python">response.geturl()</code>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!