check if an URL exists without opening it

Joonas Paalasmaa joonas.paalasmaa at
Sat Aug 10 20:36:49 CEST 2002

In article <3d541502$0$549$626a54ce at>, David wrote:
> I would like to check if an URL exists.
> (for instance
> The method 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 

http = httplib.HTTP(servername)

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

errcode, errmsg, headers = http.getreply()

if errcode == 200:
    print "file exists"

More information about the Python-list mailing list