HTTPConncetion - HEAD request

gervaz gervaz at gmail.com
Fri Jun 17 13:53:39 EDT 2011


On 17 Giu, 12:14, Adam Tauno Williams <awill... at whitemice.org> wrote:
> On Thu, 2011-06-16 at 15:43 -0700, gervaz wrote:
> > Hi all, can someone tell me why the read() function in the following
> > py3 code returns b''
> > >>> h = http.client.HTTPConnection("www.twitter.com")
> > >>> h.connect()
> > >>> h.request("HEAD", "/", "HTTP 1.0")
> > >>> r = h.getresponse()
> > >>> r.read()
> > b''
>
> Because there is no body in a HEAD request.  What is useful are the
> Content-Type, Content-Length, and etag headers.
>
> Is r.getcode() == 200?  That indicates a successful response; you
> *always* much check the response code before interpreting the response.
>
> Also I'm pretty sure that "HTTP 1.0" is wrong.

Ok, thanks for the replies, just another question in order to have a
similar behaviour using a different approach...
I decided to implement this solution:

class HeadRequest(urllib.request.Request):
    def get_method(self):
        return "HEAD"

Now I download the url using:

r = HeadRequest(url, None, self.headers)
c = urllib.request.urlopen(r)

but I don't know how to retrieve the request status (e.g. 200) as in
the previous examples with a different implementation...

Any suggestion?

Thanks,

Mattia



More information about the Python-list mailing list