Python:傳送簡訊
阿新 • • 發佈:2019-02-19
# 介面型別:互億無線觸發簡訊介面,支援傳送驗證碼簡訊、訂單通知簡訊等。
# 賬戶註冊:請通過該地址開通賬戶http://sms.ihuyi.com/register.html
# 注意事項:
# (1)除錯期間,請用預設的模板進行測試,預設模板詳見介面文件;
# (2)請使用APIID(檢視APIID請登入使用者中心->驗證碼簡訊->產品總覽->APIID)及 APIkey來呼叫介面;
# (3)該程式碼僅供接入互億無線簡訊介面參考使用,客戶可根據實際需要自行編寫;
# !/usr/local/bin/python
# -*- coding:utf-8 -*-
import http.client
import urllib
# 將簡訊驗證碼封裝成類,驗證碼為隨機數
host = "106.ihuyi.com"
sms_send_uri = "/webservice/sms.php?method=Submit"
# 使用者名稱是登入使用者中心->驗證碼簡訊->產品總覽->APIID
account = "xxxxxxxx"
# 密碼 檢視密碼請登入使用者中心->驗證碼簡訊->產品總覽->APIKEY
password = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
def send_sms(text, mobile):
params = urllib.parse.urlencode(
{'account' : account, 'password': password, 'content': text, 'mobile': mobile, 'format': 'json'})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = http.client.HTTPConnection(host, port=80, 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__':
mobile = "12345678901" # 接收人的號碼
# 接收的簡訊內容
text = "您的驗證碼是:121254。請不要把驗證碼洩露給其他人。"
print(send_sms(text, mobile))