pycurl是一个python的网络模块,支持FTP、HTTP以及TELNET等协议,类似于curl命令,可以对web服务质量进行探测。
pycurl模块安装方法有三种
#pip命令安装
pip install pycurl
#源码安装 需要安装好curl包
wget http://curl.haxx.se/download/curl-x.xx.x.tar.gz
tar -zxvf curl-x.xx.x.tar.gz
cd curl-x.xx.x
./configure
make && make install
然后
wget https://pypi.python.org/packages/source/p/pycurl/pycurl-x.xx.x.x.tar.gz --no-check-certificate
tar -zxvf pycurl-x.xx.x.x.tar.gz
cd pycurl-x.xx.x.x
python setup.py install --cutrl-config=/usr/local/bin/curl-config
然后在交互式上尝试import pycurl即可
pycurl模块的使用
Curl方法可以创建一个Curl句柄对象,也就是创建一个Curl类
close()方法。无参数,实现关闭,对Curl类对象的回收。
perform()方法,无参数,实现Curl对象的请求的提交
setopt(option,value)方法,value的值依赖于option,以下列举一些使用方法
from pycurl import *
test=Curl()
test.setopt(CONNECTTIMEOUT,5) #连接的等待时间,后一个参数可以设置时间的长短
test.setopt(TIMEOUT,5) #请求超时的时间
test.setopt(MAXREDIRS,5) #设置HTTP最大重定向的个数
test.setopt(FORBID_REUSE,1) #设置完成交互后强制断开连接
test.setopt(DNS_CACHE_TIMEOUT,60) #设置DNS的缓存信息的时间,默认的时间为120秒
test.setopt(URL,"http://www.baidu.com")#设定请求的URL
test.setopt(HEADERFUNCTION,getheader) #将请求定向到回调函数GETHEADER上
test.setopt(WRITEFUNCTION,getbody) #将返回内容定向到回调函数GETBODY上
test.setopt(WRITEHEADER,file1) #将返回的HTTP HEADER信息定向到file1文件上
test.setopt(WRITEDATA,file1) #将返回的HTML内容file1重定向到file1文件上
getinfo(option)方法,以下列举一些常用的使用方法
test.getinfo(HTTP_CODE) #返回HTTP的状态码
test.getinfo(TOTAL_TIME) #传输所用的总时间
test.getinfo(NAMELOOKUP_TIME) #DNS解析所使用的时间
test.getinfo(CONNECT_TIME) #连接所花的时间
test.getinfo(PRETRANSFER_TIME) #从连接建立到准备传输所花的时间
test.getinfo(STARTTRANSFER_TIME)#从建立连接到传输开始所花的时间
test.getinfo(REDIRECT_TIME) #重定向所消耗的时间
test.getinfo(SIZE_UPLOAD) #上传数据包的大小
test.getinfo(SIZE_DOWNLOAD) #下载数据包的大小
test.getinfo(SPEED_DOWNLOAD) #下载速度
test.getinfo(SPEED_UPLOAD) #上传速度
test.getinfo(HEADER_SIZE) #HTTP的头部大小