Created
January 15, 2014 04:47
-
-
Save krmaxwell/8430955 to your computer and use it in GitHub Desktop.
Wrapper function to handle logic around urllib2.urlopen()
This file contains 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
# Utility function to get a URL with error handling | |
# Accepts URL string or urllib2.Request object | |
def get_url(orig_request): | |
if isinstance(orig_request, basestring): | |
url = orig_request.encode('utf8') | |
request = urllib2.Request(url) | |
elif isinstance(orig_request, urllib2.Request): | |
request = orig_request | |
else: | |
return None | |
try: | |
response = urllib2.urlopen(request) | |
except urllib2.HTTPError as e: | |
sys.stderr.write("The server couldn't fulfill the request for URL %s: %s\n" % (request.get_full_url(), e)) | |
return None | |
except urllib2.URLError as e: | |
sys.stderr.write('We failed to reach a server for URL %s: %s\n' % (request.get_full_url(), e)) | |
return None | |
else: | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment