2

I get an error. What is the reason for this error? I try to catch data from the API with an async function and it comes to this error.

ClientConnectorCertificateError at /
Cannot connect to host swapi.dev:443 ssl:True [SSLCertVerificationError: (1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1091)')]
async def index(request):
    start_time = time.time()
    url = 'https://swapi.dev/api/starships/9/'
    async with aiohttp.ClientSession() as client:
        task = asyncio.ensure_future(fetch(client, url))
        results = await asyncio.gather(task)
        total = time.time() - start_time
        print(total)
    return render(request, 'index.html', {'results':results })
Alireza
  • 2,103
  • 1
  • 6
  • 19
paul385mi
  • 21
  • 2

1 Answers1

0

you are most probably querying an API that do not have a verified ssl certificate. And by default most library doing http call do check for the validity of the said certificate and will issue a warning or error if they can not verify it. This is a configurable behavior, in you case I think if you do:

connector=aiohttp.TCPConnector(ssl=False)

You will be able to disable the check for a valid ssl certificate. So your code would end up looking like:

async def index(request):
start_time = time.time()
url = 'https://swapi.dev/api/starships/9/'
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as client:
    task = asyncio.ensure_future(fetch(client, url))
    results = await asyncio.gather(task)
    total = time.time() - start_time
    print(total)
return render(request, 'index.html', {'results':results })

ALTHOUGH, keep in mind that checking for the validity of the certificate is a security procedure. So you may want to take that into account.

Paulo
  • 8,690
  • 5
  • 20
  • 34