PyTux

Trips of a curious penguin.

Hello, time traveler! You are reading an article that is almost ten years old. The world has changed, and so have I and my opinions. There is a good chance what's below is not current, correct, or secure anymore, and maybe it never was. This page is preserved because I am an archivist at heart, but you have been warned.

Send a HEAD request in Python

There are a lot of questions on this topic around the web and common answers are to use httplib, that however is a really-low level library, or to use urllib2, but a lot of people complains about it returning to GET if following a redirect.

Here is my urllib2 solution, written looking at the code of urllib2.HTTPRedirectHandler and subclassing it in order to make it keep using the HeadRequest.

For example, here is a fast URL un-shortener (redirect follower) realized with the method above (and a fallback).