Determine Whether File Exists On HTTP Server
FeU Hagen
Hartmut.Wallbrecher at web.de
Sat May 22 15:04:45 EDT 2004
This works with HTTP:
import sys # exc_info
import httplib # HTTPConnection
HOST = "www.python.org"
PAGE = "/path/to/some/file.html"
try:
c = httplib.HTTPConnection( HOST )
# c._http_vsn = 10; c._http_vsn_str = "HTTP/1.0"
c.connect( )
c.putrequest ( "GET", PAGE )
c.endheaders()
r = c.getresponse()
print "%s\n%s\n%s\n" % (r.status, r.reason, r.msg)
if r.status == 200: # OK
print "%s exists" % PAGE
PageContent = r.read() # this is the requested html file in a
string
elif r.status == 404: # not found
print "%s does not exist" % PAGE
Page404 = r.read() # this is the 404 page in a string
else:
print "%s : status %s %s %s" % (PAGE, r.status, r.reason, r.msg)
except:
print sys.exc_info()[1]
Greetings
Harald Walter
"OvErboRed" <publicNO at SPAMoverbored.net> wrote in message
news:Xns94F1EA84483Byangstaoverbored at 127.0.0.1...
> Hi, I'm trying to determine whether a given URL exists. I'm new to Python
> but I think that urllib is the tool for the job. However, if I give it a
> non-existent file, it simply returns the 404 page. Aside from grepping
this
> for '404', is there a better way to do this? (Preferrably, there is a
> solution that can be applied to both HTTP and FTP.) Thanks in advance.
More information about the Python-list
mailing list