aio-libs / aioftp

ftp client/server for asyncio (http://aioftp.readthedocs.org)
Apache License 2.0
185 stars 54 forks source link

Client example out of date #135

Closed Eric-Vignola closed 3 years ago

Eric-Vignola commented 3 years ago

Python 3.9 (win32) asyncio-3.4.3 aioftp-0.18.1

Just trying out aioftp following the example. Seems like list and download are undefined attributes of client.

pohmelie commented 3 years ago

More information need: code to reproduce and traceback.

Eric-Vignola commented 3 years ago

Modified the example in README.rst

Using an existing server, fill in the details for host, login, password and port.

` import asyncio, aioftp

Recursive

async def get_mp3(host, port, login, password): async with aioftp.Client.context(host, port, login, password) as client: for path, info in (await client.list(recursive=True)): if info["type"] == "file" and path.suffix == ".mp3": await client.download(path)

MY_SERVER_IP

tasks = ( asyncio.create_task(get_mp3("MY_SERVER_IP", 22, "MY_LOGIN", "MY_PASSWORD")) ) asyncio.run(asyncio.wait(tasks)) `

Traceback error:

Traceback (most recent call last): File "C:\async_test.py", line 17, in asyncio.create_task(get_mp3(host, port, login, password)) File "C:\Python39\lib\asyncio\tasks.py", line 360, in create_task loop = events.get_running_loop() RuntimeError: no running event loop sys:1: RuntimeWarning: coroutine 'get_mp3' was never awaited

Eric-Vignola commented 3 years ago

Sorry pressed close by mistake and before finishing.

MY_SERVER_IP, MY_LOGIN and MY_PASSWORD are filled out with my server details.

This is essentially the example edited to connect to my server. In my IDE (Wing) i see client.list and client.download as undefined attributes, which may or may not be related to the traceback error i posted above.

pohmelie commented 3 years ago

Oh, example code was modified by @decaz in recent pull request. I will fix them soon. In short, code should look as:

import asyncio
import aioftp

async def get_mp3(host, port, login, password):
    async with aioftp.Client.context(host, port, login, password) as client:
        for path, info in (await client.list(recursive=True)):
            if info["type"] == "file" and path.suffix == ".mp3":
                await client.download(path)

async def main():
    tasks = (
        asyncio.create_task(get_mp3("server1.com", 21, "login", "password")),
        asyncio.create_task(get_mp3("server2.com", 21, "login", "password")),
        asyncio.create_task(get_mp3("server3.com", 21, "login", "password")),
    )
    await asyncio.wait(tasks)

asyncio.run(main())
decaz commented 3 years ago

Ah, didn't notice that asyncio.run is getting asyncio.wait directly, sorry.