Python implements a small program that detects the health of a Web server _python

Source: Internet
Author: User
Tags curl response code

To do a health check on a Web server, we generally use the Curl library (whether it is php,perl or shell), the approximate method is the same:

Copy Code code as follows:

Curl-i-S www.qq.com |head-1|awk ' {health = $2== ' 200 '? Server is OK ":" The server is bad "}end{print Health}"
Server is OK

Frankly, these methods are encapsulated in the Curl library, and there are some modules on HTTP, such as Perl Lwp,python Httplib,urllib or URLLIB2 can also be implemented, today go a little bit lower, Use the socket method for one detection:

#!/usr/bin/env python #coding =utf8 "" To check the server's monitoring status by using the socket method "" "" from Optparse import optionparser import socket Import sys import re from stringio Import Stringio class Check_server: "" "This class mainly uses the socket to establish a connection, sends an HTTP request, then according to the Returns the status code that determines the health status of the host "" "Def __init__ (self,address,port,resource): self.address = Address Self.port = Port sel F.resource = Resource def check (self): "" "This method is also the primary method of the class, including building request resources, parsing return results, and so on" "if not self.resource.st Artswith ('/'): Self.resource = '/' + self.resource request = ' Get%s http/1.1\r\nhost:%s\r\n\r\n '% (Self.resou rce,self.address) #建立一个socket连接 s = socket.socket () #设置连接超时时间 s.settimeout () print "Now starts on%s %s Port connection ...% (Self.address,self.port) try:s.connect ((self.address,self.port)) Print connection%s port%s Success "% (Self.address,self.port) s.send (Request) response = S.RECV (MB) except Socket.error,e:print "Port%s on connection%s failed because:%s" % (self.address,self.port,e) return False finally:print "close Connection" s.close () line = Stringio (re
      Sponse). ReadLine () Try: (http_version,status,messages) = Re.split (R ' \s+ ', line,2) except ValueError: Print "Split response code failed" Return False print "The status code returned is%s"% (status) if status in [', '] ', ' 302 ': print ' The server's monitoring condition is good "else:print" obediently, go online to see, the "if __name__ = = ' __main__ ':" "" Processing Parameters "" "Parser =optionpars ER () parser.add_option ("A", "--address", dest= "address", default= ' localhost ', help= "to check host addresses or host names") Parser.add_ Option (' P ', '--port ', dest= "Port", type=int,default=80,help= "to check the host's Ports") parser.add_option (' R ', '--resource ', dest= "Resource", default= "/", help= "resources to check, for example" (Options,args) = Parser.parse_args () #开始检测鸟 checks = Check_server (options.ad Dress,options.port,options.resource) Checks.check ()

Look at the effect.

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.