How to do it...

Here we will look into a simple recipe that performs this action, as indicated by listing 14.1:

#!/usr/bin/env python 
# Python Network Programming Cookbook, Second Edition
-- Chapter - 14 # This program is optimized for Python 2.7.12 and
Python 3.5.2. # It may run on any other version with/without modifications. import socket from sys import stdout from time import sleep import argparse def is_alive(address, port): # Create a socket object to connect with s = socket.socket() # Now try connecting, passing in a tuple with
address & port try: s.connect((address, port)) return True except socket.error: return False finally: s.close() def confirm(addres, port): while True: if is_alive(address, port): stdout.write(address + ":" +
str(port) + ' is alive ') stdout.flush() else: stdout.write(address + ":" +
str(port) + ' is dead ') stdout.flush() sleep(10) if __name__ == '__main__': # setup commandline arguments parser = argparse.ArgumentParser
(description='Health Checker') parser.add_argument('--address', action="store",
dest="address") parser.add_argument('--port', action="store",
dest="port", default=80, type=int) # parse arguments given_args = parser.parse_args() address, port = given_args.address,
given_args.port confirm(address, port)

The following is the sample output of the program:

$ python 14_1_healthcheck.py --address=google.com --port=80
google.com:80 is alive
google.com:80 is alive
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.88.62