先前是使用 yagmail 将附件(文件或图片)发送到邮箱,因为要经常接收消息,感觉还是使用Server酱推送到微信比较方便,这里记录下如何使用Python调用Server酱api接口实现微信推送
配置
Server酱有两个版本,具体见 Server酱升级说明 ,这里以旧版为例(可能会失效)
按照 Server酱旧版 配置好后,在 发送消息
复制URL,类似 https://sc.ftqq.com/你的SCKEY.send
一个简单例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import requests
url = 'https://sc.ftqq.com/你的SCKEY.send' desp = ''' 内容1 内容2 内容3 ''' data = { 'text': '标题', 'desp': desp }
requests.post(url, data=data)
|
发送文件内容
比如发送txt文件内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import requests
def read_txt(path): ''' 读取文件 '''
with open(path, 'r', encoding='utf-8') as f: txt = f.read() print(txt) return txt
def pust_txt(text, desp): ''' 发送消息 '''
url = 'https://sc.ftqq.com/你的SCKEY.send' data = { 'text': text, 'desp': desp }
requests.post(url, data=data)
pust_txt('标题', read_txt('文件路径'))
|
path
:文件路径
text
:标题
desp
:文本内容
注意:如果文件有多行,那么微信收到消息后,在查看详情页面时不会换行显示,而是拼接在一起,解决方法是在行与行之间留有一个空行