PycURl是一個C語言寫的libcurl的python綁定庫。libcurl 是一個自由的,而且容易使用的用在客戶端的 URL 傳輸庫。它的功能很強大,PycURL 是一個很是快速(參考多併發操做)和豐富完整特性的,可是有點複雜的接口。若是你須要些簡單或純 Python 的模塊,你能夠參考一下 urllib2 或 urlgrabber 。
Project Address:
http://pycurl.sourceforge.net/
Pypi:
https://pypi.python.org/pypi/pycurl
Github:
https://github.com/pycurl/pycurl
pip install:
liushadeMacBook-Pro:~ LiuSha$ sudo pip install pycurl
pycurl經常使用方法
##pycurl.Curl()類實現一個ibcurl包的Curl句柄對象,Curl對象經常使用的方法以下##
close():
對應libcurl包中curl_easy_cleanup方法,無參數,實現關閉回收Curl對象。
perform():
對應libcurl包中curl_easy_perform方法,無參數,實現Curl對象請求的提交。
setopt(option,value):
對應libcurl包中curl_easy_setopt方法,參數option經過libcurl的常量來定義,參數value的值會依賴option,能夠是一個字符串、整形、長整形、文件對象、列表、函數等。
###模塊導入###
>>> import pycurl
###建立curl對象###
>>> curl = pycurl.Curl()
###鏈接等待時間,0則不等待###
>>> curl.setopt(pycurl.CONNECTTIMEOUT,5)
###超時時間###
>>> curl.setopt(pycurl.TIMEOUT,5)
###下載進度條,非0則屏蔽###
>>> curl.setopt(pycurl.NOPROGRESS,0)
###指定HTTP重定向最大次數###
>>> curl.setopt(pycurl.MAXREDIRS,5)
###完成交互後強制斷開鏈接,不重用###
>>> curl.setopt(pycurl.FORBID_REUSE,1)
###設置DNS信息保存時間,默認爲120秒###
>>> curl.setopt(pycurl.DNS_CACHE_TIMEOUT,60)
###設置HTTP的User-Agent(自行設置時需跟着常規標準走)###
>>> curl.setopt(pycurl.USERAGENT,"www.ipython.me")
###設置請求的Url###
>>> curl.setopt(pycurl.URL,"http://www.ipython.me")
###將返回的HTTP HEADER定向到回調函數getheader###
>>> curl.setopt(pycurl.HEADERFUNCTION,getheader)
###將返回的內容定向到回調函數getbody###
>>> curl.setopt(pycurl.WRITEHEADERFUNCTION,getbody)
###將返回的HTTP HEADER定向到fileobj文件對象###
>>> curl.setopt(pycurl.WRITEHEADER,fileobj)
###將返回的HTML內容定向到fileobj文件對象###
>>> curl.setopt(pycurl.WRITEDATE,fileobj)
getinfo(option):
對應libcurl的curl_easy_getinfo方法,參數option經過libcurl的常量指定。
>>> curl = pycurl.Curl()
###返回HTTP狀態碼###
>>> curl.getinfo(pycurl.HTTP_CODE)
###傳輸結束時所消耗的總時間###
>>> curl.getinfo(pycurl.TOTAL_TIME)
###DNS解析所消耗的時間###
>>> curl.getinfo(pycurl.NAMELOOKUP_TIME)
###創建鏈接所消耗的時間###
>>> curl.getinfo(pycurl.CONNECT_TIME)
###從創建鏈接到準備傳輸所消耗的時間###
>>> curl.getinfo(pycurl.PRETRANSFER_TIME)
###從創建鏈接到數據開始傳輸所消耗的時間###
>>> curl.getinfo(pycurl.STARTTRANSFER_TIME)
###重定向所消耗的時間###
>>> curl.getinfo(pycurl.REDIRECT_TIME)
###上傳數據包大小###
>>> curl.getinfo(pycurl.SIZE_UPLOAD)
###下載數據包大小###
>>> curl.getinfo(pycurl.SIZE_DOWNLOAD)
###平均下載速度###
>>> curl.getinfo(pycurl.SPEED_DOWNLOAD)
###平均上傳速度###
>>> curl.getinfo(pycurl.SPEED_UPLOAD)
###HTTP頭部大小###
>>> curl.getinfo(pycurl.HEADER_SIZE)
簡單包裝一下實現WEB探測(curl_webSev.py):
#!/usr/bin/python
#--coding:utf-8--#
#-------------------------------------------------------------------------------
# Name: curl_webSev.py
#
# Author: LiuSha
#
# Created: 12/15/2014
# Copyright: (c) WDZJ-SA 2014
#-------------------------------------------------------------------------------
def curl_webSev(URL = 'www.ipython.me'):
_Curl = pycurl.Curl()
_Curl.setopt(pycurl.CONNECTTIMEOUT,5)
_Curl.setopt(pycurl.TIMEOUT,5)
_Curl.setopt(pycurl.NOPROGRESS,1)
_Curl.setopt(pycurl.FORBID_REUSE,1)
_Curl.setopt(pycurl.MAXREDIRS,1)
_Curl.setopt(pycurl.DNS_CACHE_TIMEOUT,30)
_Curl.setopt(pycurl.URL,URL)
try:
with open(os.path.dirname(os.path.realpath(__file__)) + "/content.txt",'w') as outfile:
_Curl.setopt(pycurl.WRITEHEADER,outfile)
_Curl.setopt(pycurl.WRITEDATA,outfile)
_Curl.perform()
except Exception as err:
print "exec error!\n\t%s" %err
sys.exit()
print "Http Code:\t%s" %_Curl.getinfo(_Curl.HTTP_CODE)
print "DNS lookup time:\t%s ms" %(_Curl.getinfo(_Curl.NAMELOOKUP_TIME) * 1000)
print "Create conn time:\t%s ms" %(_Curl.getinfo(_Curl.CONNECT_TIME) * 1000)
print "Ready conn time:\t%s ms" %(_Curl.getinfo(_Curl.PRETRANSFER_TIME) * 1000)
print "Tran Star time:\t%s ms" %(_Curl.getinfo(_Curl.STARTTRANSFER_TIME) * 1000)
print "Tran Over time:\t%s ms" %(_Curl.getinfo(_Curl.TOTAL_TIME) * 1000)
print "Download size:\t%d bytes/s" %_Curl.getinfo(_Curl.SIZE_DOWNLOAD)
print "HTTP header size:\t%d byte" %_Curl.getinfo(_Curl.HEADER_SIZE)
print "Avg download speed:\t%s bytes/s" %_Curl.getinfo(_Curl.SPEED_DOWNLOAD)
if __name__ == '__main__':
import os
import sys
import time
import pycurl
if sys.argv[1]:
curl_webSev(sys.argv[1])
else:
curl_webSev()
執行效果
##將域名做爲參數傳入##
liushadeMacBook-Pro:Python Code LiuSha$ python curl_webSev.py http://www.ipython.me
Http Code: 200
DNS lookup time: 5.134 ms
Create conn time: 50.056 ms
Ready conn time: 50.111 ms
Tran Star time: 142.599 ms
Tran Over time: 364.888 ms
Download size: 72185 bytes/s
HTTP header size: 242 byte
Avg download speed: 197827.0 bytes/s
##直接執行##
liushadeMacBook-Pro:Python Code LiuSha$ python curl_webSev.py
Http Code: 200
DNS lookup time: 4.486 ms
Create conn time: 47.92 ms
Ready conn time: 47.984 ms
Tran Star time: 141.122 ms
Tran Over time: 458.379 ms
Download size: 72185 bytes/s
HTTP header size: 242 byte
Avg download speed: 157478.0 bytes/s
##文件log部份內容##
HTTP/1.1 200 OK
Server: nginx/1.7.5
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Date: Mon, 15 Dec 2014 15:56:14 GMT
X-Page-Speed: 1.9.32.1-4238
Cache-Control: max-age=0, no-cache
<!DOCTYPE html>
<!--[if IE 7]>
<html class="ie ie7" lang="zh-CN">
<![endif]-->
<!--[if IE 8]>
<html class="ie ie8" lang="zh-CN">
<![endif]-->
<!--[if !(IE 7) | !(IE 8) ]><!-->
<html lang="zh-CN">
<!--<![endif]-->
<head>
<title>IT辰逸 - 熱愛IT技術與互聯網</title>
» 轉載保留版權:IT辰逸 » 《Python pycurl模塊淺析》
» 本文連接地址:http://www.ipython.me/python/python-pycurl.html
» 本文版權採起: BY-NC-SA 協議進行受權,轉載註明出處。除IT-Tools、News以及特別標註,本站全部文章均爲原創。
» 若是喜歡能夠: 點此訂閱本站html