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

推荐订阅源

V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Latest news
Latest news
T
The Exploit Database - CXSecurity.com
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
L
Lohrmann on Cybersecurity
aimingoo的专栏
aimingoo的专栏
B
Blog
T
Threat Research - Cisco Blogs
罗磊的独立博客
Application and Cybersecurity Blog
Application and Cybersecurity Blog
P
Proofpoint News Feed
P
Palo Alto Networks Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
宝玉的分享
宝玉的分享
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
T
Tor Project blog
阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
Martin Fowler
Martin Fowler
酷 壳 – CoolShell
酷 壳 – CoolShell
Recorded Future
Recorded Future
D
DataBreaches.Net
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
B
Blog RSS Feed
Scott Helme
Scott Helme
P
Proofpoint News Feed
V
Vulnerabilities – Threatpost
A
Arctic Wolf
Help Net Security
Help Net Security
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Vercel News
Vercel News
AWS News Blog
AWS News Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Schneier on Security
Hacker News: Ask HN
Hacker News: Ask HN
N
Netflix TechBlog - Medium
L
LangChain Blog
博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
M
MIT News - Artificial intelligence
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
W
WeLiveSecurity

🤪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