• python DEMO

    python DEMO

    功能说明:该接口要求提前在云片后台添加模板,提交短信时,系统会自动匹配审核通过的模板,匹配成功任意一个模板即可发送。系统已提供的默认模板添加签名后可以直接使用。

    1. #!/usr/local/bin/python
    2. #-*-coding:utf-8-*-
    3. #author: jacky
    4. # Time: 15-12-15
    5. # Desc: 短信http接口的python代码调用示例
    6. # https://www.yunpian.com/api/demo.html
    7. # https访问,需要安装 openssl-devel库。apt-get install openssl-devel
    8. import httplib
    9. import urllib
    10. import json
    11. #服务地址
    12. sms_host = "sms.yunpian.com"
    13. voice_host = "voice.yunpian.com"
    14. #端口号
    15. port = 443
    16. #版本号
    17. version = "v2"
    18. #查账户信息的URI
    19. user_get_uri = "/" + version + "/user/get.json"
    20. #智能匹配模板短信接口的URI
    21. sms_send_uri = "/" + version + "/sms/single_send.json"
    22. #模板短信接口的URI
    23. sms_tpl_send_uri = "/" + version + "/sms/tpl_single_send.json"
    24. #语音短信接口的URI
    25. sms_voice_send_uri = "/" + version + "/voice/send.json"
    26. #语音验证码
    27. voiceCode = 1234
    28. def get_user_info(apikey):
    29. """
    30. 取账户信息
    31. """
    32. conn = httplib.HTTPSConnection(sms_host , port=port)
    33. headers = {
    34. "Content-type": "application/x-www-form-urlencoded",
    35. "Accept": "text/plain"
    36. }
    37. conn.request('POST',user_get_uri,urllib.urlencode( {'apikey' : apikey}))
    38. response = conn.getresponse()
    39. response_str = response.read()
    40. conn.close()
    41. return response_str
    42. def send_sms(apikey, text, mobile):
    43. """
    44. 通用接口发短信
    45. """
    46. params = urllib.urlencode({'apikey': apikey, 'text': text, 'mobile':mobile})
    47. headers = {
    48. "Content-type": "application/x-www-form-urlencoded",
    49. "Accept": "text/plain"
    50. }
    51. conn = httplib.HTTPSConnection(sms_host, port=port, timeout=30)
    52. conn.request("POST", sms_send_uri, params, headers)
    53. response = conn.getresponse()
    54. response_str = response.read()
    55. conn.close()
    56. return response_str
    57. def tpl_send_sms(apikey, tpl_id, tpl_value, mobile):
    58. """
    59. 模板接口发短信
    60. """
    61. params = urllib.urlencode({
    62. 'apikey': apikey,
    63. 'tpl_id': tpl_id,
    64. 'tpl_value': urllib.urlencode(tpl_value),
    65. 'mobile': mobile
    66. })
    67. headers = {
    68. "Content-type": "application/x-www-form-urlencoded",
    69. "Accept": "text/plain"
    70. }
    71. conn = httplib.HTTPSConnection(sms_host, port=port, timeout=30)
    72. conn.request("POST", sms_tpl_send_uri, params, headers)
    73. response = conn.getresponse()
    74. response_str = response.read()
    75. conn.close()
    76. return response_str
    77. def send_voice_sms(apikey, code, mobile):
    78. """
    79. 通用接口发短信
    80. """
    81. params = urllib.urlencode({'apikey': apikey, 'code': code, 'mobile':mobile})
    82. headers = {
    83. "Content-type": "application/x-www-form-urlencoded",
    84. "Accept": "text/plain"
    85. }
    86. conn = httplib.HTTPSConnection(voice_host, port=port, timeout=30)
    87. conn.request("POST", sms_voice_send_uri, params, headers)
    88. response = conn.getresponse()
    89. response_str = response.read()
    90. conn.close()
    91. return response_str
    92. if __name__ == '__main__':
    93. #修改为您的apikey.可在官网(http://www.yunpian.com)登录后获取
    94. apikey = "xxxxxxxxxxxxxxxx"
    95. #修改为您要发送的手机号码,多个号码用逗号隔开
    96. mobile = "xxxxxxxxxxxxxxxx"
    97. #修改为您要发送的短信内容
    98. text = "【云片网】您的验证码是1234"
    99. #查账户信息
    100. print(get_user_info(apikey))
    101. #调用智能匹配模板接口发短信
    102. print send_sms(apikey,text,mobile)
    103. #调用模板接口发短信
    104. tpl_id = 1 #对应的模板内容为:您的验证码是#code#【#company#】
    105. tpl_value = {'#code#':'1234','#company#':'云片网'}
    106. print tpl_send_sms(apikey, tpl_id, tpl_value, mobile)
    107. #调用模板接口发语音短信
    108. print send_voice_sms(apikey,voiceCode,mobile)