python笔记系列:探测web服务质量方法:pycurl

探测web服务质量方法:pycurl
easy_install pycurl #easy_install安装方法
pip install pycurl #pip安装方法
源码安装方法:(要求curl-config包支持,需要源码方式重新安装curl)
wget http://curl.haxx.se/download/curl-7.36.0.tar.gz
tar -zxvf curl-7.36.0.tar.gz
cd curl-7.36.0
./configure
make && make install
export LD_LIBRARY_PATH=/usr/local/lib
wget https://pypi.python.org/packages/source/p/pycurl/pycrul-7.19.3.1.tar.gz --no-check-certificate
tar -zxvf pycrul-7.19.3.1.tar.gz
cd pycrul-7.19.3.1
python setup.py install --curl-config=/usr/local/bin/curl-config

校验安装结果:
import pycurl
pycurl.version

pycurl.Curl()类实现创建一个libcurl包的Curl句柄对象,无参数。Curl几个常用方法:
close() 对应libcurl包中的curl_easy_cleanup方法 无参数
perform() 对应libcurl包中的curl_easy_perform方法 无参数
setopt(option,value) 对应libcurl包中的curl_easy_setopt方法,参数option通过libcurl的常量指定
getinfo(option) 对应libcurl包中的curl_easy_getinfo方法

实践:实现探测web服务质量(服务可用性、响应速度)
# -*- coding: utf-8 -*-
import os,sys
import time
import sys
import pycurl

URL="http://www.novowater.com.cn"
c = pycurl.Curl()
c.setopt(pycurl.URL, URL)
#连接超时时间,5秒
c.setopt(pycurl.CONNECTTIMEOUT, 5)

#下载超时时间,5秒
c.setopt(pycurl.TIMEOUT, 5)
c.setopt(pycurl.FORBID_REUSE, 1)
c.setopt(pycurl.MAXREDIRS, 1)
c.setopt(pycurl.NOPROGRESS, 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 "connecion 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()

探测web服务质量方法:pycurl
easy_install pycurl #easy_install安装方法
pip install pycurl #pip安装方法
源码安装方法:(要求curl-config包支持,需要源码方式重新安装curl)
wget http://curl.haxx.se/download/curl-7.36.0.tar.gz
tar -zxvf curl-7.36.0.tar.gz
cd curl-7.36.0
./configure
make && make install
export LD_LIBRARY_PATH=/usr/local/lib
wget https://pypi.python.org/packages/source/p/pycurl/pycrul-7.19.3.1.tar.gz --no-check-certificate
tar -zxvf pycrul-7.19.3.1.tar.gz
cd pycrul-7.19.3.1
python setup.py install --curl-config=/usr/local/bin/curl-config

校验安装结果:
import pycurl
pycurl.version

pycurl.Curl()类实现创建一个libcurl包的Curl句柄对象,无参数。Curl几个常用方法:
close() 对应libcurl包中的curl_easy_cleanup方法 无参数
perform() 对应libcurl包中的curl_easy_perform方法 无参数
setopt(option,value) 对应libcurl包中的curl_easy_setopt方法,参数option通过libcurl的常量指定
getinfo(option) 对应libcurl包中的curl_easy_getinfo方法

实践:实现探测web服务质量(服务可用性、响应速度)
# -*- coding: utf-8 -*-
import os,sys
import time
import sys
import pycurl

URL="http://www.novowater.com.cn"
c = pycurl.Curl()
c.setopt(pycurl.URL, URL)
#连接超时时间,5秒
c.setopt(pycurl.CONNECTTIMEOUT, 5)

#下载超时时间,5秒
c.setopt(pycurl.TIMEOUT, 5)
c.setopt(pycurl.FORBID_REUSE, 1)
c.setopt(pycurl.MAXREDIRS, 1)
c.setopt(pycurl.NOPROGRESS, 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 "connecion 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()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值