check if an URL exists without opening it

Michael Chermside mcherm at destiny.com
Fri Aug 9 15:45:29 EDT 2002


> I would like to check if an URL exists.
> (for instance http://www.yahoo.com/try.pdf)
> 
> The method urllib.open is unsatisfactory because the URL (which will be a
> file in my program) is opened ! So it can take too long time, just to check
> the existence !

Unfortunately, this is impossible for many kinds of URLs. http: urls, 
for instance, can only be detected by an attempt to download them.

You COULD try opening the URL and then abandoning the download as soon 
as you get the first few bytes of the content, but I wouldn't advise it. 
A huge amount of the overhead is in creating the TCP/IP connection and 
sending HTTP headers.. if there's a reasonable chance you'll want to 
download the contents of the URL I'd go ahead and do it.

-- Michael Chermside








More information about the Python-list mailing list