Can urllib check path exists on server?

rdmurray at bitdance.com rdmurray at bitdance.com
Tue Feb 10 13:08:25 EST 2009


Muddy Coder <cosmo_general at yahoo.com> wrote:
> urllib bridges up a client to a server, it works fine. I wonder: is
> there a method that can check the existence of a file in the server
> side? We can check such an existence on local filesystem by using
> os.path.exists(), can I do such a check on server? For example,
> http://somedomain.com/foo/bar.jpg is residing in a hosting server, can
> I use urllib to check if bar.jpg file existing or not? Thanks!

urllib implements the http protocol for http URLs, so you can only do
what the http protocol allows.  Thus if http://somedomain.com/foo/bar.jpg
is a valid URL on that server, you can do a urlopen, and then check
the 'getcode' method on the returned object to see if you got a '404'
(not found) code back.

--RDM




More information about the Python-list mailing list