Python實現批量檢測HTTP服務的狀態(tài)
更新時間:2016年10月27日 08:42:34 作者:dgd2010
本文給大家分享的是一個使用python實現的批量檢測web服務可用性的腳本代碼,主要功能有測試一組url的可用性(可以包括HTTP狀態(tài)、響應時間等)并統(tǒng)計出現不可用情況的次數和頻率等。
用Python實現批量測試一組url的可用性(可以包括HTTP狀態(tài)、響應時間等)并統(tǒng)計出現不可用情況的次數和頻率等。
類似的,這樣的腳本可以判斷某個服務的可用性,以及在眾多的服務提供者中選擇最優(yōu)的。
需求以及腳本實現的功能如下:
- 默認情況下,執(zhí)行腳本會檢測一組url的可用性。
- 如果可用,返回從腳本所在的機器到HTTP服務器所消耗的時間和內容等信息。
- 如果url不可用,則記錄并提示用戶,并顯示不可用發(fā)生的時間。
- 默認情況下,允許最大的錯誤次數是200,數目可以自定義,如果達到允許的最大錯誤次數,則在輸出信息的最后,根據每一個url做出錯誤統(tǒng)計。
- 如果用戶手動停止腳本,則需要在輸出信息的最后,根據每一個url做出錯誤統(tǒng)計。
腳本中涉及的一些技巧:
- 使用gevent并發(fā)處理多個HTTP請求,多個請求之間無須等待響應(gevent還有很多使用技巧,可再自行學習);
- 使用signal模塊捕獲信號,如果捕獲到則處理并退出,避免主進程接收到KeyboardInterrupt直接退出但無法處理的問題;
- 注意留意腳本中關于統(tǒng)計次數方面的小技巧;
腳本運行效果圖( 如果圖片看不清楚,請選擇“在新標簽頁中打開圖片” )如下:
腳本如下:
#!/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)
相關文章
詳解python路徑拼接os.path.join()函數的用法
os.path.join()函數:連接兩個或更多的路徑名組件。這篇文章主要介紹了python路徑拼接os.path.join()函數的用法,需要的朋友可以參考下2019-10-10Python使用execjs執(zhí)行包含中文參數的JavaScript
爬蟲的開發(fā)過程中,往往需要對JS進行模擬,簡單或者通用的還可以在Python中模擬或者找到對應的第三方庫,但是復雜的就可能不好實現了,下面這篇文章主要給大家介紹了關于Python使用execjs執(zhí)行包含中文參數的JavaScript的相關資料,需要的朋友可以參考下2022-03-03