-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendSMS.py
More file actions
83 lines (70 loc) · 2.78 KB
/
sendSMS.py
File metadata and controls
83 lines (70 loc) · 2.78 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
import httplib, urllib, hashlib, re, logging, getopt, sys, os
def usage():
print """%s, usage\n
-h/--help - display this help\n
<-r/--recepient> - recepient msisdn, could be string with comma separated values\n
<-s/--subject> - subject\n
<-b/--body> - text message\n
""" % os.path.abspath(__file__)
def checkNumber(msisdn):
match = re.match(r'^7[0-9]{3}[0-9]{7}$', msisdn, re.M)
if match:
return True
else:
return False
def parseNumber(to):
return to.split(",")
class SendSMS:
"""Class for sending email via http://smsaero.ru/api/"""
url = "gate.smsaero.ru"
port = 80
user = "user"
password = "password"
logging.basicConfig(filename='/var/log/sendSMS/sendSMS.log', filemode='a', format='%(asctime)s - %(levelname)s - %(message)s', level=logging.DEBUG)
def __init__(self, to, subject, body):
msisdns = parseNumber(to)
for msisdn in msisdns:
if checkNumber(msisdn):
md5sum = hashlib.md5(SendSMS.password)
params = urllib.urlencode({"user" : SendSMS.user, "password" : md5sum.hexdigest(), "to" : msisdn, "text" : body, "from" : "INFORM"})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = httplib.HTTPConnection(SendSMS.url, SendSMS.port)
conn.request("POST", "/send/", params, headers)
res = conn.getresponse()
data = res.read()
if int(res.status) == 200 and res.reason == "OK":
match = re.match(r'[0-9]+\s*=\s*accepted', data, re.M|re.I)
if match:
logging.info("SMS sent: " + data)
else:
logging.info("SMS not sent: " + data)
else:
logging.error("Server response: " + res.status + " and reason: " + res.reason)
conn.close()
else:
logging.error("Check msisdn is correct: " + msisdn)
def main(argv):
try:
opts, args = getopt.getopt(argv,"h:r:s:b:",["help","recepient=","subject=","body="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit(0)
elif opt in ("-r", "--recepient"):
recepient = arg
elif opt in ("-s", "--subject"):
subject = arg
elif opt in ("-b", "--body"):
body = arg
else:
assert False, "unhandled option"
if len(opts) < 3:
usage()
sys.exit(2)
SendSMS(recepient, subject, body)
if __name__=='__main__':
main(sys.argv[1:])