Python implements the bulk detection HTTP service status _python

Source: Internet
Author: User

Use Python to test the availability of a set of URLs (which can include HTTP status, response time, and so on) and to count the number and frequency of unavailable occurrences.

Similarly, such a script can determine the availability of a service and choose the best among the many service providers.

Requirements and the capabilities of the script implementation are as follows:

    1. By default, the execution script detects the availability of a set of URLs.
    2. If available, returns information such as the time and content consumed by the machine from which the script resides to the HTTP server.
    3. If the URL is not available, the user is logged and prompted, and the time that the occurrence is unavailable is displayed.
    4. By default, the maximum number of errors allowed is 200, the number can be customized, and if the maximum number of allowable errors is reached, error statistics are made based on each URL at the end of the output information.
    5. If the user stops the script manually, you will need to make an error statistic at the end of the output information based on each URL.

Some of the tricks involved in the script:

    1. Using gevent Concurrent processing of multiple HTTP requests, there is no need to wait for the response between multiple requests (gevent There are many use techniques, can be self-learning);
    2. Using the signal module to capture the signal, if the capture is handled and exited, to avoid the main process to receive keyboardinterrupt directly exit but cannot handle the problem;
    3. Pay attention to the script in the statistics on the number of small tips;

Script Run Effect chart (if the picture doesn't look clear, select "Open Picture on new tab") as follows:

The script is as follows:

#!/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 instal L 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 co ncurrently using python from gevent import Monkey Monkey.patch_all () Import gevent import URLLIB2 hosts = [' Https://webp

Ush.wx2.qq.com/cgi-bin/mmwebwx-bin/synccheck ', ' Https://webpush.wx.qq.com/cgi-bin/mmwebwx-bin/synccheck ',] errorstopcounts = Quit_flag = False statistics = dict () def changequit_flag (Signum, frame): Del Signum, Frame glo


Bal 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 Globalsta  Rttime = 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 ' would be better, request'll return header info.  Endtime = Time.time () data = Resp.read () responsetime = endtime-starttime print '%d bytes received from %s. Response time are:%s '% (len (data), URL, responsetime) print "Data received from%s in%d try is:%s"% (URL, try Counts, 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 are%d times (total) occurs in%s."% (E, statis Tics[url], URL, currenttime) if errorcounts >= errorstopcounts:globalendtime = TiMe.time () Tryflag = False Else:globalendtime = Time.time () break for URL in Statistics:print ' Total err  Or counts is%d on%s "% (Statistics[url], url) hosts.remove (URL) for URL in hosts:print ' total error counts ' 0 on


%s '% url globalusedtime = globalendtime-globalstarttime print ' Total ' time to%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 F
 or each host. Signal.signal (signal. SIGINT, Changequit_flag) Gevent.joinall ([Gevent.spawn (Testnohttpresponseexception, host) for host in hosts]) except Key
 Boardinterrupt: # Note:this Line can is reached, because signal has been
 Print "Canceled task on their own by the user."
 Sys.exit (0)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.