send_sms.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import urllib2
  4. import json
  5. import os
  6. import sys
  7. import time
  8. reload(sys)
  9. sys.setdefaultencoding('utf8')
  10. #准备日志文件
  11. if not os.path.exists("/opt/matrix/agent/bin/alert_cmd"):
  12. os.makedirs("/opt/matrix/agent/bin/alert_cmd")
  13. f = open("/opt/matrix/agent/bin/alert_cmd/send_sms.log", "a+")
  14. #日志输出
  15. def log(s):
  16. dt=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
  17. print(dt+" "+s+"\n")
  18. f.write(dt+" "+s+"\n")
  19. try:
  20. #
  21. date_time=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
  22. tmp_date_time=time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
  23. tmp_date=time.strftime('%Y-%m-%d',time.localtime(time.time()))
  24. tmp_time=time.strftime('%H:%M:%S',time.localtime(time.time()))
  25. #接收参数
  26. log("=============================================================================")
  27. data1 = sys.argv[1]
  28. log("data1=" + data1)
  29. data2 = sys.argv[2]
  30. log("data2=" + data2)
  31. #参数解析
  32. #hjson1= json.loads(data1.decode('utf-8'))
  33. hjson1= json.loads(data1)
  34. ipaddress=hjson1['netgate']['address']
  35. port=hjson1['netgate']['port']
  36. protocol=hjson1['netgate']['protocol']
  37. brNo=hjson1['netgate']['brNo']
  38. chanNo=hjson1['netgate']['chanNo']
  39. srvId=hjson1['netgate']['srvId']
  40. svcId=hjson1['netgate']['svcId']
  41. #log(hjson1['jdbc']['driver'])
  42. #hjson2= json.loads(data2.decode('utf-8'))
  43. hjson2= json.loads(data2)
  44. #f.write(date_time+" "+hjson1+" "+hjson2+"\n")
  45. #处理
  46. for msg in hjson2:
  47. message=msg['msg']
  48. for names in msg['to']:
  49. touser=names['mobile']
  50. #url=protocol+'://'+ipaddress+':'+port+'/'
  51. url=protocol+'://'+str(ipaddress)+':'+str(port)+'/'
  52. body='<?xml version="1.0" encoding="UTF-8"?>' \
  53. '<reqt>' \
  54. '<appHdr>' \
  55. '<reqDt>'+tmp_date+'</reqDt>' \
  56. '<reqTm>'+tmp_time+'</reqTm>' \
  57. '<svcId>'+svcId+'</svcId>' \
  58. '</appHdr>' \
  59. '<appBody>' \
  60. '<srvId>'+srvId+'</srvId>' \
  61. '<chanNo>'+chanNo+'</chanNo>' \
  62. '<brNo>'+brNo+'</brNo>' \
  63. '<objAddr>'+touser+'</objAddr>' \
  64. '<msgCont>'+message+'</msgCont>' \
  65. '<ordTm>'+tmp_date_time+'</ordTm>' \
  66. '<accNo>null</accNo>' \
  67. '<custNo>null</custNo>' \
  68. '</appBody>' \
  69. '</reqt>'
  70. #data = urllib.urlencode(body)
  71. log("发送请求:"+date_time+" "+url+" "+body)
  72. response = urllib2.urlopen(urllib2.Request(url, body), timeout=3).read()
  73. log("请求返回:"+date_time+" "+response)
  74. #r = requests.post(url,data=body.decode("gbk").encode("utf-8"))
  75. #r = requests.post(url,data=body.encode("utf-8"))
  76. #f.write(date_time+" "+url+" "+body+"\n")
  77. #f.write(date_time+" "+touser+" "+message+r.text+"\n")
  78. #f.write(date_time+" "+touser+" "+message+"\n")
  79. #print(hjson1)
  80. #print(hjson2)
  81. #send wechart
  82. except Exception as e:
  83. log("{}".format(e))
  84. #关闭日志文件
  85. f.close()