You can obtain the http status code by checking a web test page to determine the status of the web server. The source script is from the Internet and a small comment is made here for you to study the python-like shoes. #! /Usr/bin/env python # script name check_web_stat.pyimport socket # use import re # Regular Expression module import sys def check_webserver (address, port, resource) to establish a socket connection in tcp ): # create an http request string if not resource. startswith ('/'): # determine whether to start with '/' resource = '/' + resource request_string = "GET % s HTTP/1.1 \ r \ nHost: % s \ r \ n "% (resource, address) print 'HTTP request: 'print '| % s |' % request_string # create a TCP socket s = socket. sock Et () print "Attempting to connect to % s on port % s" % (address, port) try: s. connect (address, port) print "Connected to % s on port % s" % (address, port) s. send (request_string) # obtain the first 100 bytes rsp = s. recv (100) print 'Received 100 bytes of HTTP response' print '| % s |' % rsp accept t socket. error, e: print "Connection to % s on port % s failed: % s" % (address, port, e) return False finally: # Close the socket Connection Print "Closing the connection" s. close () lines = rsp. splitlines () # split the string of a paragraph into a list of print 'first line of HTTP response: % s' % lines [0] try: version, status, message = re. split (r '\ s +', lines [0], 2) print 'version: % s, Status: % s, Message: % s' % (Version, status, message) Failed t ValueError: print 'failed' to split status line'return False if status in ['20170101', '20160301']: print 'success-status was % S' % status return True else: print 'status was % s' % Status return Falseif _ name _ = '_ main __': from optparse import OptionParser # import optionparser command line tool module parser = OptionParser () # construct the optionparser object parser. add_option ("-a", "-- address", dest = "address", default = 'localhost', help = "ADDRESS for webserver", metavar = "ADDRESS") parser. add_option ("-p", "-- port", dest = "port", type = "int", default = 80, help = "PORT for webserver", metavar = "PORT") parser. add_option ("-r", "-- resource", dest = "resource", default='index.html ', help = "RESOURCE to check", metavar = "RESOURCE ") # Add option: parser to the optionparser object. add_option () (options, args) = parser. parse_args () # Call the optionparser parsing function, use the resolved options in options, and use other location parameters args print 'Options: % s, args: % s' % (options, args) check = check_webserver (options. address, o Ptions. port, options. resource) print 'check _ webserver returned % s' % check sys. exit (not check) # python check_web_stat.py-a www.baidu.com-r index. phpoptions: {'resource': 'index. php ', 'Port': 80, 'address': 'www .baidu.com'}, args: [] HTTP request: | GET/index. php HTTP/1.1 Host: www.baidu.com | Attempting to connect to www.baidu.com on port 80 Connected to www.baidu.com on port 80 Received 100 bytes Of HTTP response | HTTP/1.1 200 OKDate: Wed, 09 Jan 2013 01:19:06 GMTServer: BWS/1.0Content-Length: 9777Content | Closing the connectionFirst line of HTTP response: HTTP/1.1 200 OKVersion: HTTP/1.1, Status: 200, Message: OKSuccess-status was 200check_webserver returned True note: Meaning of the parameter in add_option (): short option string: is the first parameter, indicating the abbreviation of option, for example,-f; long option string: is the second parameter, indicating the full spelling of option, for example, -- file; the parameters following are all Name parameter. The name parameter is an optional parameter: www.2cto. comaction =: indicates the processing method of this option. The default value is store, indicating that the value of the option is stored in the members of the resolved options object. Action can also have other values: For bool values, use store_true to store true by default, use store_false to store false by default, and store_const to store the value set by const to this option, append indicates adding the option parameter to the list. In this case, this option is a list and may contain multiple values. count indicates adding one to counter and callback indicates calling the specified function. All action values are as follows: store + store_true + store_false + store_const + append + count + callbacktype =: indicates the type of the option value. The default value is string, which can be specified as string or int, choice, float and complex; dest =: indicates the member name of this option in the options object parsed by optionparser. The default value is long option string; default =: indicates the default value of option; metavar =: the default value of option displayed in help;