码迷,mamicode.com
首页 > 编程语言 > 详细

官网服务质量检测脚本(源码来自《Python自动化运维实战》第二版刘天斯)

时间:2016-04-19 00:15:47      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

脚本Python版本2.7

 1 #!/usr/bin/python
 2 #-*- coding:utf-8 -*-
 3 import os,sys
 4 import time
 5 import sys
 6 import pycurl
 7 URL="http://www.tianler.com"
 8 c = pycurl.Curl()
 9 c.setopt(pycurl.URL,URL)
10 c.setopt(pycurl.CONNECTTIMEOUT,5)
11 c.setopt(pycurl.TIMEOUT,5)
12 c.setopt(pycurl.NOPROGRESS,1)
13 c.setopt(pycurl.FORBID_REUSE,1)
14 c.setopt(pycurl.MAXREDIRS,1)
15 indexfile = open(os.path.dirname(os.path.realpath(__file__))+"/content.txt","wb")
16 c.setopt(pycurl.DNS_CACHE_TIMEOUT,30)
17 c.setopt(pycurl.WRITEHEADER,indexfile)
18 c.setopt(pycurl.WRITEDATA,indexfile)
19 c.setopt(pycurl.URL,URL)
20 c.setopt(pycurl.URL,URL)
21 try:
22     c.perform()
23     except Exception,e:
24     print"connection error:"+str(e)
25     indexfile.close()
26     c.close()
27     sys.exit()
28 NAMELOOKUP_TIME = c.getinfo(c.NAMELOOKUP_TIME)
29 CONNECT_TIME = c.getinfo(c.CONNECT_TIME)
30 PRETRANSFER_TIME = c.getinfo(c.PRETRANSFER_TIME)
31 STARTTRANSFER_TIME = c.getinfo(c.STARTTRANSFER_TIME)
32 TOTAL_TIME = c.getinfo(c.TOTAL_TIME)
33 HTTP_CODE = c.getinfo(c.HTTP_CODE)
34 SIZE_DOWNLOAD = c.getinfo(c.SIZE_DOWNLOAD)
35 HEADER_SIZE = c.getinfo(c.HEADER_SIZE)
36 SPEED_DOWNLOAD = c.getinfo(c.SPEED_DOWNLOAD)
37 print "HTTP状态码:%s"%(HTTP_CODE)
38 print "DNS解析时间:%.2f ms"%(NAMELOOKUP_TIME*1000)
39 print "建立连接时间:%.2f ms"%(CONNECT_TIME*1000)
40 print "准备传输时间:%.2f ms"%(PRETRANSFER_TIME*1000)
41 print "传输开始时间:%.2f ms"%(STARTTRANSFER_TIME*1000)
42 print "传输结束总时间:%.2f ms"%(TOTAL_TIME*1000)
43 print "下载数据包大小:%d bytes/s"%(SIZE_DOWNLOAD)
44 print "HTTP头部大小:%d bytes"%(HEADER_SIZE)
45 print "平均下载速度:%d bytes/s"%(SPEED_DOWNLOAD)

 

官网服务质量检测脚本(源码来自《Python自动化运维实战》第二版刘天斯)

标签:

原文地址:http://www.cnblogs.com/carl-angela/p/5406264.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!