命令行工具和库,用于通过JSON-RPC与aria2c守护进程交互。
项目描述
aria2p
命令行工具和Python库,通过JSON-RPC与aria2c
守护进程交互。
为了避免混淆
- aria2是一个轻量级的跨平台下载实用程序,支持多协议和多源,以命令行方式运行。它支持HTTP/HTTPS、FTP、SFTP、BitTorrent和Metalink。
aria2c
是aria2提供的命令行可执行文件的名称。它可以作为一个守护进程。aria2p
(p
代表Python)是一个命令行客户端,可以与aria2c
守护进程交互。它不是一个官方客户端。还有其他Python包允许您与aria2c
守护进程交互。这些其他包提供的可用性(在我看来)不足,这就是我开发aria2p
的原因。
目的:aria2c
可以前台运行,进行一次性下载,也可以后台运行,作为守护进程。这时,aria2p
就会介入:当 aria2c
的一个实例在后台运行时,aria2p
将能够与之通信,以便将下载添加到队列,删除、暂停或恢复它们等。
为了使 aria2p
能够与 aria2c
进程通信,必须通过 aria2c
的 --enable-rpc
选项启用 RPC 模式。RPC 代表远程过程调用(Remote Procedure Call)。尽管 aria2c
支持 JSON-RPC 和 XML-RPC 协议,但 aria2p
仅支持 JSON(不支持 XML)。有关如何配置 aria2c
以启用 RPC 模式作为守护进程的更多信息,请参阅文档中的 配置部分。
目录
要求
aria2 必须安装。在 apt-get
系统上
sudo apt-get install aria2
安装
使用 pip
python3.6 -m pip install aria2p[tui]
使用 pipx
python3.8 -m pip install --user pipx
pipx install aria2p[tui]
需要 tui
扩展才能使用交互式界面。如果您不需要该界面(例如,当您正在编写一个依赖 aria2p 的 Python 包时),只需安装不带任何扩展的 aria2p
即可。
使用(作为库)
这个库仍在开发中。稍后会增加更多示例。在此期间,您可以在官方文档的 参考部分 中阅读。
import aria2p
# initialization, these are the default values
aria2 = aria2p.API(
aria2p.Client(
host="http://localhost",
port=6800,
secret=""
)
)
# list downloads
downloads = aria2.get_downloads()
for download in downloads:
print(download.name, download.download_speed)
# add downloads
magnet_uri = "magnet:?xt=urn:..."
download = aria2.add_magnet(magnet_uri)
使用(命令行)
usage: aria2p [GLOBAL_OPTS...] COMMAND [COMMAND_OPTS...]
Command-line tool and Python library to interact with an `aria2c` daemon
process through JSON-RPC.
Global options:
-h, --help Show this help message and exit. Commands also accept
the -h/--help option.
-p PORT, --port PORT Port to use to connect to the remote server.
-H HOST, --host HOST Host address for the remote server.
-s SECRET, --secret SECRET
Secret token to use to connect to the remote server.
-L {TRACE,DEBUG,INFO,SUCCESS,WARNING,ERROR,CRITICAL}, --log-level {TRACE,DEBUG,INFO,SUCCESS,WARNING,ERROR,CRITICAL}
Log level to use
-P LOG_PATH, --log-path LOG_PATH
Log path to use. Can be a directory or a file.
-T CLIENT_TIMEOUT, --client-timeout CLIENT_TIMEOUT
Timeout in seconds for requests to the remote server.
Floats supported. Default: 60.0.
Commands:
add Add downloads with URIs/Magnets/torrents/Metalinks.
add-magnets (add-magnet)
Add downloads with Magnet URIs.
add-metalinks (add-metalink)
Add downloads with Metalink files.
add-torrents (add-torrent)
Add downloads with torrent files.
autopurge (autoclear)
Automatically purge completed/removed/failed
downloads.
call Call a remote method through the JSON-RPC client.
pause (stop) Pause downloads.
remove (rm, del, delete)
Remove downloads.
resume (start) Resume downloads.
show Show the download progression.
top Launch the top-like interactive interface.
listen Listen to notifications.
如果不带任何参数调用 aria2p
,则默认调用 top
命令,这是一个控制台交互式界面。
命令
add
usage: aria2p add [-h] [-f FROM_FILE] [uris [uris ...]]
Add downloads with URIs/Magnets/torrents/Metalinks.
positional arguments:
uris The URIs/file-paths to add.
optional arguments:
-h, --help Show this help message and exit.
-f FROM_FILE, --from-file FROM_FILE
Load URIs from a file.
add-magnets
usage: aria2p add-magnets [-h] [-f FROM_FILE] [uris [uris ...]]
Add downloads with Magnet URIs.
positional arguments:
uris The magnet URIs to add.
optional arguments:
-h, --help Show this help message and exit.
-f FROM_FILE, --from-file FROM_FILE
Load URIs from a file.
add-metalinks
usage: aria2p add-metalinks [-h] [-f FROM_FILE]
[metalink_files [metalink_files ...]]
Add downloads with Metalink files.
positional arguments:
metalink_files The paths to the metalink files.
optional arguments:
-h, --help Show this help message and exit.
-f FROM_FILE, --from-file FROM_FILE
Load file paths from a file.
add-torrents
usage: aria2p add-torrents [-h] [-f FROM_FILE]
[torrent_files [torrent_files ...]]
Add downloads with torrent files.
positional arguments:
torrent_files The paths to the torrent files.
optional arguments:
-h, --help Show this help message and exit.
-f FROM_FILE, --from-file FROM_FILE
Load file paths from a file.
autopurge
usage: aria2p autopurge [-h]
Automatically purge completed/removed/failed downloads.
optional arguments:
-h, --help Show this help message and exit.
call
usage: aria2p call [-h] [-P PARAMS [PARAMS ...] | -J PARAMS] method
Call a remote method through the JSON-RPC client.
positional arguments:
method The method to call (case insensitive). Dashes and
underscores will be removed so you can use as many as
you want, or none. Prefixes like 'aria2.' or 'system.'
are also optional.
optional arguments:
-h, --help Show this help message and exit.
-P PARAMS [PARAMS ...], --params-list PARAMS [PARAMS ...]
Parameters as a list of strings.
-J PARAMS, --json-params PARAMS
Parameters as a JSON string. You should always wrap it
at least once in an array '[]'.
如帮助文本中所述,method
可以是确切的方法名称,也可以是去掉前缀的名称。它不区分大小写,并且会删除破折号和下划线。
以下都是等效的
aria2.addUri
aria2.adduri
addUri
ADDURI
aria2.ADD-URI
add_uri
A-d_D-u_R-i
(是的,这是有效的)A---R---I---A---2.a__d__d__u__r__i
(我想你已经明白了)- 以及更多难看的格式...
示例
列出所有可用方法。此示例使用 jq
。
$ aria2p call listmethods | jq
[
"aria2.addUri",
"aria2.addTorrent",
"aria2.getPeers",
"aria2.addMetalink",
"aria2.remove",
"aria2.pause",
"aria2.forcePause",
"aria2.pauseAll",
"aria2.forcePauseAll",
"aria2.unpause",
"aria2.unpauseAll",
"aria2.forceRemove",
"aria2.changePosition",
"aria2.tellStatus",
"aria2.getUris",
"aria2.getFiles",
"aria2.getServers",
"aria2.tellActive",
"aria2.tellWaiting",
"aria2.tellStopped",
"aria2.getOption",
"aria2.changeUri",
"aria2.changeOption",
"aria2.getGlobalOption",
"aria2.changeGlobalOption",
"aria2.purgeDownloadResult",
"aria2.removeDownloadResult",
"aria2.getVersion",
"aria2.getSessionInfo",
"aria2.shutdown",
"aria2.forceShutdown",
"aria2.getGlobalStat",
"aria2.saveSession",
"system.multicall",
"system.listMethods",
"system.listNotifications"
]
列出所有活动下载的 GID(标识符)。请注意,我们必须将参数作为 JSON 字符串提供。
$ aria2p call tellactive -J '[["gid"]]'
[{"gid": "b686cad55029d4df"}, {"gid": "4b39a1ad8fd94e26"}, {"gid": "9d331cc4b287e5df"}, {"gid": "8c9de0df753a5195"}]
使用 GID 暂停下载。请注意,当需要单个字符串参数时,可以使用 -P
直接传递。
$ aria2p call pause -P b686cad55029d4df
"b686cad55029d4df"
使用磁力链接 URI 添加下载。此示例使用 jq -r
删除结果周围的引号。
$ aria2p call adduri -J '[["magnet:?xt=urn:..."]]' | jq -r
4b39a1ad8fd94e26f
清除下载结果(从列表中删除已完成下载)。
$ aria2p call purge_download_result
"OK"
listen
usage: aria2p listen [-h] [-c CALLBACKS_MODULE] [-t TIMEOUT]
[event_types [event_types ...]]
Listen to notifications.
positional arguments:
event_types The types of notifications to process: start, pause,
stop, error, complete or btcomplete. Example: aria2p
listen error btcomplete. Useful if you want to spawn
multiple specialized aria2p listener, for example one
for each type of notification, but still want to use
only one callback file.
optional arguments:
-h, --help Show this help message and exit.
-c CALLBACKS_MODULE, --callbacks-module CALLBACKS_MODULE
Path to the Python module defining your notifications
callbacks.
-t TIMEOUT, --timeout TIMEOUT
Timeout in seconds to use when waiting for data over
the WebSocket at each iteration. Use small values for
faster reactivity when stopping to listen.
pause
usage: aria2p pause [-h] [-a] [-f] [gids [gids ...]]
Pause downloads.
positional arguments:
gids The GIDs of the downloads to pause.
optional arguments:
-h, --help Show this help message and exit.
-a, --all Pause all the downloads.
-f, --force Pause without contacting servers first.
remove
usage: aria2p remove [-h] [-a] [-f] [gids [gids ...]]
Remove downloads.
positional arguments:
gids The GIDs of the downloads to remove.
optional arguments:
-h, --help Show this help message and exit.
-a, --all Remove all the downloads.
-f, --force Remove without contacting servers first.
resume
usage: aria2p resume [-h] [-a] [gids [gids ...]]
Resume downloads.
positional arguments:
gids The GIDs of the downloads to resume.
optional arguments:
-h, --help Show this help message and exit.
-a, --all Resume all the downloads.
show
usage: aria2p show [-h]
Show the download progression.
optional arguments:
-h, --help Show this help message and exit.
top
usage: aria2p top [-h]
Launch the top-like interactive interface.
optional arguments:
-h, --help Show this help message and exit.
故障排除
-
当使用
aria2p
作为 Python 库时,会输出以下错误信息requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=6800): Max retries exceeded with url: /jsonrpc (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x1115b1908>: Failed to establish a new connection: [Errno 61] Connection refused',))
解决方案:
aria2c
需要先启动并运行。
支持
为了支持我作为开源软件作者,请考虑通过以下平台捐赠或成为支持者
谢谢!
项目详情
下载文件
下载适用于您平台的应用程序。如果您不确定选择哪一个,请了解有关安装包的更多信息。