https://www.twilio.com/不多說,註冊
需要python3.5.5
本地window和centos服務器上測試都可以
註冊的時候會綁定你的手機(代碼裏用到的to),給你發驗證碼。試用賬號只能操作這個號碼。
創建一個 SMS項目獲得twilio一個用來給你發短信的手機號碼(代碼裏的from_)並獲得sid和token
pip install twilio #安裝依賴庫
from twilio.rest import Client
# Your Account Sid and Auth Token from twilio.com/console
account_sid = 'AC9a6e51be2xxxxxxxxxxxxx'#註冊,創建項目後獲得
auth_token = 'you auth token'#註冊,創建項目後獲得
client = Client(account_sid, auth_token)
call = client.messages.create(
to='+86133xxxxxx',#86是區號後面是你註冊時用的手機
from_='+16162xxxxxx',#創建項目時,給你的號碼
body='test info',#發送的短信信息
)
我用的時候還出現了ssl驗證的報錯,大概是再python2.7之後增加了這個東西。
Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)')))
網上查到說在全局請求的地方加入
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
反正我試了還是一樣的報錯,我就在python包
site-packages\twilio\http\http_client.py
修改
response = session.send(
prepped_request,
allow_redirects=allow_redirects,
timeout=timeout,
verify=False, #注意!!!就加了這行,去掉驗證
)
OK,萬事大吉!
最後,如果想不受號碼限制就需要付費,試用版有15美金的拿來學習是夠用了。