Skip to content

Instantly share code, notes, and snippets.

@ishikawa
Created July 31, 2008 09:32
Show Gist options
  • Select an option

  • Save ishikawa/3430 to your computer and use it in GitHub Desktop.

Select an option

Save ishikawa/3430 to your computer and use it in GitHub Desktop.
Handling urllib2.HTTPError, urllib2.URLError
# Handling urllib2.HTTPError, urllib2.URLError
# (via http://www.voidspace.org.uk/python/articles/urllib2.shtml)
from urllib2 import Request, urlopen, URLError, HTTPError
req = Request(someurl)
try:
response = urlopen(req)
except HTTPError, e:
print 'The server couldn\'t fulfill the request.'
print 'Error code: ', e.code
except URLError, e:
print 'We failed to reach a server.'
print 'Reason: ', e.reason
else:
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment