欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)批量檢測(cè)HTTP服務(wù)的狀態(tài)

 更新時(shí)間:2016年10月27日 08:42:34   作者:dgd2010  
本文給大家分享的是一個(gè)使用python實(shí)現(xiàn)的批量檢測(cè)web服務(wù)可用性的腳本代碼,主要功能有測(cè)試一組url的可用性(可以包括HTTP狀態(tài)、響應(yīng)時(shí)間等)并統(tǒng)計(jì)出現(xiàn)不可用情況的次數(shù)和頻率等。

用Python實(shí)現(xiàn)批量測(cè)試一組url的可用性(可以包括HTTP狀態(tài)、響應(yīng)時(shí)間等)并統(tǒng)計(jì)出現(xiàn)不可用情況的次數(shù)和頻率等。

類(lèi)似的,這樣的腳本可以判斷某個(gè)服務(wù)的可用性,以及在眾多的服務(wù)提供者中選擇最優(yōu)的。

需求以及腳本實(shí)現(xiàn)的功能如下:

  1. 默認(rèn)情況下,執(zhí)行腳本會(huì)檢測(cè)一組url的可用性。
  2. 如果可用,返回從腳本所在的機(jī)器到HTTP服務(wù)器所消耗的時(shí)間和內(nèi)容等信息。
  3. 如果url不可用,則記錄并提示用戶(hù),并顯示不可用發(fā)生的時(shí)間。
  4. 默認(rèn)情況下,允許最大的錯(cuò)誤次數(shù)是200,數(shù)目可以自定義,如果達(dá)到允許的最大錯(cuò)誤次數(shù),則在輸出信息的最后,根據(jù)每一個(gè)url做出錯(cuò)誤統(tǒng)計(jì)。
  5. 如果用戶(hù)手動(dòng)停止腳本,則需要在輸出信息的最后,根據(jù)每一個(gè)url做出錯(cuò)誤統(tǒng)計(jì)。

腳本中涉及的一些技巧:

  1. 使用gevent并發(fā)處理多個(gè)HTTP請(qǐng)求,多個(gè)請(qǐng)求之間無(wú)須等待響應(yīng)(gevent還有很多使用技巧,可再自行學(xué)習(xí));
  2. 使用signal模塊捕獲信號(hào),如果捕獲到則處理并退出,避免主進(jìn)程接收到KeyboardInterrupt直接退出但無(wú)法處理的問(wèn)題;
  3. 注意留意腳本中關(guān)于統(tǒng)計(jì)次數(shù)方面的小技巧;

腳本運(yùn)行效果圖( 如果圖片看不清楚,請(qǐng)選擇“在新標(biāo)簽頁(yè)中打開(kāi)圖片” )如下:

腳本如下:

#!/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)

相關(guān)文章

  • python持久性管理pickle模塊詳細(xì)介紹

    python持久性管理pickle模塊詳細(xì)介紹

    這篇文章主要介紹了python持久性管理pickle模塊詳細(xì)介紹,本文講解了什么是持久性、一些經(jīng)過(guò) pickle 的 Python等內(nèi)容,并講給出了18個(gè)使用示例,需要的朋友可以參考下
    2015-02-02
  • 詳解python路徑拼接os.path.join()函數(shù)的用法

    詳解python路徑拼接os.path.join()函數(shù)的用法

    os.path.join()函數(shù):連接兩個(gè)或更多的路徑名組件。這篇文章主要介紹了python路徑拼接os.path.join()函數(shù)的用法,需要的朋友可以參考下
    2019-10-10
  • Python彈出輸入框并獲取輸入值的實(shí)例

    Python彈出輸入框并獲取輸入值的實(shí)例

    今天小編就為大家分享一篇Python彈出輸入框并獲取輸入值的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06
  • Python中的lstrip()方法使用簡(jiǎn)介

    Python中的lstrip()方法使用簡(jiǎn)介

    這篇文章主要介紹了Python中的lstrip()方法使用簡(jiǎn)介,是Python入門(mén)的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • Python循環(huán)結(jié)構(gòu)的應(yīng)用場(chǎng)景詳解

    Python循環(huán)結(jié)構(gòu)的應(yīng)用場(chǎng)景詳解

    這篇文章主要介紹了Python循環(huán)結(jié)構(gòu)的應(yīng)用場(chǎng)景詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • python中單下劃線與雙下劃線的區(qū)別及說(shuō)明

    python中單下劃線與雙下劃線的區(qū)別及說(shuō)明

    這篇文章主要介紹了python中單下劃線與雙下劃線的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Python使用execjs執(zhí)行包含中文參數(shù)的JavaScript

    Python使用execjs執(zhí)行包含中文參數(shù)的JavaScript

    爬蟲(chóng)的開(kāi)發(fā)過(guò)程中,往往需要對(duì)JS進(jìn)行模擬,簡(jiǎn)單或者通用的還可以在Python中模擬或者找到對(duì)應(yīng)的第三方庫(kù),但是復(fù)雜的就可能不好實(shí)現(xiàn)了,下面這篇文章主要給大家介紹了關(guān)于Python使用execjs執(zhí)行包含中文參數(shù)的JavaScript的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • python 將Excel轉(zhuǎn)Word的示例

    python 將Excel轉(zhuǎn)Word的示例

    這篇文章主要介紹了python 將Excel轉(zhuǎn)Word的示例,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-03-03
  • Python書(shū)單 不將就

    Python書(shū)單 不將就

    對(duì)于學(xué)習(xí)Python語(yǔ)言,如何選擇合適的Python書(shū)單,是不是已經(jīng)眼花繚亂,不知道該選擇哪本好了呢?今天我來(lái)為大家分享幾本不可錯(cuò)過(guò)的Python好書(shū)
    2017-07-07
  • Python音頻處理庫(kù)pydub的使用教程詳解

    Python音頻處理庫(kù)pydub的使用教程詳解

    Pydub是Python音頻處理庫(kù),可以對(duì)音頻進(jìn)行切割、合并、轉(zhuǎn)換、調(diào)整音量等操作。本文將對(duì)pydub各個(gè)知識(shí)點(diǎn)和案例進(jìn)行介紹,需要的可以參考一下
    2023-03-03

最新評(píng)論