实战:python实现web服务质量检测

这是利用系统的命令测试
[root@www pycurl]# curl -I -s www.baidu.com |head -1|awk ‘{ health = $2==”200″?”server is ok”:”server is bad”}END{print health}’
server is ok
[root@www pycurl]#

[root@www pycurl]# vim web.py

!/usr/bin/env python

– coding: utf-8 –

import os,sys
import time
import sys
import pycurl

URL=”http://www.fuchao.com/

URL =raw_input(“please input you want The target url: “)
c = pycurl.Curl()
c.setopt(pycurl.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)
c.setopt(pycurl.DNS_CACHE_TIMEOUT,30)

indexfile = open(os.path.dirname(os.path.realpath(file))+”/content.txt”, “wb”)
c.setopt(pycurl.WRITEHEADER, indexfile)
c.setopt(pycurl.WRITEDATA, indexfile)
try:
c.perform()
except Exception,e:
print “connecion error:”+str(e)
indexfile.close()
c.close()
sys.exit()

NAMELOOKUP_TIME = c.getinfo(c.NAMELOOKUP_TIME)
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)
SIZE_DOWNLOAD = c.getinfo(c.SIZE_DOWNLOAD)
HEADER_SIZE = c.getinfo(c.HEADER_SIZE)
SPEED_DOWNLOAD=c.getinfo(c.SPEED_DOWNLOAD)

print “HTTP Status code:%s” %(HTTP_CODE)
print “DNS Parsing time:%.2f ms”%(NAMELOOKUP_TIME1000)
print “Establish a connection time:%.2f ms” %(CONNECT_TIME
1000)
print “Ready to transfer time:%.2f ms” %(PRETRANSFER_TIME1000)
print “Transfer starting time:%.2f ms” %(STARTTRANSFER_TIME
1000)
print “Total transmission over time:%.2f ms” %(TOTAL_TIME*1000)
print “Download the packet size:%d bytes/s” %(SIZE_DOWNLOAD)
print “HTTP Head size:%d byte” %(HEADER_SIZE)
print “The average download speed:%d bytes/s” %(SPEED_DOWNLOAD)
indexfile.close()
c.close()
[root@www pycurl]#

    原文作者:付炜超
    原文地址: https://www.jianshu.com/p/6876444ce0be
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞