try:
req = Request(theurl, txdata, txheaders) # create a request object
handle = urlopen(req) # and open it to return a handle on the url
except IOError, e:
print 'We failed to open "%s".' % theurl
if hasattr(e, 'code'):
print 'We failed with error code - %s.' % e.code
else:
print 'Here are the headers of the page :'
print handle.info() # handle.read() returns the page, handle.geturl() returns the true url of the page fetched (in case urlopen has followed any redirects, which it sometimes does)