Python学习笔记-实现探测Web服务质量

来源:互联网 发布:法语网络教学视频 编辑:程序博客网 时间:2024/06/05 03:51

    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  

报错:

1
setuptools.sandbox.UnpickleableExceptionConfigurationError("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)        #HTTP1
c.setopt(pycurl.DNS_CACHE_TIMEOUT,30)       #DNS30
#"web"http
indexfile = open(os.path.dirname(os.path.realpath(__file__))+"/content.txt","wb")
c.setopt(pycurl.WRITEHEADERindexfile)     #HTTP HEADERindexfile
c.setopt(pycurl.WRITEDATAindexfile)       #HTMLindexfile
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
HTTP200
DNS17.44 ms
17.88 ms
17.89 ms
39.79 ms
39.88 ms
2526 bytes/s
HTTP389 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
HTTP/1.1 200 OK
DateFri09 Jun 2017 03:01:46 GMT
ServerApache/2.2.15 (CentOS)
X-Powered-ByPHP/5.3.3
Set-CookiePHPSESSID=qmhmq2hkbb3v5hs67rf38c5006path=/
ExpiresThu19 Nov 1981 08:52:00 GMT
Cache-Controlno-storeno-cachemust-revalidatepost-check=0pre-check=0
Pragmano-cache
Content-Length2526
Connectionclose
Content-Typetext/htmlcharset=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>