<!-- lang: python --> import argparse import socket import sys def check_server(ip, port): s = socket.socket() print "Attempt to connect to %s port is %s" % (ip, port) try: s.connect((ip, port)) s.send("GET / HTTP 1.0\n\n") print 'Connected to ip : %s, port is : %s' % (ip, port) return True except Exception, e: print 'Connect to ip %s ,port %s failed : %s' % (ip, port, e) finally: s.close() return False if __name__ == '__main__': parser = argparse.ArgumentParser(description='connect to ip:port') parser.add_argument('-a', '--adress', dest='adress', default='localhost', help='ADRESS for server', metavar='ADRESS') parser.add_argument('-p', '--port', dest='port', type=int, default=80, help='PORT for adress', metavar='PORT') args = parser.parse_args() print 'adress : %s, port : %s' % (args.adress, args.port) check_server = check_server(args.adress, args.port) print 'check_server return %s' % check_server sys.exit(not check_server)