python對接ihuyi實現(xiàn)短信驗證碼發(fā)送
在日常生活中我們經(jīng)常會遇到接收短信驗證碼的場景,Python也提供了簡便的方法實現(xiàn)這個功能,下面就用代碼來實現(xiàn)這個功能。
一般我們需要租借短信供應(yīng)商的服務(wù)器發(fā)送短信。如果是用于自學會有一定免費條數(shù)的限額。
我們就借用互憶的平臺來是實現(xiàn)代碼。
首先需要訪問http://www.ihuyi.com/sms.html注冊私人賬號,注冊完之后進入個人信息界面會看到自己的賬號和密鑰。
所需導入的包:
import requests,random,bs4
requests模塊用于發(fā)送請求,random模塊用于產(chǎn)生驗證碼,bs4模塊用于解析服務(wù)器響應(yīng)信息。如果沒有安裝這些包,打開cmd,輸入pip install 包名 進行安裝。
一般手機驗證碼都是隨機四位數(shù),所以我們用一個函數(shù)來實現(xiàn),主要用random函數(shù)產(chǎn)生4位隨機數(shù)并返回。
def create_verify_code(): '''隨機產(chǎn)生一個4位數(shù)驗證碼''' verify_code = ’’ for i in range(4): verify_code += str(random.randint(0,9)) return verify_code
接著就要利用供應(yīng)商的API接口來發(fā)送短信,API文檔在互憶官網(wǎng)上就能下載到或者到自己賬戶中就能找到。
headers用于構(gòu)造請求頭,我們只需傳入手機號和要發(fā)送的文本,然后利用requests發(fā)送post請求給服務(wù)器,就會收到返回信息。
def sendmessagecode(phonenum,content): '''發(fā)送短信驗證碼''' headers = {'Content-type': 'application/x-www-form-urlencoded', 'Accept': 'text/plain'} data = {’account’:account,’password’:password,’mobile’:phonenum,’content’:content} return requests.post(host,data=data,headers=headers)
在收到服務(wù)器返回信息后,我們就可以解析信息,來判斷服務(wù)器是否發(fā)送成功。
response = sendmessagecode(phoneNum,content) # 用response來接收響應(yīng)信息
判斷是否與服務(wù)器聯(lián)通,若鏈接成功再進行下一步,否則打印失敗信息。
if 200 == response.status_code: TODO... else: print(’與服務(wù)器連接失敗:’,response.status_code)
若響應(yīng)成功,就利用BeautifulSoup來解析響應(yīng)信息。
soup = bs4.BeautifulSoup(response.text,features=’lxml’) # 構(gòu)造soup對象code = soup.find(’code’).string msg = soup.find(’msg’).stringif 2 == code: # 若服務(wù)器響應(yīng)碼為2,說明短信發(fā)送成功 print(’code: %s msg: %s ’ %(code,msg))else: print(’code: %s msg: %s ’ %(code,msg))
全文代碼:
#! python3# 測試發(fā)送短信,所用服務(wù)器為互億測試賬號import requests,random,bs4 host = ’http://106.ihuyi.com/webservice/sms.php?method=Submit’account = ’C27187646’password = ’64713042f161ae0555e9617afef40610’ def sendmessagecode(phonenum,content): '''發(fā)送短信驗證碼''' headers = {'Content-type': 'application/x-www-form-urlencoded', 'Accept': 'text/plain'} data = {’account’:account,’password’:password,’mobile’:phonenum,’content’:content} return requests.post(host,data=data,headers=headers) def create_verify_code(): '''隨機產(chǎn)生一個4位數(shù)驗證碼''' verify_code = ’’ for i in range(4): verify_code += str(random.randint(0,9)) return verify_code if __name__ == ’__main__’: phoneNum = ’159XXXXXXXX’ code = create_verify_code() content = ’您的驗證碼是:%s。請不要把驗證碼泄露給其他人。’ %code response = sendmessagecode(phoneNum,content) print(’短信內(nèi)容:’,content) if 200 == response.status_code: soup = bs4.BeautifulSoup(response.text,features=’lxml’) code = soup.find(’code’).string msg = soup.find(’msg’).string if 2 == code: print(’code: %s msg: %s ’ %(code,msg)) else: print(’code: %s msg: %s ’ %(code,msg)) else: print(’與服務(wù)器連接失敗:’,response.status_code)
以上就是python對接ihuyi實現(xiàn)短信驗證碼發(fā)送的詳細內(nèi)容,更多關(guān)于python短信驗證碼發(fā)送實例的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 詳解JSP 內(nèi)置對象request常見用法2. NetCore 配置Swagger的詳細代碼3. ASP.NET MVC增加一條記錄同時添加N條集合屬性所對應(yīng)的個體4. .NET Framework各版本(.NET2.0 3.0 3.5 4.0)區(qū)別5. 解決request.getParameter取值后的if判斷為NULL的問題6. JSP中param動作的實例詳解7. ASP.NET MVC實現(xiàn)下拉框多選8. .Net反向代理組件Yarp用法詳解9. .NET中的MassTransit分布式應(yīng)用框架詳解10. ASP.NET MVC實現(xiàn)本地化和全球化
