def test_ReadSetting(self): username = config_helper.read_setting('Session1', 'username', 'test_setting.ini') self.assertEqual('abc', username) password = config_helper.read_setting('Session1', 'password', 'test_setting.ini') self.assertEqual('abcpwd', password)
def send_email(subject, message): try: gmail_user = read_setting('Gmail', 'gmail_user') gmail_password = read_setting('Gmail', 'gmail_password') with smtplib.SMTP_SSL(host=read_setting('Gmail', 'host'), port=int(read_setting('Gmail', 'port'))) as server: server.ehlo() server.login(gmail_user, gmail_password) body = message from_address = gmail_user to_address = gmail_user email_text = create_email_text(body, gmail_user, gmail_user, subject) server.sendmail(from_addr=from_address, to_addrs=to_address, msg=email_text.as_string()) server.close() print('Email is sent!') except Exception as e: logging.error('Something went wrong...' + str(e))
from requests import get from send_email import send_email from config_helper import read_setting, write_setting import logging logging.basicConfig(filename='ipnotifier.log',level=logging.DEBUG) ip = get('https://api.ipify.org').text last_public_ip = read_setting('ip', 'public_ip') # get public ip if last_public_ip != ip: send_email('IPApp message', 'Hello Linh: this is your number today: ' + ip) write_setting('ip', 'public_ip', ip) else: logging.info('No email is sent') print('No email is sent')
def test_WriteSetting(self): config_helper.write_setting('Session2', 'username2', 'uname2', 'test_setting.ini') username = config_helper.read_setting('Session2', 'username2', 'test_setting.ini') self.assertEqual('uname2', username)
def test_ReadSetting_FileNotFound(self): username = config_helper.read_setting('Session1', 'username', 'not_found.ini') self.assertIsNone(username)
def test_ReadSetting_notFoundKey(self): username = config_helper.read_setting('Session1', 'notfound', 'test_setting.ini') self.assertIsNone(username)