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
|
报错:
setuptools.sandbox.UnpickleableException: ConfigurationError("Could not run curl-config: [Errno 2] No such file or directory: 'curl-config'",)
先安装libcurl-devel解决:
1
2
3
|
[root@kurol ~]
# yum -y install libcurl-devel
[root@kurol ~]
# python3 -m easy_install -i http://pypi.douban.com/simple/ pycurl
Finished 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()
|
执行结果:
1
2
3
4
5
6
7
8
9
|
HTTP状态码:200
DNS解析时间:17.44 ms
建立连接时间:17.88 ms
准备传输时间:17.89 ms
传输开始时间:39.79 ms
传输结束总时间:39.88 ms
下载数据包大小:2526 bytes
/s
HTTP头部大小:389 bytes
/s
平均下载速度:63333 bytes
/s
|
查看获取的HTTP文件头部及页面内容content.txt
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
50
51
52
53
54
55
56
57
58
59
60
61
|
HTTP
/
1.1
200
OK
Date: Fri,
09
Jun
2017
03
:
01
:
46
GMT
Server: Apache
/
2.2
.
15
(CentOS)
X
-
Powered
-
By: PHP
/
5.3
.
3
Set
-
Cookie: PHPSESSID
=
qmhmq2hkbb3v5hs67rf38c5006; path
=
/
Expires: Thu,
19
Nov
1981
08
:
52
:
00
GMT
Cache
-
Control: no
-
store, no
-
cache, must
-
revalidate, post
-
check
=
0
, pre
-
check
=
0
Pragma: no
-
cache
Content
-
Length:
2526
Connection: close
Content
-
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>
|
本文转自谢育政 51CTO博客,原文链接:http://blog.51cto.com/kurolz/1935054
,如需转载请自行联系原作者