python 使用pycurl模組檢測web狀態品質

來源:互聯網
上載者:User

標籤:pycurl

pycurl模組安裝


pip install pycurl

源碼安裝方法:

wget https://curl.haxx.se/download/curl-7.29.0.tar.gz

tar zxf curl-7.29.0.tar.gz

cd curl-7.29.0/

./configure 

make && make install

export LD_LIBRARY_PATH=/usr/local/lib


wget https://pypi.python.org/packages/source/p/pycurl/pycurl-7.19.3.1.tar.gz --no-check-certificate

tar zxf pycurl-7.19.3.1.tar.gz

cd pycurl-7.19.3.1/

python setup.py install --curl-config=/usr/local/bin/curl-config


檢測web樣本:

#!/usr/bin/env python# -*- coding: utf-8 -*-import os,sysimport timeimport pycurlURL="http://www.baidu.com"c=pycurl.Curl()c.setopt(pycurl.URL,URL)c.setopt(pycurl.CONNECTTIMEOUT,5)c.setopt(pycurl.TIMEOUT,5)c.setopt(pycurl.NOPROGRESS,1)c.setopt(pycurl.FORBID_REUSE,1)c.setopt(pycurl.MAXREDIRS,1)c.setopt(pycurl.DNS_CACHE_TIMEOUT,30)indexfile=open(os.path.dirname(os.path.realpath(__file__))+"/content.txt","wb")c.setopt(pycurl.WRITEHEADER,indexfile)c.setopt(pycurl.WRITEDATA,indexfile)try:        c.perform()except Exception,e:        print "connection error 串連錯誤:"+str(e)        indexfile.close()        c.close()        sys.exit()NAMELOOKUP_TIME=c.getinfo(c.NAMELOOKUP_TIME)CONNECT_TIME=c.getinfo(c.CONNECT_TIME)PRETRANSFER_TIME=c.getinfo(c.PRETRANSFER_TIME)STARTTRANSFER_TIME=c.getinfo(c.STARTTRANSFER_TIME)TOTAL_TIME=c.getinfo(c.TOTAL_TIME)HTTP_CODE=c.getinfo(c.HTTP_CODE)SIZE_DOWNLOAD=c.getinfo(c.SIZE_DOWNLOAD)HEADER_SIZE=c.getinfo(c.HEADER_SIZE)SPEED_DOWNLOAD=c.getinfo(c.SPEED_DOWNLOAD)print "HTTP狀態代碼:%s" % (HTTP_CODE)print "DNS解析時間:%.2f ms" % (NAMELOOKUP_TIME*1000)print "建立連線時間:%.2f ms" % (CONNECT_TIME*1000)print "準備傳輸時間:%.2f ms" % (PRETRANSFER_TIME*1000)print "傳輸開始時間:%.2f ms" % (STARTTRANSFER_TIME*1000)print "傳輸結束總時間:%.2f ms" % (TOTAL_TIME*1000)print "下載資料包大小:%d bytes/s" % (SIZE_DOWNLOAD)print "HTTP頭部大小:%d byte" % (HEADER_SIZE)print "平均下載速度:%d bytes/s" % (SPEED_DOWNLOAD)indexfile.close()c.close()



運行  python url.py  輸出檢測百度頁面的結果

HTTP狀態代碼:200

DNS解析時間:4.27 ms

建立連線時間:8.43 ms

準備傳輸時間:8.56 ms

傳輸開始時間:13.92 ms

傳輸結束總時間:22.70 ms

下載資料包大小:115717 bytes/s

HTTP頭部大小:948 byte

平均下載速度:5259863 bytes/s






python 使用pycurl模組檢測web狀態品質

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.