SlideShare une entreprise Scribd logo
1  sur  19
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日星期五

Contenu connexe

Tendances

Puppet的使用与管理
Puppet的使用与管理Puppet的使用与管理
Puppet的使用与管理Elain Ding
 
[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2
[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2
[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2Yang FaFa
 
快快樂樂學 Scrapy
快快樂樂學 Scrapy快快樂樂學 Scrapy
快快樂樂學 Scrapyrecast203
 
Google IoT Core 初體驗
Google IoT Core 初體驗Google IoT Core 初體驗
Google IoT Core 初體驗Simon Su
 
輕輕鬆鬆用 Nodejs 寫網路爬蟲
輕輕鬆鬆用 Nodejs 寫網路爬蟲輕輕鬆鬆用 Nodejs 寫網路爬蟲
輕輕鬆鬆用 Nodejs 寫網路爬蟲傑倫 鍾
 
Day16. Ktor Exposed - DAO
Day16. Ktor Exposed - DAODay16. Ktor Exposed - DAO
Day16. Ktor Exposed - DAO舒安 楊
 
Day17. Ktor Exposed - DSL 實作 #將聊天機器人收到的訊息,寫入Ktor的資料庫吧~
Day17. Ktor Exposed - DSL 實作 #將聊天機器人收到的訊息,寫入Ktor的資料庫吧~Day17. Ktor Exposed - DSL 實作 #將聊天機器人收到的訊息,寫入Ktor的資料庫吧~
Day17. Ktor Exposed - DSL 實作 #將聊天機器人收到的訊息,寫入Ktor的資料庫吧~舒安 楊
 
Shell奇技淫巧
Shell奇技淫巧Shell奇技淫巧
Shell奇技淫巧David Xie
 
Node.js從無到有 基本課程
Node.js從無到有 基本課程Node.js從無到有 基本課程
Node.js從無到有 基本課程Simon Su
 
Dive into kissy
Dive into kissyDive into kissy
Dive into kissyjay li
 
資料庫應用與實際操作
資料庫應用與實際操作資料庫應用與實際操作
資料庫應用與實際操作阿狗 郭
 
Log analytics
Log analyticsLog analytics
Log analyticsitseo
 

Tendances (15)

Puppet的使用与管理
Puppet的使用与管理Puppet的使用与管理
Puppet的使用与管理
 
Java soap
Java soapJava soap
Java soap
 
[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2
[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2
[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2
 
快快樂樂學 Scrapy
快快樂樂學 Scrapy快快樂樂學 Scrapy
快快樂樂學 Scrapy
 
Google IoT Core 初體驗
Google IoT Core 初體驗Google IoT Core 初體驗
Google IoT Core 初體驗
 
輕輕鬆鬆用 Nodejs 寫網路爬蟲
輕輕鬆鬆用 Nodejs 寫網路爬蟲輕輕鬆鬆用 Nodejs 寫網路爬蟲
輕輕鬆鬆用 Nodejs 寫網路爬蟲
 
Day16. Ktor Exposed - DAO
Day16. Ktor Exposed - DAODay16. Ktor Exposed - DAO
Day16. Ktor Exposed - DAO
 
Day17. Ktor Exposed - DSL 實作 #將聊天機器人收到的訊息,寫入Ktor的資料庫吧~
Day17. Ktor Exposed - DSL 實作 #將聊天機器人收到的訊息,寫入Ktor的資料庫吧~Day17. Ktor Exposed - DSL 實作 #將聊天機器人收到的訊息,寫入Ktor的資料庫吧~
Day17. Ktor Exposed - DSL 實作 #將聊天機器人收到的訊息,寫入Ktor的資料庫吧~
 
Shell奇技淫巧
Shell奇技淫巧Shell奇技淫巧
Shell奇技淫巧
 
Node.js從無到有 基本課程
Node.js從無到有 基本課程Node.js從無到有 基本課程
Node.js從無到有 基本課程
 
Dive into kissy
Dive into kissyDive into kissy
Dive into kissy
 
MySQL aio
MySQL aioMySQL aio
MySQL aio
 
Websocket
WebsocketWebsocket
Websocket
 
資料庫應用與實際操作
資料庫應用與實際操作資料庫應用與實際操作
資料庫應用與實際操作
 
Log analytics
Log analyticsLog analytics
Log analytics
 

En vedette

The introduction of data visualization
The introduction of data visualizationThe introduction of data visualization
The introduction of data visualizationdreampuf
 
Python client api
Python client apiPython client api
Python client apidreampuf
 
Refactoring
RefactoringRefactoring
Refactoringdreampuf
 
Python magicmethods
Python magicmethodsPython magicmethods
Python magicmethodsdreampuf
 
Machine learning share No.1
Machine learning share No.1Machine learning share No.1
Machine learning share No.1dreampuf
 
Python profiling
Python profilingPython profiling
Python profilingdreampuf
 
A short introduction of D3js
A short introduction of D3jsA short introduction of D3js
A short introduction of D3jsdreampuf
 
The Outcome Economy
The Outcome EconomyThe Outcome Economy
The Outcome EconomyHelge Tennø
 

En vedette (8)

The introduction of data visualization
The introduction of data visualizationThe introduction of data visualization
The introduction of data visualization
 
Python client api
Python client apiPython client api
Python client api
 
Refactoring
RefactoringRefactoring
Refactoring
 
Python magicmethods
Python magicmethodsPython magicmethods
Python magicmethods
 
Machine learning share No.1
Machine learning share No.1Machine learning share No.1
Machine learning share No.1
 
Python profiling
Python profilingPython profiling
Python profiling
 
A short introduction of D3js
A short introduction of D3jsA short introduction of D3js
A short introduction of D3js
 
The Outcome Economy
The Outcome EconomyThe Outcome Economy
The Outcome Economy
 

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日星期五
  • 13. httplib2 urllib2 - The Missing Manual: HOWTO Fetch Internet Resources with Python www-authenticate: SCHEME realm="REALM" 12年7月6日星期五
  • 14. 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日星期五
  • 19. 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日星期五