码迷,mamicode.com
首页 > 编程语言 > 详细

Python短信接口demo:创蓝253短信验证码接口、短信营销接口

时间:2018-02-08 20:19:38      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:one   账号   print   end   uri   plain   ram   amp   esc   

#!/usr/local/bin/python
#-- coding:utf-8 --

Author: jacky

Time: 14-2-22 下午11:48

Desc: 短信http接口的python代码调用示例**

import httplib
import urllib

#服务地址
host = "sms.253.com"

#端口号
port = 80

#版本号
version = "v1.1"

#查账户信息的URI
balance_get_uri = "/msg/balance"

#智能匹配模版短信接口的URI
sms_send_uri = "/msg/send"

#创蓝253账号
un = "xxxx"

#创蓝密码
pw = "xxxx"

def get_user_balance():
"""
取账户余额
"""
conn = httplib.HTTPConnection(host, port=port)
conn.request(‘GET‘, balance_get_uri + "?un=" + un + "&pw=" + pw)
response = conn.getresponse()
response_str = response.read()
conn.close()
return response_str

def send_sms(text, phone):
"""
能用接口发短信
"""
params = urllib.urlencode({‘un‘: un, ‘pw‘ : pw, ‘msg‘: text, ‘phone‘:phone, ‘rd‘ : ‘1‘})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = httplib.HTTPConnection(host, port=port, timeout=30)
conn.request("POST", sms_send_uri, params, headers)
response = conn.getresponse()
response_str = response.read()
conn.close()
return response_str

if name == ‘main‘:

phone = "188xxxxxxxx"
text = "【创蓝253云通讯】您的验证码是1234"

#查账户余额
print(get_user_balance())

#调用智能匹配模版接口发短信
print(send_sms(text, phone))

Python短信接口demo:创蓝253短信验证码接口、短信营销接口

标签:one   账号   print   end   uri   plain   ram   amp   esc   

原文地址:http://blog.51cto.com/13567544/2070331

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!