Skip to main content

Support Aria2 rpc client and manage server with async/await

Project description

aioaria2

提供aria2异步客户端通信

pypi python implementation wheel license

提供与aria2异步通信的客户端与管理aria2进程的服务端

使用方法:

示例

import asyncio
from pprint import pprint

import aioaria2


async def main():
    async with aioaria2.Aria2HttpClient("http://117.0.0.1:6800/jsonrpc",
                                        token="token") as client:
        pprint(await client.getVersion())


asyncio.run(main())

相关ip地址应该换成自己的

client对象的相关方法见aria2手册

# 示例http协议
import asyncio
from pprint import pprint

import aioaria2
import ujson


async def main():
    async with aioaria2.Aria2HttpClient("http://127.0.0.1:6800/jsonrpc",
                                        token="token",
                                        loads=ujson.loads,
                                        dumps=ujson.dumps) as client:
        pprint(await client.addUri(["http://www.demo.com"]))  # 即可下载


asyncio.run(main())
# 示例websocket协议
import asyncio
from pprint import pprint

import aioaria2
import ujson


@aioaria2.run_sync
def on_download_complete(trigger, data):
    print(f"downlaod complete {data}")


async def main():
    client: aioaria2.Aria2WebsocketTrigger = await aioaria2.Aria2WebsocketTrigger.new("http://127.0.0.1:6800/jsonrpc",
                                                                                      token="token",
                                                                                      loads=ujson.loads,
                                                                                      dumps=ujson.dumps)
    client.onDownloadComplete(on_download_complete)
    pprint(await client.addUri(["http://www.demo.com"]))  # 即可下载


loop = asyncio.get_event_loop()
loop.create_task(main())
loop.run_forever()
  • 运行该协程函数即可,方法对应aria2jsonrpc的方法。对于服务端,每一个实例对应一个aria2进程
import aioaria2
import asyncio


async def main():
    server = aioaria2.AsyncAria2Server(r"aria2c.exe",
                                       r"--conf-path=aria2.conf", "--rpc-secret=admin", daemon=True)
    await server.start()
    await server.wait()


asyncio.run(main())

即可启动一个aria2进程

参考选项及设置

todolist

  • 异步http通信
  • 异步websocket通信
  • 修复server类的bug
  • 单元测试

本模块在aria2jsonrpc 之上构建,提供了异步支持,以级websocket支持

windows用户应该加上以下设置

# 为了启动异步子进程管理
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
asyncio.set_event_loop(asyncio.ProactorEventLoop())
python3.8以后默认是ProactorEventLoop,因此可以不用修改

v1.2.0更新

新增Aria2WebsocketTrigger类,可以监听websocket消息, 使用on*方法注册自定义回调函数,既可以是同步也可以是异步的

如下

@trigger.onDownloadStart
async def onDownloadStart(trigger, future):
    print("下载开始{0}".format(future.result()))

v1.2.3更新

可以给一个事件注册多个回调,现在只能是协程函数,同步函数需要自行从utils.run_sync包装

@trigger.onDownloadStart
async def callback1(trigger, future):
    print("第一个回调{0}".format(future.result()))

@trigger.onDownloadStart
@run_sync
def callback2(trigger, future):
    print("第二个回调{0}".format(future.result()))

v1.3.0更新

本版本大量修改了Aria2WebsocketTrigger类的方法,Aria2HttpClient保持不变

  • 回调直接接受dict参数而不再是asyncio.Future
  • Aria2WebsocketTrigger的相应方法获得了返回值,等效于http协议
  • id现在需要传入一个可以调用的id工厂函数作为uuid使用,否则将使用默认的uuid生成器
@trigger.onDownloadStart
async def callback1(trigger, data:dict):
    print("第一个回调{0}".format(data))

@trigger.onDownloadStart
@run_sync
def callback2(trigger, data:dict):
    print("第二个回调{0}".format(data))

v1.3.1更新

  • 可以使用自定义的json序列化函数,使用关键字参数loads= dumps=传入构造方法

title

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aioaria2-1.3.1.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

aioaria2-1.3.1-py3-none-any.whl (31.2 kB view details)

Uploaded Python 3

File details

Details for the file aioaria2-1.3.1.tar.gz.

File metadata

  • Download URL: aioaria2-1.3.1.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/51.3.3 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.5

File hashes

Hashes for aioaria2-1.3.1.tar.gz
Algorithm Hash digest
SHA256 28a64e76e4eb0cfafcb85ca52226072c70c2e893aefcc3d6ff32f3b1f0ec7d48
MD5 9388736a4a1328a961d8585e90bee975
BLAKE2b-256 7c0fea154dbf452456139cf6621782deb48701332e92014d15c3c66f58c34d89

See more details on using hashes here.

File details

Details for the file aioaria2-1.3.1-py3-none-any.whl.

File metadata

  • Download URL: aioaria2-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 31.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/51.3.3 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.5

File hashes

Hashes for aioaria2-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 beb0573d315d88bf2191fba9c5c60c88346ee8320a412ed3ffac265982e438eb
MD5 10370b959e1d115b3cec7de2bd6d64ae
BLAKE2b-256 1d7c7f14fb19d8128a2be84dd20a8e2435f257330e904a86e1c5f51cdd0e3cb6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page