Skip to content

Instantly share code, notes, and snippets.

@krmaxwell
Created January 15, 2014 04:47
Show Gist options
  • Save krmaxwell/8430955 to your computer and use it in GitHub Desktop.
Save krmaxwell/8430955 to your computer and use it in GitHub Desktop.
Wrapper function to handle logic around urllib2.urlopen()
# 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