Created
August 15, 2018 07:34
-
-
Save ryumei/e9f738bbac5e3a025c6936a79cd86b29 to your computer and use it in GitHub Desktop.
GET HTTP with Python 2.7 standard lib
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
import urllib | |
import urllib2 | |
import urlparse | |
import json | |
def get(url, headers=None, params=None): | |
if params is not None: | |
query = urllib.urlencode(params) | |
url = '%s?%s' % (url, query) | |
req = urllib2.Request(url, headers=headers) | |
logging.debug("GET %s", url) | |
try: | |
res = urllib2.urlopen(req) | |
return json.loads(res.read()) | |
except urllib2.HTTPError as err: | |
logging.error("%s. Client error GET %s with status %d.", | |
err.reason, url, err.code) | |
except urllib2.URLError as err: | |
logging.exception(err) | |
except (ValueError, TypeError) as err: | |
logging.error(err) | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment