【Python】創藍253雲通訊平臺國際簡訊API介面DEMO

luck_yun發表於2018-06-29
#!/usr/local/bin/python
#-*- coding:utf-8 -*-
# Author: jacky
# Time: 14-2-22 下午11:48
# Desc: 簡訊http介面的python程式碼呼叫示例
import httplib
import urllib
import json
#服務地址  
host = "intapi.253.com"

#埠號
port = 80

#版本號
version = "v1.1"

#查賬戶資訊的URI
balance_get_uri = "/balance/json"

#智慧匹配模版簡訊介面的URI
sms_send_uri = "/send/json"

#創藍賬號
account  = ""

#創藍密碼
password = ""

def get_user_balance():
    """
    取賬戶餘額
    """
    params = {'account': account, 'password' : password}
    params=json.dumps(params)

    headers = {"Content-type": "application/json"}
    conn = httplib.HTTPConnection(host, port=port)
    conn.request('POST', balance_get_uri, params, headers)
    response = conn.getresponse()
    response_str = response.read()
    conn.close()
    return response_str

def send_sms(text, phone):
    """
    能用介面發簡訊
    """

    params = {'account': account, 'password' : password, 'msg': urllib.quote(text), 'mobile':phone, 'report' : 'false'}
    params=json.dumps(params)

    headers = {"Content-type": "application/json"}
    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__':
    #手機號碼,格式(區號+手機號碼),例如:8615800000000,其中86為中國的區號
    phone = "8615800000000"
    text = "【253雲通訊】您的驗證碼是1234"

    #查賬戶餘額
    print(get_user_balance())

    #呼叫智慧匹配模版介面發簡訊
    print(send_sms(text, phone))

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31510215/viewspace-2157046/,如需轉載,請註明出處,否則將追究法律責任。

相關文章