check if an URL exists without opening it

Joonas Paalasmaa joonas.paalasmaa at iki.fi
Sat Aug 10 14:36:49 EDT 2002


In article <3d541502$0$549$626a54ce at news.free.fr>, David wrote:
> 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 !

Use httplib module to issue a HEAD request that downloads only headers, 
not the actual file. For further information refer to the httplib 
documentation.

http = httplib.HTTP(servername)

http.putrequest("HEAD", path)
http.putheader("Host", servername)
http.endheaders()

errcode, errmsg, headers = http.getreply()

if errcode == 200:
    print "file exists"



More information about the Python-list mailing list