Obtain Web Page Information With Error Document Handling : Web Client « Network « Python






Obtain Web Page Information With Error Document Handling

import sys, urllib2

req = urllib2.Request(sys.argv[1])

try:
    fd = urllib2.urlopen(req)
except urllib2.HTTPError, e:
    print "Error retrieving data:", e
    print "Server errror document follows:\n"
    print e.read()
    sys.exit(1)
except urllib2.URLError, e:
    print "Error retrieving data:", e
    sys.exit(2)

print "Retrieved", fd.geturl()
info = fd.info()
for key, value in info.items():
    print "%s = %s" % (key, value)
    


           
       








Related examples in the same category

1.Obtain Web Page
2.Obtain Web Page With Full Error Handling
3.Obtain Web Page Information With Simple Error Handling
4.Submit GET Data
5.Basic Connection ExampleBasic Connection Example