0

This is my code,I have used aioftp and asyncio with no success

import asyncio
import requests
import aiohttp
import urllib.request
import urllib.error
import time
import urllib.request

t1 = time.time()


async def job(session,url):
    filename = url.split("/")[-1]
    ftp = await session.get(url)
    ftpcode = await ftp.read()
    with open("new/"+str(filename),'wb') as f:
        f.write(ftpcode)
    return str(url)

    # urllib.request.urlretrieve(url, filename)

async def main(loop,URL):
    async with aiohttp.ClientSession()as session:
        tasks = [loop.create_task(job(session, URL[x])) for x in range(2)]
   
        finished, unfinished = await asyncio.wait(tasks)
        all_results = [r.result() for r in finished]
        print("ALL RESULT:", all_results)


URL = ["ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_package/8e/71/PMC5334499.tar.gz",
"ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_package/ad/b5/PMC7444942.tar.gz",
"ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_package/e8/38/PMC8903393.tar.gz",
"ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_package/f3/7d/PMC9130726.tar.gz"]

loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop,URL))
loop.close()

t2 = time.time()
print(int(t2-t1))

If you can help, I would be very grateful

QHarr
  • 83,427
  • 12
  • 54
  • 101

0 Answers0