HTTP requests in Python
posted Tue 17 Feb 2015 by Michael Galloy under PythonThe “Zen of Python” provides the basic philosophy of Python. From PEP 20:
There should be one – and preferably only one — obvious way to do it.
I doubt there is a single area in Python that violates this more than making a simple HTTP request. Python provides at least six builtin libraries to do this: httplib, httplib2, urllib, urllib2, urllib3, and pycurl. There are several reviews comparing the various libraries.
But there is a third party library, requests, that might be the “obvious way to do it” now:
import requests, json
url = 'https://api.github.com/repos/mgalloy/mglib'
r = requests.get(url, auth=('mgalloy', 'my_password'))
print r.json()['updated_at']
requests is installed as part of Anaconda, which an easy way to get all the core scientific programming packages for Python.