Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Deep Dive into Python Requests

A Deep Dive into Python Requests

In this not-actually-so-deep dive into requests we'll do a lightning sweep through some of requests' lesser-known features that you could be using to make your code faster, cleaner and more beautiful.

Cory Benfield

September 20, 2014

Transcript

18 slides
  1. Hi

  2. Cookie Management >>>  import  requests   >>>  s  =  requests.Session()

      >>>  s.get('http://www.google.com/')   >>>  len(s.cookies)   2
  3. Avoid Boilerplate >>>  s  =  requests.Session()   >>>  s.auth  =

     ('Cory',  'LOLNO')   >>>  s.get('http://some-­‐protected-­‐resource.org/')
  4. Connection Pooling #  Two  TCP  Connections   >>>  requests.get('http://www.google.com/')  

    >>>  requests.get('http://www.google.com/')   #  Re-­‐use  the  same  TCP  connection   >>>  s  =  requests.Session()   >>>  s.get('http://www.google.com/')   >>>  s.get('http://www.google.com/')
  5. Transport Adapters • Change the way requests manages connections. •

    Useful for applying connection- scope changes. • Think: SSL version, protocol etc.
  6. Streaming #  Does  not  download  response  body   >>>  r

     =  requests.get(url,  stream=True)   #  Now,  iterate  over  the  body,  in  chunks...   >>>  [x  for  x  in  r.iter_content(1024)]   #  or  lines...   >>>  [x  for  x  in  r.iter_lines()]   #  Downloads  all  at  once   >>>  r.content
  7. SSL Client Certs >>>  r  =  requests.get(url,  cert=(    

                 'cert.crt',  ‘cert.key’          ))

More Decks by Cory Benfield

Other Decks in Programming