Tuesday, October 11, 2011

New Python simple http request library

Where

http://docs.python-requests.org/en/latest


What and why

A successful attempt at simplifying HTTP requests in python.
Full support for cookies, headers, HEAD method and sessions (use of with block).
Code looks simple and clean (can't think of better complement).


Code example

>>> r = requests.get('https://api.github.com', auth=('user', 'pass'))
>>> r.status_code
204
>>> r.headers['content-type']
'application/json'
>>> print r.cookies
{'requests-is': 'awesome'}

Hooks

There are hooks available. One can have a function called pre-request or post-request.
http://docs.python-requests.org/en/latest/user/advanced/#event-hooks


Recommendations

This library is used internally at Twitter