【python】实现短信通知

python实现短信通知
修改账号密码即可使用

# coding=utf-8
import urllib
import urllib.request
import hashlib


def md5(str):
    import hashlib
    m = hashlib.md5()
    m.update(str.encode("utf8"))
    return m.hexdigest()


def send_sms(phone_input):
    statusStr = {
        '0': '短信发送成功',
        '-1': '参数不全',
        '-2': '服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间',
        '30': '密码错误',
        '40': '账号不存在',
        '41': '余额不足',
        '42': '账户已过期',
        '43': 'IP地址限制',
        '50': '内容含有敏感词'
    }

    smsapi = "http://api.smsbao.com/"
    # 短信平台账号
    user = 'xxxx'
    # 短信平台密码
    password = md5('xxxx')
    # 要发送的短信内容
    content = '【速购网络】你好,票已经预定,请及时付款,如非本人操作请勿理会。'
    # 要发送短信的手机号码
    phone = phone_input

    data = urllib.parse.urlencode({'u': user, 'p': password, 'm': phone, 'c': content})
    send_url = smsapi + 'sms?' + data
    response = urllib.request.urlopen(send_url)
    the_page = response.read().decode('utf-8')
    print(statusStr[the_page])

send_sms(
    phone_input= input("请输入电话号码:")
)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python可以使用不同的库来实现消息通知功能,具体取决于您希望发送通知的方式。以下是几种实现方式: 1. 使用smtplib库发送电子邮件通知 smtplib是Python的内置库,可用于发送电子邮件。您可以将电子邮件配置为接收通知,然后使用以下代码发送电子邮件: ```python import smtplib sender_email = "your_email" receiver_email = "receiver_email" password = "your_email_password" message = """\ Subject: Hi there This is a test email.""" with smtplib.SMTP("smtp.gmail.com", 587) as server: server.starttls() server.login(sender_email, password) server.sendmail(sender_email, receiver_email, message) ``` 2. 使用Twilio API发送短信通知 Twilio是一个通信API提供商,可以使用它来发送短信通知。首先,创建一个Twilio账户并获取账户SID和身份验证令牌。然后,使用以下代码发送短信通知: ```python from twilio.rest import Client account_sid = "your_account_sid" auth_token = "your_auth_token" client = Client(account_sid, auth_token) message = client.messages.create( to="+15555555555", from_="+15555555556", body="This is a test message." ) print(message.sid) ``` 3. 使用pushbullet库发送移动设备通知 Pushbullet是一个可以将通知推送到移动设备的服务。您可以使用pushbullet库来发送通知。首先,创建一个Pushbullet账户并获取API密钥。然后,使用以下代码发送移动设备通知: ```python from pushbullet import Pushbullet api_key = "your_api_key" pb = Pushbullet(api_key) push = pb.push_note("Test Notification", "This is a test notification.") ``` 这些只是其中的几种方法。根据您的需求,您还可以使用其他库或服务来实现消息通知功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值