python向手机发送短信
时间: 2023-10-22 17:10:36 浏览: 191
你可以使用以下代码实现Python向手机发送短信的功能:
```python
import requests
class SMS:
"""短信发送功能"""
def __init__(self, account, password):
"""account: APIID(用户中心【验证码通知短信】-【产品纵览】查看)
password:APIKEY(用户中心【验证码通知短信】-【产品纵览】查看)
self.url: 接口请求地址
"""
self.account = account
self.password = password
self.url = 'https://106.ihuyi.com/webservice/sms.php?method=Submit'
def send_sms(self, mobile, content):
""" 发短信
:param mobile: 手机号
:param content: 短信内容
:return: None
"""
headers = {
"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"
}
data = {
'account': self.account,
'password': self.password,
'mobile': mobile,
'content': content
}
# 发起请求
response = requests.post(self.url, headers=headers, data=data)
print(response.content.decode())
if __name__ == '__main__':
sms = SMS('C4xxxxx41', '46c4xaxxxxxxxxxxxxcd4')
sms.send_sms('157xxxx1995', '您的验证码是:8888。请不要把验证码泄露给其他人。')
```
阅读全文