惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

🤪C'est la vie

自建 Tailscale 的 DERP Server 使用 ACME.SH 申请 Google CA SSL 证书 版权声明 关于 Hello Hugo——记Hugo博客搭建 友链
优雅地使用 Python requests 调用 Aria2 JSON-RPC
Jonathan Zhang · 2023-01-17 · via 🤪C'est la vie

背景

博主打算造一个能从 Telegram 自动下载文件/媒体并自动上传至 Google Drive 的轮子,自动上传的功能已经有现成的轮子可以拼上了,生成直链的轮子也找到了,就差联动Aria2 JSON-RPCPython了,但是无奈找了半天都没有解决,最后还是问了大佬才搞定,遂记录。

其实本来看着JSON-RPC就比较复杂,相比之下XML-RPC就显得善良许多,但是不知道是版本问题还是什么原因,XML-RPC似乎不能使用,最后只好投奔复杂的JSON-RPC了。😭

说实话,我确实找到了不少现成的 Python 库可用,但是想着我只需要下个文件,没必要引入那么多不必要的代码,就想着自己写一小段了。🤔

还有一点就是,我看了好几个PyPI上的相关包,有的不支持新加入的rpc-secret属性,就没法用了。😒

依赖

依赖很少,甚至很多人早就安装好了。😂

$ pip install requests     #Windows
$ pip3 install requests    #MacOS or Linux

代码实现

import json, requests
data = {
"jsonrpc": "2.0",
"method": "aria2.addUri",        #操作为添加下载链接
"id": "1",        #这个 id 没什么用,随便填一个就行,重复也无所谓
"params": ["token:<Your rpc-secret>",["url"]]        #有设置 rpc-secret 参数的将此处 <Your rpc-secret> 替换为自己的 rpc-secret
  }
dl_url = 'https://dl.example.com/example.file'
data['params'][0][1] = dl_url        #自定义下载文件链接
url = ''        #你的 Aria2 服务器完整链接,使用 HTTP(S) 协议,包含端口
#例如 http://aria2.example.com:6800/jsonrpc
response = requests.post(url, data=json.dumps(data))
print(response.text)         #显示请求结果

返回结果示例:

{"id":"1","jsonrpc":"2.0","result":"a68bf66b2db28041"}    

"result" 中即为该下载链接对应的 GID