tags:

views:

154

answers:

3
>>> 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 is 500MB. Does my script first download the file, then check it? Or, can it immediately check the error code without saving the file?

A: 

i think your code already does that. you never call the read() method on the response, so you are never actually downloading the file's contents.

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

Corey Goldberg
So that means...if I were to check the status code of a 500gigabyte file..it would only take a second?
TIMEX
That's not entirely true. Because urllib sent a full request to the server, the server will start dumping it, even if it doesn't get all the way to the app.
Ken
Ken, I know what you mean, but his questions was how to do it without downloading the file. and in this case, no content is read by the client after the response header
Corey Goldberg
@corey: It might still block, and you're wasting bandwidth.
Jed Smith
That's true, but what he really wants is HEAD, which won't waste bandwidth on either side.
Ken
+1  A: 

Yes, it will fetch the file.

I think what you really want to do is send a HTTP HEAD request (which basically asks the server not for the data itself, but for the headers only). you can look here.

Ofri Raviv
+8  A: 

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

That'll save at least one of you bandwidth, while simply not doing a read() will only not bother getting the full file.

Try this:

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

The status code will be printed. Url should only be a segment, like "/foo" and hostname should be like, "www.example.com".

Ken
in py3k it's `http.client` instead of `httplib` and the rest is exactly the same.
SilentGhost