Last active
August 29, 2015 14:07
-
-
Save cansadadeserfeliz/7d3d93d8756dc9edbf9a to your computer and use it in GitHub Desktop.
Send GET request and manage exceptions
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
import socket | |
import httplib | |
import urllib2 | |
import logging | |
# Get an instance of a logger | |
logger = logging.getLogger(__name__) | |
def send_request(url): | |
try: | |
response = urllib2.urlopen(url.encode('utf8')) | |
except urllib2.URLError as exc: | |
logger.error( | |
u'Error fetching data (HTTPError); error code "{0}"'.format( | |
unicode(exc.code) | |
), | |
) | |
return {'success': False} | |
except urllib2.URLError as exc: | |
logger.error( | |
u'Error fetching data (URLError): "{0}"'.format( | |
unicode(exc.reason) | |
) | |
) | |
return {'success': False} | |
except httplib.HTTPException as exc: | |
logger.error( | |
u'Error fetching data (HTTPException):"{0}"'.format( | |
unicode(exc), | |
) | |
) | |
return {'success': False} | |
except socket.timeout, exc: | |
logger.info( | |
u'Timeout error:"{0}"'.format( | |
unicode(exc), | |
) | |
) | |
return {'success': False} | |
return { | |
'success': True, | |
'data': response.read(), | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment