When using the aiohttp library in Python, you may occasionally see errors where aiohttp attempts to connect to the wrong host. For example, you request www.example.com but aiohttp connects to a different IP address.
This can be frustrating, but there are a few things you can try to resolve it:
Check your DNS configuration
One common cause is that you have an incorrect DNS configuration that is returning the wrong IP address for the domain name.
Try manually doing a DNS lookup from the command line using
Specify the host explicitly
When creating the aiohttp
async with aiohttp.ClientSession(host='www.example.com') as session:
# requests will now use the right host
This overrides aiohttp's normal host resolution and forces it to use the host you provide.
Use IP addresses instead of hostnames
Instead of using the domain name, switch to directly using the IP address, like
Of course the downside is that IP addresses may change, but it can help narrow down the problem.
Add server name indication (SNI)
For HTTPS connections, check that your aiohttp version has SNI enabled for the HTTPS requests. Older versions did not support SNI which can cause issues.
Upgrading to the latest aiohttp version can help resolve this.
With a bit of tweaking and troubleshooting of your DNS and connection settings, you should be able to resolve issues where aiohttp is connecting to the wrong host.