import logging logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) import os import requests import time COUNT = 5 TIMEOUT = 1 INTERVAL = 0.2 HOST = '192.168.50.1' PING_COMMAND = 'ping -c {} -W {} -i {} {} > /dev/null 2>&1' LOGIN_URL = 'http://login.tulip.biz/portal/user-authen.php' LOGIN = dict(txtLogin='tb215', txtPasswd='363499') LOGIN_TIMEOUT = 20 while True: try: ret = os.system(PING_COMMAND.format(COUNT, TIMEOUT, INTERVAL, HOST)) if ret == 2: break elif ret == 256: requests.post(LOGIN_URL, data=LOGIN, timeout=LOGIN_TIMEOUT) logging.info('Attempted to login.') time.sleep(10) time.sleep(5) except KeyboardInterrupt: break except BaseException as e: logging.error(str(e)) time.sleep(10) logging.info('Exiting...')