How to use Python to call Chuanglan 253 SMS verification code

#!/usr/local/bin/python
#-*- coding:utf-8 -*-
# Author: jacky
# Time: 14-2-22 11:48 pm
# Desc: Example of calling python code of SMS http interface
import httplib
import urllib #Service

address
host = "sms.253.com" #Port

number
port = 80 #Version

number
version = "v1.1" #URI

for checking account information
balance_get_uri = "/msg/balance" #Smart

matching template SMS URI of the interface
sms_send_uri = "/msg/send" #Chuanglan

253 account
un = "xxxx" #Chuanglan

password
pw = "xxxx"

def get_user_balance():
    """ Get
    account 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 = "[ Chuanglan 253 Cloud Communication ] Your verification code is 1234" #Check

    account balance
    print(get_user_balance()) #Call

    the smart matching template interface to send text messages
    print(send_sms(text, phone))

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324900161&siteId=291194637