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

requests, czyli HTTP dla ludzi

requests, czyli HTTP dla ludzi

requests: HTTP for humans
My talk from PyWaw #2 (in Polish).

Zbigniew Siciarz

July 11, 2011
Tweet

More Decks by Zbigniew Siciarz

Other Decks in Programming

Transcript

  1. httplib urllib(2) requests na ratunek requests, czyli HTTP dla ludzi

    Zbigniew Siciarz PyWaw 11 lipca 2011 @zsiciarz requests, czyli HTTP dla ludzi
  2. httplib urllib(2) requests na ratunek Plan prezentacji 1 httplib 2

    urllib(2) 3 requests na ratunek @zsiciarz requests, czyli HTTP dla ludzi
  3. httplib urllib(2) requests na ratunek httplib import httplib conn =

    httplib.HTTPConnection("httpbin.org") conn.request("GET", "/status /418") response = conn.getresponse () print response.status , response.reason print response.read() @zsiciarz requests, czyli HTTP dla ludzi
  4. httplib urllib(2) requests na ratunek httplib zbyszek@wicherek :~/ Dokumenty/requests$ python

    code/httplib1.py 418 I’M A TEAPOT -=[ teapot ]=- _...._ .’ _ _ ‘. | ."‘ ^ ‘". _, \_;‘"---" ‘|// | ;/ \_ _/ ‘"""‘ @zsiciarz requests, czyli HTTP dla ludzi
  5. httplib urllib(2) requests na ratunek urllib/urllib2 obie biblioteki w stdlib

    ró»na funkcjonalno±¢ @zsiciarz requests, czyli HTTP dla ludzi
  6. httplib urllib(2) requests na ratunek urllib/urllib2 obie biblioteki w stdlib

    ró»na funkcjonalno±¢ urllib: kodowanie parametrów, pobieranie wprost do pliku @zsiciarz requests, czyli HTTP dla ludzi
  7. httplib urllib(2) requests na ratunek urllib/urllib2 obie biblioteki w stdlib

    ró»na funkcjonalno±¢ urllib: kodowanie parametrów, pobieranie wprost do pliku urllib2: obsªuga otwieraczy @zsiciarz requests, czyli HTTP dla ludzi
  8. httplib urllib(2) requests na ratunek urllib2 - Basic Auth import

    urllib2 url = ’http :// httpbin.org/basic -auth/user/pass’ user = ’user’ password = ’pass’ req = urllib2.Request(url) pwd_manager = urllib2.HTTPPasswordMgrWithDefaultRealm () pwd_manager.add_password(None , url , user , password) auth_manager = urllib2.HTTPBasicAuthHandler(pwd_manager) opener = urllib2.build_opener(auth_manager) urllib2.install_opener(opener) response = urllib2.urlopen(req) print response.getcode () print response.headers.getheader(’content -type’) print response.read() @zsiciarz requests, czyli HTTP dla ludzi
  9. httplib urllib(2) requests na ratunek urllib2 - Basic Auth import

    urllib2 url = ’http :// httpbin.org/basic -auth/user/pass’ user = ’user’ password = ’pass’ req = urllib2.Request(url) pwd_manager = urllib2.HTTPPasswordMgrWithDefaultRealm () pwd_manager.add_password(None , url , user , password) auth_manager = urllib2.HTTPBasicAuthHandler(pwd_manager) opener = urllib2.build_opener(auth_manager) urllib2.install_opener(opener) response = urllib2.urlopen(req) print response.getcode () print response.headers.getheader(’content -type’) print response.read() O RLY? @zsiciarz requests, czyli HTTP dla ludzi
  10. httplib urllib(2) requests na ratunek requests http://python-requests.org/ Requests: Python HTTP

    Module (That Doesn't Suck) @zsiciarz requests, czyli HTTP dla ludzi
  11. httplib urllib(2) requests na ratunek requests http://python-requests.org/ Requests: Python HTTP

    Module (That Doesn't Suck) Requests: The Simple (e.g. usable) HTTP Module @zsiciarz requests, czyli HTTP dla ludzi
  12. httplib urllib(2) requests na ratunek requests http://python-requests.org/ Requests: Python HTTP

    Module (That Doesn't Suck) Requests: The Simple (e.g. usable) HTTP Module Requests: HTTP for Humans @zsiciarz requests, czyli HTTP dla ludzi
  13. httplib urllib(2) requests na ratunek requests - API requests.get(...) requests.post

    requests.put @zsiciarz requests, czyli HTTP dla ludzi
  14. httplib urllib(2) requests na ratunek requests - API requests.get(...) requests.post

    requests.put requests.delete @zsiciarz requests, czyli HTTP dla ludzi
  15. httplib urllib(2) requests na ratunek requests - API requests.get(...) requests.post

    requests.put requests.delete requests.patch @zsiciarz requests, czyli HTTP dla ludzi
  16. httplib urllib(2) requests na ratunek requests - API requests.get(...) requests.post

    requests.put requests.delete requests.patch requests.options @zsiciarz requests, czyli HTTP dla ludzi
  17. httplib urllib(2) requests na ratunek requests - API requests.get(...) requests.post

    requests.put requests.delete requests.patch requests.options requests.head @zsiciarz requests, czyli HTTP dla ludzi
  18. httplib urllib(2) requests na ratunek requests - proste »¡danie GET

    import requests response = requests.get(’http :// httpbin.org/status /418’) print response.status_code print response.content @zsiciarz requests, czyli HTTP dla ludzi
  19. httplib urllib(2) requests na ratunek requests - Basic Auth import

    requests url = ’http :// httpbin.org/basic -auth/user/pass’ response = requests.get(url , auth=(’user’, ’pass’)) print response.status_code print response.headers[’content -type’] print response.content @zsiciarz requests, czyli HTTP dla ludzi
  20. httplib urllib(2) requests na ratunek requests - Basic Auth zbyszek@wicherek

    :~/ Dokumenty/requests$ python code/requests_2.py 2 application/json { "authenticated": true , "user": "user" } @zsiciarz requests, czyli HTTP dla ludzi
  21. httplib urllib(2) requests na ratunek requests - POST i upload

    pliku import requests data = dict(username=’’) with open(__file__) as f: response = requests.post(’http :// httpbin.org/post’, data=data , files ={’filename ’: f}) print response.status_code print response.content @zsiciarz requests, czyli HTTP dla ludzi
  22. httplib urllib(2) requests na ratunek requests - POST i upload

    pliku zbyszek@wicherek :~/ Dokumenty/requests$ python code/requests_3.py 2 { "origin": "::ffff :184.73.223.212", "files": { "filename": "import requests\n\n<CIACH >" }, "form": null , "url": "http :// httpbin.org/post", "args": {}, "headers": { "Content -Length": "555", <CIACH > "Content -Type": "multipart/form -data; boundary =3298 fc5c93854f96b712fa47 8e4cdef" }, "data": "username" } @zsiciarz requests, czyli HTTP dla ludzi
  23. httplib urllib(2) requests na ratunek Alternatywy PyCURL wrapper na libcurl

    Mechanize klient to "przegl¡darka" @zsiciarz requests, czyli HTTP dla ludzi