-
Notifications
You must be signed in to change notification settings - Fork 4
/
api_sms.py
executable file
·76 lines (64 loc) · 1.91 KB
/
api_sms.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
70
71
72
73
74
75
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: justinli.ljt@gmail.com
# date: 2013-11-06
import sys,os
import time
import traceback
import urllib
import urllib2
import json
import config
from lib.logger_service import logger
class SMSGateway(object):
'''
'''
def __init__(self):
self._gateway_url = config.SMS_GATEWAY_URL1
self._tel_list = config.SMS_TEL_LIST
self._send_interval = config.MIN_INTERVAL_SMS
self._last_send = 0
pass
def _wget(self, url):
ret = False
data = None
req = urllib2.Request(url)
try:
response = urllib2.urlopen(req, timeout=20)
data = response.read()
ret = True
except urllib2.HTTPError, e:
logger.error('HTTP Error: %d\t%s\t%s\t%s' % (e.code, e.reason, e.geturl(), e.read()))
except urllib2.URLError, e:
logger.error('URL Error: %s' % (e.reason))
return ret,data
def _check_interval(self):
now = int(time.time())
if now - self._last_send > self._send_interval:
self._last_send = now
return True
else:
return False
def send(self, msg):
ret = False
if not self._check_interval():
logger.info('sms send operation too frequently, cancel - "%s"' % msg)
return ret
url = self._gateway_url % (','.join(self._tel_list), msg)
ret, data = self._wget(url)
logger.info('request %s - "%s"' % (ret, data))
if ret:
json_data = json.loads(data)
if json_data['ret'] == '0':
logger.info('sms send ok - "%s"' % (msg))
else:
ret = False
logger.error('sms send fail - "%s"' % (msg))
return ret
pass
def main():
sms = SMSGateway()
print sms.send('test_by_main')
pass
if __name__ == '__main__':
main()