-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
45 lines (36 loc) · 1.31 KB
/
utilities.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
#!/usr/bin/python
import os
import json
import time
import requests
# suppress dirty SSL warnings when using requests
try:
requests.packages.urllib3.disable_warnings()
except Exception:
pass
def timestamp():
"""Returns YYYY-MM-DD HH:MM:SS formatted UTC timestamp string"""
return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
def get_public_ip():
"""Fetch public IP of hosting server with the help of httpbin API"""
return json.loads(requests.get('http://httpbin.org/ip').text)['origin']
def send_email(subject="", html="", from_id="", recipients=[], debug=False):
"""Send an email with the given html body & subject line with the help of MAILGUN API"""
sandbox = os.environ.get('MAILGUN_SANDBOX', '')
request_url = 'https://api.mailgun.net/v3/{0}/messages'.format(sandbox)
request = requests.post(
request_url,
verify=False,
auth=('api', os.environ.get('MAILGUN_KEY', '')),
data={
'from': from_id,
'to': recipients,
'subject': subject,
'html': html,
},
)
if debug:
print 'Status: {0}'.format(request.status_code)
print 'Body: {0}'.format(request.text)
return True if int(request.status_code) == 200 else False
__all__ = ['timestamp', 'get_public_ip', 'send_email']