MCPcopy Create free account
hub / github.com/geekcomputers/Python / download_file_by_url

Function download_file_by_url

async_downloader/async_downloader.py:65–108  ·  view source on GitHub ↗
(url, session=None)

Source from the content-addressed store, hash-verified

63
64
65async def download_file_by_url(url, session=None):
66 fail = True
67 file_name = basename(url)
68
69 assert session
70
71 try:
72 async with session.get(url) as response:
73 if response.status == 404:
74 print(
75 "\t{} from {} : Failed : {}".format(
76 file_name, url, "404 - Not found"
77 )
78 )
79 return fail, url
80
81 if not response.status == 200:
82 print(
83 "\t{} from {} : Failed : HTTP response {}".format(
84 file_name, url, response.status
85 )
86 )
87 return fail, url
88
89 data = await response.read()
90
91 with open(file_name, "wb") as file:
92 file.write(data)
93
94 except asyncio.TimeoutError:
95 print("\t{} from {}: Failed : {}".format(file_name, url, "Timeout error"))
96
97 except aiohttp.client_exceptions.ClientConnectionError:
98 print(
99 "\t{} from {}: Failed : {}".format(
100 file_name, url, "Client connection error"
101 )
102 )
103
104 else:
105 print("\t{} from {} : Success".format(file_name, url))
106 fail = False
107
108 return fail, url
109
110
111def test():

Callers 1

async_downloaderFunction · 0.85

Calls 1

getMethod · 0.45

Tested by

no test coverage detected