Skip to content

Commit

Permalink
Merge pull request #5 from Andy-Wu12/input-sanitization
Browse files Browse the repository at this point in the history
Input sanitization
  • Loading branch information
Andy-Wu12 authored Oct 7, 2022
2 parents 42e4dd2 + 39a92d1 commit 50ef7ad
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ A Python script that helps check the online status of a list of user-provided we
## 2. Permanently
1. Additionally install the dotenv module with `pip install python-dotenv`
2. Modify the `config_template.json` file replacing the provided placeholders with your own data in the same format.
1. You can rename the file as you wish, just make sure to pass the correct name when calling the script.
1. The necessary configuration variables are:
1. "recipients": List of strings representing email addresses to send status updates to
2. "urls": List of strings representing website urls following same format as mentioned above
3. "emails_fail_only": Boolean representing whether to send emails only if a status check fails.
1. true indicates that you want the server to send an email when at least one status check fails.
2. This can be used with a lower "check_interval_secs" to prevent a flood of emails while also having the status check run constantly.
4. "check_interval_secs": Integer representing the number of seconds to delay another round of status checks.
2. You can rename the file as you wish, just make sure to pass the correct name when calling the script.
3. Create a .env file with your gmail address and [APP PASSWORD](https://support.google.com/accounts/answer/185833?hl=en)
1. If you want to use a different mailing client, you will have to modify `perm_server_status` with the
client's required configuration.
Expand Down
35 changes: 27 additions & 8 deletions perm_server_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
Config_Data = TypedDict('Config_Data', {'recipients': List[str], 'urls': List[str],
'emails_fail_only': bool, 'check_interval_secs': int})

ENV_EMAIL = 'USER_EMAIL'
ENV_PASS = 'APP_PASSWORD'
user_email = os.getenv(ENV_EMAIL)
# Most likely necessary if using gmail or any other highly secure mailing client
app_password = os.getenv(ENV_PASS)

if not user_email or not app_password:
print(f'Set up your .env file with an email and password set to {ENV_EMAIL} and {ENV_PASS}, respectively')
sys.exit()


def is_site_running(site_url: str):
"""Ping desired site
Expand Down Expand Up @@ -85,6 +95,11 @@ def run(config_data: Config_Data):
downtime = config_data.get('check_interval_secs')
emails_on_fail_only = config_data.get('emails_fail_only')

if None in [recipients, urls, downtime, emails_on_fail_only]:
print('A key is missing from your configuration!')
print('Please make sure you have all keys as shown in the configuration template.')
return None

while True:
url_statuses = []
for url in urls:
Expand All @@ -101,22 +116,26 @@ def run(config_data: Config_Data):


def parseConfigData(filepath: str):
with open(filepath) as f:
data = json.load(f)
try:
with open(filepath) as f:
data = json.load(f)
except (FileNotFoundError, json.JSONDecodeError) as e:
raise e

return data


if __name__ == "__main__":
user_email = os.getenv('USER_EMAIL')
# Most likely necessary if using gmail or any other highly secure mailing client
app_password = os.getenv('APP_PASSWORD')

# Read in command line argument for config file path
try:
config_path = sys.argv[1]
except IndexError:
sys.exit("A filepath must be provided for the location of your configuration file!")

config_json = parseConfigData(config_path)
run(config_json)
try:
config_json = parseConfigData(config_path)
run(config_json)
except json.JSONDecodeError:
print("Error in parsing JSON. Please check your file for syntax errors!")
except FileNotFoundError:
print("The filename you entered does not exist!")

0 comments on commit 50ef7ad

Please sign in to comment.