Communication with
      python HTTP module
             dreampuf , jul 2012




12年7月6日星期五
introduction



      urllib2.urlopen("http://www.guokr.com")




12年7月6日星期五
introduction



                                                Pyt
                                                      hon
                                                            GC
                                                                 :(
      urllib2.urlopen("http://www.guokr.com").read()




12年7月6日星期五
introduction



       fd = urllib2.urlopen(“http://www.guokr.com”)
       print fd.read()
       fd.close()




12年7月6日星期五
introduction



     with contextlib.closing(urllib2.urlopen("URL")) as f:
       print f.read()




12年7月6日星期五
introduction


     import contextlib
     import urllib2
     with contextlib.closing(urllib2.urlopen("URL")) as f:
       print f.read()




12年7月6日星期五
introduction
                                           Pyt
                                                 hon
                                                       2.5
     from __futrure__ import with_statement :)
     import contextlib
     import urllib2
     with contextlib.closing(urllib2.urlopen("URL")) as f:
        print f.read()




12年7月6日星期五
introduction
 cj = cookielib.LWPCookieJar()
 cookie_support = urllib2.HTTPCookieProcessor(cj)
 opener = urllib2.build_opener(cookie_support,
 urllib2.HTTPHandler)
 urllib2.install_opener(opener)          Build
 postdata = urllib.urlencode(postdata)
 headers = {'User-Agent':'Mozilla/5.0 (X11; Linux
 i686; rv:8.0) Gecko/20100101 Firefox/8.0'}
 req = urllib2.Request(
   url = url,
   data = postdata,
   headers = headers
 )
 result = urllib2.urlopen(req)            Send
 text = result.read()
12年7月6日星期五
agenda


       urllib2

       httplib2

       pycurl

       requests



12年7月6日星期五
Mission



             Post https://api.github.com/user/repos
             With some data
             And our credentials




12年7月6日星期五
Urllib2




12年7月6日星期五
httplib2




12年7月6日星期五
httplib2
             urllib2 - The Missing Manual: HOWTO
             Fetch Internet Resources with Python


      www-authenticate: SCHEME realm="REALM"




12年7月6日星期五
httplib2
 Basic Authentication Process
 1.Client -> Server
 Get	
  /index.html	
  HTTP/1.0
 Host:www.google.com
 2.Server -> Client
 HTTP/1.0	
  401	
  Unauthorised
 Server:	
  nginx/1.0.13
 WWW-­‐AuthenBcate:	
  Basic	
  realm="github.com"
 Content-­‐Type:	
  text/html
 Content-­‐Length:	
  xxx
 3.Client -> Server
 Get	
  /index.html	
  HTTP/1.0
 Host:www.google.com
 AuthorizaBon:	
  Basic	
  xxxxxxxxxxxxxxxxxxxxxxxxxxxx
12年7月6日星期五
httplib2




12年7月6日星期五
pycurl




12年7月6日星期五
requests




12年7月6日星期五
Thank you !



12年7月6日星期五
reference

  •GitHub Document v3 #create
  http://developer.github.com/v3/repos/#create
  •Restful API in Python
  http://isbullsh.it/2012/06/Rest-api-in-python/
  •urllib2 - The Missing Manual
  http://www.voidspace.org.uk/python/articles/urllib2.shtml#id6
  •HTTP使用BASIC认证的原理及实现方法
  http://space.itpub.net/23071790/viewspace-709367




12年7月6日星期五

Communication with python_http_module

  • 1.
    Communication with python HTTP module dreampuf , jul 2012 12年7月6日星期五
  • 2.
    introduction urllib2.urlopen("http://www.guokr.com") 12年7月6日星期五
  • 3.
    introduction Pyt hon GC :( urllib2.urlopen("http://www.guokr.com").read() 12年7月6日星期五
  • 4.
    introduction fd = urllib2.urlopen(“http://www.guokr.com”) print fd.read() fd.close() 12年7月6日星期五
  • 5.
    introduction with contextlib.closing(urllib2.urlopen("URL")) as f: print f.read() 12年7月6日星期五
  • 6.
    introduction import contextlib import urllib2 with contextlib.closing(urllib2.urlopen("URL")) as f: print f.read() 12年7月6日星期五
  • 7.
    introduction Pyt hon 2.5 from __futrure__ import with_statement :) import contextlib import urllib2 with contextlib.closing(urllib2.urlopen("URL")) as f: print f.read() 12年7月6日星期五
  • 8.
    introduction cj =cookielib.LWPCookieJar() cookie_support = urllib2.HTTPCookieProcessor(cj) opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler) urllib2.install_opener(opener) Build postdata = urllib.urlencode(postdata) headers = {'User-Agent':'Mozilla/5.0 (X11; Linux i686; rv:8.0) Gecko/20100101 Firefox/8.0'} req = urllib2.Request( url = url, data = postdata, headers = headers ) result = urllib2.urlopen(req) Send text = result.read() 12年7月6日星期五
  • 9.
    agenda urllib2 httplib2 pycurl requests 12年7月6日星期五
  • 10.
    Mission Post https://api.github.com/user/repos With some data And our credentials 12年7月6日星期五
  • 11.
  • 12.
  • 13.
    httplib2 urllib2 - The Missing Manual: HOWTO Fetch Internet Resources with Python www-authenticate: SCHEME realm="REALM" 12年7月6日星期五
  • 14.
    httplib2 Basic AuthenticationProcess 1.Client -> Server Get  /index.html  HTTP/1.0 Host:www.google.com 2.Server -> Client HTTP/1.0  401  Unauthorised Server:  nginx/1.0.13 WWW-­‐AuthenBcate:  Basic  realm="github.com" Content-­‐Type:  text/html Content-­‐Length:  xxx 3.Client -> Server Get  /index.html  HTTP/1.0 Host:www.google.com AuthorizaBon:  Basic  xxxxxxxxxxxxxxxxxxxxxxxxxxxx 12年7月6日星期五
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
    reference •GitHubDocument v3 #create http://developer.github.com/v3/repos/#create •Restful API in Python http://isbullsh.it/2012/06/Rest-api-in-python/ •urllib2 - The Missing Manual http://www.voidspace.org.uk/python/articles/urllib2.shtml#id6 •HTTP使用BASIC认证的原理及实现方法 http://space.itpub.net/23071790/viewspace-709367 12年7月6日星期五