怎样经过Python完成批量检测HTTP服务的状况?
用Python完成批量测验一组url的可用性(能够包含HTTP状况、呼应时刻等)并计算呈现不可用状况的次数和频率等。
相似的,这样的脚本能够判别某个服务的可用性,以及在许多的服务供给者中挑选最优的。
需求以及脚本完成的功用如下:
- 默许状况下,履行脚本会检测一组url的可用性。
- 假如可用,回来从脚本地点的机器到HTTP服务器所耗费的时刻和内容等信息。
- 假如url不可用,则记载并提示用户,并显现不可用发作的时刻。
- 默许状况下,答应最大的过错次数是200,数目能够自定义,假如到达答应的最大过错次数,则在输出信息的最终,依据每一个url做出过错计算。
- 假如用户手动中止脚本,则需要在输出信息的最终,依据每一个url做出过错计算。
脚本中触及的一些技巧:
- 运用gevent并发处理多个HTTP恳求,多个恳求之间无须等候呼应(gevent还有许多运用技巧,可再自行学习);
- 运用signal模块捕获信号,假如捕获到则处理并退出,防止主进程接收到KeyboardInterrupt直接退出但无法处理的问题;
- 留心留心脚本中关于计算次数方面的小技巧;
脚本运转效果图( 假如图片看不清楚,请挑选“在新标签页中翻开图片” )如下:
脚本如下:
#!/usr/bin/python # encoding: utf-8 # -*- coding: utf8 -*- """ Created by PyCharm. File: LinuxBashShellScriptForOps:testNoHttpResponseException,testHttpHostAvailability.py User: Guodong Create Date: 2016/10/26 Create Time: 12:09 Function: test Http Host Availability Some helpful message: For CentOS: yum -y install python-devel python-pip; pip install gevent For Ubuntu: apt-get -y install python-dev python-pip; pip install gevent For Windows: pip install gevent """ import signal import time import sys # execute some operations concurrently using python from gevent import monkey monkey.patch_all() import gevent import urllib2 hosts = ['https://webpush.wx2.qq.com/cgi-bin/mmwebwx-bin/synccheck', 'https://webpush.wx.qq.com/cgi-bin/mmwebwx-bin/synccheck', ] errorStopCounts = 200 quit_flag = False statistics = dict() def changeQuit_flag(signum, frame): del signum, frame global quit_flag quit_flag = True print "Canceled task on their own by the user." def testNoHttpResponseException(url): tryFlag = True global quit_flag errorCounts = 0 tryCounts = 0 global statistics globalStartTime = time.time() while tryFlag: if not quit_flag: tryCounts += 1 print('GET: %s' % url) try: startTime = time.time() resp = urllib2.urlopen(url) # using module 'request' will be better, request will return header info.. endTime = time.time() data = resp.read() responseTime = endTime - startTime print '%d bytes received from %s. response time is: %s' % (len(data), url, responseTime) print "data received from %s at %d try is: %s" % (url, tryCounts, data) gevent.sleep(2) except urllib2.HTTPError as e: errorCounts += 1 statistics[url] = errorCounts currentTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) print "HTTPError occurred, %s, and this is %d times(total) occurs on %s at %s." % ( e, statistics[url], url, currentTime) if errorCounts >= errorStopCounts: globalEndTime = time.time() tryFlag = False else: globalEndTime = time.time() break for url in statistics: print "Total error counts is %d on %s" % (statistics[url], url) hosts.remove(url) for url in hosts: print "Total error counts is 0 on %s" % url globalUsedTime = globalEndTime - globalStartTime print "Total time use is %s" % globalUsedTime sys.exit(0) try: # Even if the user cancelled the task, # it also can statistics the number of errors and the consumption of time for each host. signal.signal(signal.SIGINT, changeQuit_flag) gevent.joinall([gevent.spawn(testNoHttpResponseException, host) for host in hosts]) except KeyboardInterrupt: # Note: this line can NOT be reached, because signal has been captured! print "Canceled task on their own by the user." sys.exit(0)
本文地址:http://www.cvmachine.com/a/question/83962.html