How to check the status code of an HTTP object without loading it?

>>> a=urllib.urlopen('http://www.domain.com/bigvideo.avi') >>> a.getcode() 404 >>> a=urllib.urlopen('http://www.google.com/') >>> a.getcode() 200 

My question is: bigvideo.avi - 500 MB. My script file downloads the file first and then validates it? Or, can he immediately check the error code without saving the file?

+4
source share
3 answers

You want to actually tell the server not to send the full contents of the file. HTTP has a mechanism for this called "HEAD", which is an alternative to "GET". It works the same way, but the server sends you headers, none of the actual content.

This will save at least one of your bandwidths, while just not doing read () will not bother getting the full file.

Try the following:

 import httplib c = httplib.HTTPConnection(<hostname>) c.request("HEAD", <url>) print c.getresponse().status 

A status code will be printed. Url should be only a segment, for example "/ foo", and the host name should be "www.example.com".

+16
source

Yes, it will extract the file.

I think that you really want to send an HTTP HEAD request (which basically the server does not request for the data itself, but only for the headers). You can look here .

+1
source

I think your code is already doing this. you never call the read () method in a response, so you never download the contents of a file.

even better ... you could send an HTTP HEAD request using httplib instead of doing the HTTP GET that your urllib code does.

0
source

All Articles