0%

Python发送短信

在进行深度学习炼金时,经常需要花费很长一段时间等待结果,因此想变主动为被动,让程序在运行结束时将结果通过短信主动发送到我的手机上,省得我每次都要通过ssh连接服务器进行查看。

搜索了一下教程,找到两个心仪的解决方案:Twilio、腾讯云短信,基本套路是通过调用Python接口进行短信转发。Twilio提供500条免费短信,腾讯云短信则提供100条,不过腾讯云在1万条内的价格是5分钱一条,尚可接受。目前的解决方案是先用完Twilio的500条后再转战腾讯云。

  • Twilio

    1. 注册

      网址为:https://www.twilio.com,教程见:https://www.cnblogs.com/pythoncircle/p/11790463.html

    2. API调用模板(简单)

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      # Download the helper library from https://www.twilio.com/docs/python/install
      from twilio.rest import Client


      # Your Account Sid and Auth Token from twilio.com/console
      # DANGER! This is insecure. See http://twil.io/secure
      account_sid = 'your_acco_sid'
      auth_token = 'your_auth_token'
      client = Client(account_sid, auth_token)

      message = client.messages \
      .create(
      body="Join Earth's mightiest heroes. Like Kevin Bacon.",
      from_='+150XXXXXXXXX',
      to='+86XXXXXXXXXXX'
      )

      print(message.sid)

      需替换自己的account_sid,auth_token,获得的虚拟号码(from_),发送的号码(to),信息(body),运行前安装twilio:pip install twilio

    3. 实际上没有500条,因为原始赠送金额是15美元,获得虚拟号码及部署项目时会用掉1.056美元,不过可忽略不计,每条短信价格是0.28美元。

  • 腾讯云短信