Created
July 31, 2008 09:32
-
-
Save ishikawa/3430 to your computer and use it in GitHub Desktop.
Handling urllib2.HTTPError, urllib2.URLError
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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