aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host :443 ssl:default [The semaphore timeout period has expired]
Asked Answered
L

1

0

I use asyncio and aiohttp in my code, and it runs perfectly fine on my work laptop. However, when I run it on my virtual machine from my work laptop, I run into this error "The semaphore timeout period has expired" but I do not have semaphore in my code at all. Is this a network firewall issue? There is a proxy attached to the network settings and the VM is on windows 10.

Lop answered 6/3 at 16:1 Comment(0)
H
0

In my case the issue was that I was behind a corporate firewall. To fix the semaphore timeout error so my script would work, I added the proxy argument to session.get like in this example snippet:

import aiohttp

url = 'https://example.com'
proxy_url = 'http://<user>:<pass>@<proxy>:<port>'

async with aiohttp.ClientSession() as session:
    async with session.get(url, proxy=proxy_url)

Within Windows Subsystem for Linux (WSL) using Ubuntu/Debian I had to additionally set the SSL context, otherwise an incorrect ca bundle would be used resulting in a SSL Certificate Verify Failed error. For example:

import aiohttp
import ssl

url = 'https://example.com'
proxy_url = 'http://<user>:<pass>@<proxy>:<port>'
path_to_cafile = '/etc/ssl/certs/ca-certificates.crt'
ssl_ctx = ssl.create_default_context(cafile=path_to_cafile)

async with aiohttp.ClientSession() as session:
    async with session.get(url, proxy=proxy_url, ssl=ssl_ctx)

I posted this answer here as well: https://mcmap.net/q/1230255/-discord-py-oserror-winerror-121-the-semaphore-timeout-period-has-expired

Here is a related answer from a different user: https://mcmap.net/q/574163/-using-aiohttp-with-proxy

Halyard answered 9/7 at 14:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.