#! /usr/bin/env python2 # encoding:utf-8 # python 2.7 import httplib import urllib class SendDemo: apiKey = "" apiSecret = "" host = "api.duanxin.com:7891" def __init__( self , apiKey, apiSecret): self .apiKey = apiKey self .apiSecret = apiSecret def getRequestUrl( self , mobile, message): params = urllib.urlencode({ "dc" : "8" , "un" : self .apiKey, "pw" : self .apiSecret, "da" :mobile , "sm" :message, "tf" : "3" , "rf" : "2" , "rd" : "0" }) return "/mt?%s" % (params) def sendMsg( self , mobile, message): sendUrl = self .getRequestUrl(mobile, message) #print sendUrl conn = None try : conn = httplib.HTTPConnection( self .host, timeout = 5 ) conn.request( "GET" , sendUrl) response = conn.getresponse() #print response.status, response.reason data = response.read() #打印返回结果 print data except Exception,e: print e finally : if (conn): conn.close() if __name__ = = "__main__" : #APIKey(接口账号) apiKey = "2288**0010" #APISecret(接口密钥) apiSecret = "ae2600e9456f5b0b3ab8" #接受短信的手机号 mobile = "15100000000" #短信内容(【签名】+短信内容),发送短信需要预先申请报备短信签名,控制台>接口短信>签名报备 message = "【短信网】您的验证码是:5288" sender = SendDemo(apiKey, apiSecret) sender.sendMsg(mobile, message) |
√ 短信接口 :
1)短信内容一定要带签名,签名放在短信内容的最前面;
2)签名格式:【***】,签名内容为三个汉字以上(包括三个);
3)短信内容不允许双签名,即短信内容里只有一个"【】";
4)短信签名先申请报备后使用,控制台>接口短信>签名报备;