我正在使用下面的代码成功地发送消息到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发布于 2018-01-26 13:25:30
我没有尝试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")})把它拆散。传递要在dict 数据中格式化的请求参数。然后在dict 文件中传递该文件。重要的是文件是打开的rb (读二进制)。
https://stackoverflow.com/questions/48439624
复制相似问题