-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
69 lines (56 loc) · 2.16 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from email_validator import validate_email, EmailNotValidError
import csv
import os
import json
import time
#Creates a random number it will be used as an identifier.
def create_tracking_code():
return str(int(time.time()%99999))
# Define a function for
# for validating an Email
def check(email):
try:
# Validate.
valid = validate_email(email)
return valid
except EmailNotValidError as e:
# email is not valid, exception message is human-readable
print(str(e))
return False
#Ask and writes server options
def get_options(logging):
logging.info("Saving options.")
if os.path.isfile('options.json'):
logging.info("Initiating with a found options.json file.")
with open('options.json') as json_file:
options = json.load(json_file)
return options['notify_email'],options['notify_password'],options['notify_stmp_server']
else:
notify_stmp_server = input("Tell me your email server: ")
notify_email = input("Tell me your email: ")
notify_password = input("Tell me your email password: ")
options = {
'notify_email': notify_email,
'notify_password': notify_password,
'notify_stmp_server': notify_stmp_server
}
with open('options.json', 'w') as outfile:
json.dump(options, outfile)
logging.info("Options saved succesfully!")
return options['notify_email'],options['notify_password'],options['notify_stmp_server']
#Get local ip of the server.
def get_local_ip():
import socket
"""Try to determine the local IP address of the machine."""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Use Google Public DNS server to determine own IP
sock.connect(('8.8.8.8', 80))
return sock.getsockname()[0]
except socket.error:
try:
return socket.gethostbyname(socket.gethostname())
except socket.gaierror:
return '127.0.0.1'
finally:
sock.close()