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

    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
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
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 >

 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Title: BSD Hack Author: Ajay Kumar Tiwari Length: 410 pages Edition: 1 Language: English Publication Date: 2015-03-22 ISBN-10: B00V3DWD80 In the world of Unix operating systems, the various BSDs come with a long heritage of high-quality software and well-designed solutions, making them a favorite OS of a wide range of users. Among budget-minded users who adopted BSD early on to developers of some of today's largest Internet sites, the popularity of BSD systems continues to grow. If you use the BSD operating system, then you know that the secret of its success is not just in its price tag: practical, reliable, extraordinarily stable and flexible, BSD also offers plenty of fertile ground for creative, time-saving tweaks and tricks, and yes, even the chance to have some fun."Fun?" you ask. Perhaps "fun" wasn't covered in the manual that taught you to install BSD and administer it effectively. But BSD Hacks, the latest in O'Reilly's popular Hacks series, offers a unique set of practical tips, tricks, tools--and even fun--for administrators and power users of BSD systems.BSD Hacks takes a creative approach to saving time and getting more done, with fewer resources. You'll take advantage of the tools and concepts that make the world's top Unix users more productive. Rather than spending hours with a dry technical document learning what switches go with a command, you'll learn concrete, practical uses for that command.The book begins with hacks to customize the user environment. You'll learn how to be more productive in the command line, timesaving tips for setting user-defaults, how to automate long commands, and save long sessions for later review. Other hacks in the book are grouped in the following areas: Customizing the User Environment Dealing with Files and Filesystems The Boot and Login Environments Backing Up Networking Hacks Securing the System Going Beyond the Basics Keeping Up-to-Date Grokking BSD If you want more than your average BSD user--you want to explore and experiment, unearth shortcuts, create useful tools, and come up with fun things to try on your own--BSD Hacks is a must-have. This book will turn regular users into power users and system administrators into super system administrators.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值