python 發送短信
阿新 • • 發佈:2019-05-06
get name 觸發 用戶名 ini none stat pri pconnect
#接口類型:互億無線觸發短信接口,支持發送驗證碼短信、訂單通知短信等。 #賬戶註冊:請通過該地址開通賬戶http://user.ihuyi.com/register.html #註意事項: #(1)調試期間,請使用用系統默認的短信內容:您的驗證碼是:【變量】。請不要把驗證碼泄露給其他人。 #(2)請使用 APIID 及 APIKEY來調用接口,可在會員中心獲取; #(3)該代碼僅供接入互億無線短信接口參考使用,客戶可根據實際需要自行編寫; #!/usr/local/bin/python #-*- coding:utf-8 -*- import httplib import urllib host= "106.ihuyi.com" sms_send_uri = "/webservice/sms.php?method=Submit" #查看用戶名 登錄用戶中心->驗證碼通知短信>產品總覽->API接口信息->APIID account = "用戶名" #查看密碼 登錄用戶中心->驗證碼通知短信>產品總覽->API接口信息->APIKEY password = "密碼" def send_sms(text, mobile): params = urllib.urlencode({‘account‘: account, ‘password‘ : password, ‘content‘: text, ‘mobile‘:mobile,‘format‘:‘json‘ }) headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} conn = httplib.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 = "138xxxxxxxx" text = "您的驗證碼是:121254。請不要把驗證碼泄露給其他人。" print(send_sms(text, mobile))
2, 騰訊群發消息
#! /usr/bin/env python2 # encoding:utf-8 # python 2.7 測試通過 # python 3 更換適當的開發庫就能使用,在此我們不額外提供 import httplib import json import hashlib import random #定義發送短信的類 class SmsSender: sdkappid = 0 appkey = "" # API發送短信的文檔 url = "https://yun.tim.qq.com/v3/tlssmssvr/sendsms" #構造函數,把appid和appkey傳入 def __init__(self, sdkappid, appkey): self.sdkappid = sdkappid self.appkey = appkey #發短信的函數,傳入國家碼,手機號和內容 def sendMsg(self, nationCode, phoneNumber, content): #接口定義的appkey+phoneNumber的md5()變量 sig = hashlib.md5(self.appkey+phoneNumber).hexdigest() pkg = { "tel": { "nationcode": nationCode, "phone": phoneNumber }, "type": "0", "msg": content, "sig": sig, "extend": "", "ext":"123" } con = None try: con = httplib.HTTPSConnection(‘yun.tim.qq.com‘, timeout=10) body = json.dumps(pkg) rnd = random.randint(100000, 999999) wholeUrl = ‘%s?sdkappid=%d&random=%d‘ % (self.url, self.sdkappid, rnd) con.request(‘POST‘, wholeUrl, body) response = con.getresponse() print response.status,response.reason data = response.read() print data except Exception,e: print e finally: if(con): con.close() if __name__ == "__main__": # 開放者實際發送短信時請使用申請的 sdkappid 和 appkey #定義3個變量 wxName=‘test1‘ wxPhone=‘test2‘ wxComment=‘test3‘ #創建對象 sender = SmsSender(你的appid, "你的appkey") #要發送的手機號碼 phones = ["13833338888","13888883333"] for phone in phones: sender.sendMsg("86",phone,"昵稱:"+wxName+",電話:"+wxPhone+",評論:"+wxComment)
python 發送短信