Making HTTP HEAD request with urllib2 from Python 2 Making HTTP HEAD request with urllib2 from Python 2 python python

Making HTTP HEAD request with urllib2 from Python 2


This works just fine:

import urllib2request = urllib2.Request('http://localhost:8080')request.get_method = lambda : 'HEAD'response = urllib2.urlopen(request)print response.info()

Tested with quick and dirty HTTPd hacked in python:

Server: BaseHTTP/0.3 Python/2.6.6Date: Sun, 12 Dec 2010 11:52:33 GMTContent-type: text/htmlX-REQUEST_METHOD: HEAD

I've added a custom header field X-REQUEST_METHOD to show it works :)

Here is HTTPd log:

Sun Dec 12 12:52:28 2010 Server Starts - localhost:8080localhost.localdomain - - [12/Dec/2010 12:52:33] "HEAD / HTTP/1.1" 200 -

Edit: there is also httplib2

import httplib2h = httplib2.Http()resp = h.request("http://www.google.com", 'HEAD')


Try httplib

>>> import httplib>>> conn = httplib.HTTPConnection("www.google.com")>>> conn.request("HEAD", "/index.html")>>> res = conn.getresponse()>>> print res.status, res.reason200 OK>>> print res.getheaders()[('content-length', '0'), ('expires', '-1'), ('server', 'gws'), ('cache-control', 'private, max-age=0'), ('date', 'Sat, 20 Sep 2008 06:43:36 GMT'), ('content-type', 'text/html; charset=ISO-8859-1')]

See How do you send a HEAD HTTP request in Python 2?


The problem lies with your class HeadRequest, which inherits from urllib2.Request. According to doc, urllib2.Request.__init__ signature is

 __init__(self, url, data=None, headers={}, origin_req_host=None, unverifiable=False) 

so you must pass an url argument to it. In your second try, you just do not use HeadRequest, this is why it works.