Use Python to bulk check the availability of Web sites _python

Source: Internet
Author: User

Objective

With the increase in the site, management complexity has come up, as the saying goes: A lot of people do not take, I found that the site is not a lot of pipe, because these sites have important and unimportant, the important core of the site of course, more management, such as a number of years are not out of a problem, slowly by their own forgotten, the day of the sudden a problem , but also rush to emergency treatment, so the standard to manage these sites is very necessary, today we do the first step, regardless of the station, the first unified monitoring to do, do not say that the business situation, at least that site can not visit the first time to report out, do not wait for the business side to give you feedback, It looks like we're not professional enough, so let's take a look at the availability monitoring of multiple Web sites using Python, as follows:

#!/usr/bin/env python import pickle, OS, sys, logging from httplib import httpconnection, socket from smtplib import SMTP def email_alert (message, status): Fromaddr = ' xxx@163.com ' Toaddrs = ' xxxx@qq.com ' server = SMTP (' smtp.163.com  : () Server.starttls () server.login (' xxxxx ', ' xxxx ') server.sendmail (fromaddr, Toaddrs, ' Subject:%s\r\n%s '% (status,  Message) Server.quit () def get_site_status (URL): response = get_response (URL) try:if getattr (response, ' status ') = = 200:return ' up ' except attributeerror:pass return ' down ' def get_response (URL): Try:conn = Httpconnect Ion (URL) conn.request (' head ', '/") return Conn.getresponse () except Socket.error:return None except:logging.er Ror (' Bad URL: ', url) exit (1) def get_headers (URL): response = get_response (URL) try:return getattr (response, ' GE Theaders ') () except Attributeerror:return ' Headers unavailable ' Def compare_site_status (prev_results): Def Is_sta Tus_changed (URL): statUS = get_site_status (URL) friendly_status = '%s '%s '% (URL, status) print Friendly_status if URL in prev_results and Prev_results[url]!= status:logging.warning (status) Email_alert (str (get_headers (URL)), friendly_status) prev_ Results[url] = status return is_status_changed def is_internet_reachable (): If Get_site_status (' www.baidu.com ') = = ' Down ' and Get_site_status (' www.sohu.com ') = "Down": Return False return True def load_old_results (file_path): Pickl Edata = {} if Os.path.isfile (file_path): picklefile = open (File_path, ' rb ') Pickledata = Pickle.load (picklefile) pi Cklefile.close () return pickledata def store_results (File_path, data): output = open (File_path, ' WB ') pickle.dump (dat A, output) Output.close () def main (URLs): Logging.basicconfig (level=logging.
 
 WARNING, filename= ' Checksites.log ', format= '% (asctime) s% (levelname) s:% (message) s ', datefmt= '%y-%m-%d%h:%m:%s ') Pickle_file = ' data.pkl ' pickledata = Load_old_results (pickle_File) Print Pickledata if Is_internet_reachable (): Status_checker = Compare_site_status (pickledata) map (status_ch
  
 Ecker, URLs) else:logging.error (' Either the world ended or we are not connected to the net. ') Store_results (Pickle_file, pickledata) if __name__ = = ' __main__ ': Main (sys.argv[1:])

Script Core Point Explanation:

1, getattr() is Python's built-in function, receives an object, can return the object's value according to the object property.

2, the compare_site_status() function is returned is an internally defined function.

3, map() the need for 2 parameters, one is a function, one is a sequence, function is the sequence of each element to apply the function method.

Summarize

The above is the entire content of this article, the need for friends can refer to reference.

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.