pycurl是一个用C语言写的libcurl Python实现,功能非常强大,支持的操作协议后FTP、HTTP、HTTPS、TELNET等,可以理解成Linux下curl命令功能的Python封装,简单易用
本例通过调用pycurl提供的方法,实现探测Web服务质量的情况,比如响应HTTP状态码、请求延时、HTTP头信息、下载速度等,利用这些信息可以定位服务响应慢的具体环节。
pycurl.Curl()类实现创建一个libcurl包的Curl句柄对象,无参数。
close()方法,对应的libcurl包中的curl_easy_cleanup方法,无参数,实现关闭、回收Curl对象。
perform()方法,对应libcurl包中的curl_easy_perform方法,无参数,实现Curl对象请求的提交。
setopt(option,value)方法,对应libcurl包中的curl_easy_setopt方法,参数option是通过libcurl的常量来指定的,参数value的值依赖option,可以是一个字符串、整型、长整型、文件对象、列表或函数等
安装pycurl模块
1
[
root@
kurol
~
]
# python3 -m easy_install -i http://pypi.douban.com/simple/ pycurl
报错:
1setuptools. sandbox. UnpickleableException: ConfigurationError ( "Could not run curl-config: [Errno 2] No such file or directory: 'curl-config'", )
先安装libcurl-devel解决:
123[ root@ kurol ~ ]# yum - y install libcurl - devel[ root@ kurol ~ ]# python3 - m easy_install - i http: // pypi. douban. com / simple / pycurlFinished processing dependencies for pycurl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/python3
# _*_ coding:utf-8 _*_
import
sys,
os
import
time
import
pycurl
url
=
"http://fm.mykurol.com"
#探测的目标URL
c
=
pycurl.
Curl
(
)
#创建一个Curl对象
c.
setopt
(
pycurl.
URL,
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
)
#指定HTTP重定向的最大数为1
c.
setopt
(
pycurl.
DNS_CACHE_TIMEOUT,
30
)
#设置保存DNS信息的时间为30秒
#创建一个文件对象,以"web"方式打开,用来存储返回的http头部及页面内容
indexfile
=
open
(
os.
path.
dirname
(
os.
path.
realpath
(
__file__
))
+
"/content.txt",
"wb"
)
c.
setopt
(
pycurl.
WRITEHEADER,
indexfile
)
#将返回的HTTP HEADER定向到indexfile文件
c.
setopt
(
pycurl.
WRITEDATA,
indexfile
)
#将返回的HTML内容定向到indexfile文件对象
try:
c.
perform
(
)
except
Exception
as
e:
print
(
"connection error:"
+
str
(
e
))
indexfile.
close
(
)
c.
close
(
)
sys.
exit
(
)
NAMELOOKUP_TIME
=
c.
getinfo
(
c.
NAMELOOKUP_TIME
)
#获取DNS解析时间
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
)
#获取HTTP状态码
SIZE_DOWNLOAD
=
c.
getinfo
(
c.
SIZE_DOWNLOAD
)
#获取下载数据包的大小
HEADER_SIZE
=
c.
getinfo
(
c.
HEADER_SIZE
)
#获取HTTP头部大小
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 bytes/s"
%
(
HEADER_SIZE
))
print
(
"平均下载速度:%d bytes/s"
%
(
SPEED_DOWNLOAD
))
#关闭文件及curl对象
indexfile.
close
(
)
c.
close
(
)
执行结果:
123456789HTTP 状 态 码 : 200DNS 解 析 时 间 : 17.44 ms建 立 连 接 时 间 : 17.88 ms准 备 传 输 时 间 : 17.89 ms传 输 开 始 时 间 : 39.79 ms传 输 结 束 总 时 间 : 39.88 ms下 载 数 据 包 大 小 : 2526 bytes / sHTTP 头 部 大 小 : 389 bytes / s平 均 下 载 速 度 : 63333 bytes / s
查看获取的HTTP文件头部及页面内容content.txt
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960HTTP / 1.1 200 OKDate: Fri, 09 Jun 2017 03: 01: 46 GMTServer: Apache / 2.2.15 ( CentOS )X - Powered - By: PHP / 5.3.3Set - Cookie: PHPSESSID= qmhmq2hkbb3v5hs67rf38c5006; path= /Expires: Thu, 19 Nov 1981 08: 52: 00 GMTCache - Control: no - store, no - cache, must - revalidate, post - check= 0, pre - check= 0Pragma: no - cacheContent - Length: 2526Connection: closeContent - Type: text / html; charset= UTF - 8<! doctype html >< link href= "css.css" rel= "stylesheet" type= "text/css" />< title > MyKurol 电 影 推 荐 网 </ title >< link rel= "icon" href= "image/logo.ico" type= "img/x-ico" />< body >< link rel= "icon" href= "image/logo.ico" type= "img/x-ico" />< div class= "in" >< div class= "header" >< div class= "mykurol" >< a href= "index.php" class= "biaoyu" >< strong > 本 网 站 由 谢 育 政 设 计 </ strong ></ a ></ div >< div class= "key-sousuo" >< input type= "text" name= "mo_key" placeholder= "电影/导演/演员" >< input type= "submit" name= "mo_sub" value= "搜索" ></ div >< div class= "Inlogin" >< a href= "MovEncy.php" class= "movie-ency" > 电 影 大 全 </ a >< a href= "#" class= "movie-guess" > 猜 一 猜 </ a >< a href= "#" class= "movie-album" > 电 影 专 辑 </ a >< a href= "login.php" class= "login-sub" > 登 录 </ a >< a href= "reg.php" class= "reg-sub" > 注 册 </ a > </ div ></ div ></ div >< div style= "z-index:999; position:absolute; right: 20px; bottom:40%" >< div >< img src= "image/1495501340.png" style= "width:120px;" /></ div >< div style= "padding:0; margin:0; background-color:#FFF; width:120px; height:30px" >< a style= "color:#666; font-size:12px;" > 友 情 链 接 : < a href= "http://www.mygdmec.cn" style= "text-decoration:none; color:#F9F; font-size:12px" > 凡 梦 购 物 网 </ a ></ div ></ div > < div class= "body" >< div class= "flo" >< div class= "flo-biaoti" >< p href= "#" class= "movie-name" data - toggle= "tooltip" title= "《生化危机6》的详细介绍" >< strong > 生 化 危 机 6 </ strong ></ p >< p class= "movie-jieshao" >在 华 盛 顿 特 区 爱 丽 丝 被 威 斯 克 背 叛 后 人 类 几 乎 要 失 去 最 后 的 希 望 。 作 为 唯 一 的 幸 存 者 , 也 是 人 类 对 抗 僵 尸 大 军 的 最 后 防 线 , 爱 丽 丝 必 须 回 到 噩 梦 开 始 的 地 方—— 浣 熊 市 。 在 那 里 保护 伞 公 司 正 在 集 结 所 有 的 力 量 企 图 对 残 余 的 幸 存 者 发 起 最 后 的 打 击 。 < br >导 演 : 保 罗· 安 德 森 < br >主 演 : 米 拉· 乔 沃 维 奇 , 伊 恩· 格 雷 , 艾 丽· 拉 特 , 鲁 比· 罗 丝 , 李 准 基 , 肖 恩· 罗 伯 茨 , 威 廉· 利 维 , 伊 恩· 马 肯 < br >动 作 / 惊 悚 / 科 幻</ p > </ div ></ div ></ div >< div class= "foot" ></ div ></ div ></ body >