【问题标题】:Python publish picture attachment to pushoverPython发布图片附件到pushover
【发布时间】:2018-07-04 12:14:50
【问题描述】:

我正在使用下面的代码成功地向 pushover 发送消息,但是我想发送最新 API 中介绍的图片附件。我如何能够将最后一个代码示例转换为在 python 中工作。

工作代码:

import httplib, urllib
conn = httplib.HTTPSConnection("api.pushover.net:443")
conn.request("POST", "/1/messages.json",
  urllib.urlencode({
    "token": "APP_TOKEN",
    "user": "USER_KEY",
    "message": "hello world",
  }), { "Content-type": "application/x-www-form-urlencoded" })
conn.getresponse()

示例 API

curl -s \
 --form-string "token=APP_TOKEN" \
 --form-string "user=USER_KEY" \
 --form-string "message=here is an image attachment" \
 -F "attachment=@/path/to/your/image.jpg" \
 https://api.pushover.net/1/messages.json

【问题讨论】:

    标签: python attachment pushover


    【解决方案1】:

    我没有尝试过 urllib,但请求成功。我真的不知道你是否想使用那个模块。但无论如何,这里是代码。

    import requests
    
    r = requests.post("https://api.pushover.net/1/messages.json", data={"token":"APP_TOKEN","user":"USER_KEY","message":"Got Image?"}, files={"attachment":open("PATH_TO_IMAGE","rb")})
    

    分解它。您传递请求的参数以在字典 data 中进行编码。然后你在字典 files 中传递文件。重要的是文件是用rb打开的(读取二进制)。

    【讨论】:

      猜你喜欢
      • 2021-03-22
      • 2012-07-14
      • 2013-11-12
      • 1970-01-01
      • 2017-08-15
      • 2015-02-24
      • 2018-01-18
      • 1970-01-01
      • 2019-05-14
      相关资源
      最近更新 更多