requests: Python HTTP Requests for Humans™

IndyPy 2/21/2012

claytron

Presenter Notes

What is requests?

badass.

Presenter Notes

What is requests?

A replacement for the standard library HTTP tools.

Requests was developed with a few PEP 20 idioms in mind.

  • Beautiful is better than ugly.
  • Explicit is better than implicit.
  • Simple is better than complex.
  • Complex is better than complicated.
  • Readability counts.
  • All contributions to Requests should keep these important rules in mind.

Presenter Notes

Std Lib Example

>>>> import urllib2
>>>> gh_url = 'https://api.github.com'
>>>> req = urllib2.Request(gh_url)
>>>> password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
>>>> password_manager.add_password(None, gh_url, 'user', 'pass')
>>>> auth_manager = urllib2.HTTPBasicAuthHandler(password_manager)
>>>> opener = urllib2.build_opener(auth_manager)
>>>> urllib2.install_opener(opener)
>>>> handler = urllib2.urlopen(req)
>>>> print handler.getcode()
200
>>>> print handler.headers.getheader('content-type')
'application/json'

Presenter Notes

Bob

Did you just tell me to go f*ck myself?

yeah, go fuck yourself

Presenter Notes

Requests Example

>>> auth = ('user', 'pass')
>>> req = requests.get('https://api.github.com', auth=auth)
>>> req.status_code
200
>>> req.headers['content-type']
'application/json'

Presenter Notes

Status codes

>>> req = requests.get('http://httpbin.org/status/404')
>>> req.status_code
404
>>> req.raise_for_status()
Traceback (most recent call last):
  File "requests/models.py", line 394, in raise_for_status
    raise self.error
urllib2.HTTPError: HTTP Error 404: NOT FOUND

Presenter Notes

Headers

>>> req.headers
{
    'status': '200 OK',
    'content-encoding': 'gzip',
    'transfer-encoding': 'chunked',
    'connection': 'close',
    'server': 'nginx/1.0.4',
    'x-runtime': '148ms',
    'etag': '"e1ca502697e5c9317743dc078f67693f"',
    'content-type': 'application/json; charset=utf-8'
}

Presenter Notes

Cookies

>>> url = 'http://httpbin.org/cookies/set/requests-is/awesome'
>>> r = requests.get(url)
>>> print r.cookies
{'requests-is': 'awesome'}

# To send your own cookies to the server, you can use the cookies
# parameter
>>> url = 'http://httpbin.org/cookies'
>>> cookies = dict(cookies_are='working')
>>> r = requests.get(url, cookies=cookies)
>>> r.text
'{"cookies": {"cookies_are": "working"}}'

Presenter Notes

POST

r = requests.post("http://httpbin.org/post")
>>> r = requests.post("http://httpbin.org/post", data=payload)
>>> print r.text
{
  "origin": "179.13.100.4",
  "files": {},
  "form": {
    "key2": "value2",
    "key1": "value1"
  },
  "url": "http://httpbin.org/post",
  "args": {},
  "headers": {
      "..."
  },
  "data": ""
}

Presenter Notes

Configuration

>>> my_config = {'verbose': sys.stderr}
>>> requests.get('http://httpbin.org/headers', config=my_config)
2011-08-17T03:04:23.380175   GET   http://httpbin.org/headers
<Response [200]>

Presenter Notes

Links

  • Requests - The radical new dude
  • urllib3 - What makes the rad, rad
  • urllib2 - The old dude
  • httpbin - Testing for HTTP
  • Landslide - What this presentation was made with, in just a few minutes...

Presenter Notes

requests: Python HTTP Requests for Humans™ - Clayton Parker - IndyPy