title: How to download files efficiently with Python?
body: <p>I want to download files.</p>
  
  <p>I used asyncio but it had no response for a long time when downloading files. Furthermore, it has problems when the server certificate is invalid.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>socket.setdefaulttimeout(30)
  async def get_file(url, folder, session):
      filename = folder.strip('\\') + '\\' + str(datetime.datetime.now()).replace(':', '_') + '.pdf'
      async with session.get(url, headers = headers) as resp:
          if resp.status == 200:
              f = await aiofiles.open(filename, mode='wb')
              await f.write(await resp.read())
              await f.close()
  
  async def download_pdf(urls, folder):
      connector = aiohttp.TCPConnector(limit=60)
      semaphore = asyncio.Semaphore(10)
      async with semaphore:
          async with ClientSession(connector=connector, headers=headers) as session:
              tasks = [get_file(url, folder, session) for url in urls]
              result = await asyncio.gather(*tasks)
  
  urls = ['https://www.example.com/abc.pdf', 'https://www.example2.com/def.pdf']
  directory = r'C:\data'
  asyncio.run(download_pdf(urls, directory))
  </code></pre>
  
  <p><em>How</em> <em>to</em> <em>download</em> <em>files</em> <strong>efficiently</strong>?</p>