Demo_sms.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/python
  2. #coding:utf-8
  3. ''' usage : sys.argv[0] 'telnums' 'message' '''
  4. import sys
  5. import ConfigParser
  6. from suds.client import Client
  7. import logging
  8. import logging.config
  9. try:
  10. cf = ConfigParser.SafeConfigParser()
  11. cf.read('conf/Sms.conf')
  12. url = cf.get('Sms', 'url')
  13. operation = cf.get('Sms', 'operation')
  14. username = cf.get('Sms', 'username')
  15. password = cf.get('Sms', 'password')
  16. licence = cf.get('Sms', 'licence')
  17. msgTemp = '''<?xml version='1.0' encoding='GBK'?>
  18. <xml>
  19. <message>
  20. <OneRecord>
  21. <desttermid>__telNum__</desttermid>
  22. <username>__username__</username>
  23. <password><![CDATA[__password__]]></password>
  24. <licence><![CDATA[__licence__]]></licence>
  25. <msgcontent><![CDATA[__message__]]></msgcontent>
  26. <systeminfo><![CDATA[ISS]]></systeminfo>
  27. </OneRecord>
  28. </message>
  29. </xml>'''
  30. except Exception as e:
  31. print ' get error : ' + str(e)
  32. logging.config.fileConfig('logging.conf')
  33. logger = logging.getLogger(__name__)
  34. def sendSms(telNums,message):
  35. flg= True
  36. try:
  37. c = Client(url)
  38. Msg=msgTemp.replace('__username__',username).replace('__password__',password).replace('__licence__',licence).replace('__message__',message)
  39. telLists=telNums.split(',')
  40. for telNum in telLists:
  41. sendMsg=Msg.replace('__telNum__',telNum)
  42. logger.debug(telNum)
  43. logger.debug(sendMsg)
  44. results = c.service.SmsSendMQ(sendMsg)
  45. for result in results:
  46. if result=='0':
  47. logger.info('send message ok.')
  48. else:
  49. flg= False
  50. logger.error('send message failed !')
  51. except Exception as e:
  52. logger.error(' get error : ' ,exc_info=True )
  53. flg= False
  54. return flg
  55. if ( __name__ == "__main__"):
  56. if len(sys.argv) <3:
  57. print 'argument is ' + str(len(sys.argv)) + ' , less 3 , exp : ' + sys.argv[0] + ' telnums message '
  58. sys.exit(-1)
  59. telNums=sys.argv[1]
  60. try:
  61. message=sys.argv[2].decode('gbk')
  62. except Exception as e:
  63. message=sys.argv[2].decode('utf-8')
  64. sendSms(telNums,message)