aio-libs / aiodns

Simple DNS resolver for asyncio
https://pypi.python.org/pypi/aiodns
MIT License
538 stars 69 forks source link

Problems using custom DNS with aiohttp #40

Closed vgoklani closed 6 years ago

vgoklani commented 6 years ago

I followed the example from here:

https://aiohttp.readthedocs.io/en/stable/client_advanced.html#resolving-using-custom-nameservers

and would like to use a custom DNS server with aiohttp:

import asyncio
import uvloop
import aiohttp
from aiohttp.resolver import AsyncResolver

asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
loop = asyncio.get_event_loop()

resolver = AsyncResolver(nameservers=["1.1.1.1", "1.0.0.1"])
session = aiohttp.ClientSession(loop=loop, connector=aiohttp.TCPConnector(limit=100, ssl=False, force_close=True,
                ttl_dns_cache=300, resolver=resolver), timeout=aiohttp.ClientTimeout(total=5))

async def f(url):
    response = await session.get(url)
    response.raise_for_status()
    return response

loop.run_until_complete(f("https://example.com"))

unfortunately I don't know what to make of the error message:

OSErrorTraceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/aiohttp/connector.py in _wrap_create_connection(self, req, timeout, client_error, *args, **kwargs)
    821             with CeilTimeout(timeout.sock_connect):
--> 822                 return await self._loop.create_connection(*args, **kwargs)
    823         except certificate_errors as exc:

/opt/conda/lib/python3.6/site-packages/uvloop/loop.pyx in create_connection()

/opt/conda/lib/python3.6/site-packages/uvloop/loop.pyx in uvloop.loop.Loop.create_connection()

/opt/conda/lib/python3.6/site-packages/uvloop/handles/tcp.pyx in uvloop.loop.TCPTransport.connect()

/opt/conda/lib/python3.6/site-packages/uvloop/handles/tcp.pyx in uvloop.loop._TCPConnectRequest.connect()

OSError: [Errno 99] Cannot assign requested address

The above exception was the direct cause of the following exception:

ClientConnectorErrorTraceback (most recent call last)
<ipython-input-1-622959d2ae35> in <module>()
     16     return response
     17
---> 18 loop.run_until_complete(f("https://example.com"))

/opt/conda/lib/python3.6/site-packages/uvloop/loop.pyx in uvloop.loop.Loop.run_until_complete()

<ipython-input-1-622959d2ae35> in f(url)
     12
     13 async def f(url):
---> 14     response = await session.get(url)
     15     response.raise_for_status()
     16     return response

/opt/conda/lib/python3.6/site-packages/aiohttp/client.py in _request(self, method, url, params, data, json, headers, skip_auto_headers, auth, allow_redirects, max_redirects, compress, chunked, expect100, read_until_eof, proxy, proxy_auth, timeout, verify_ssl, fingerprint, ssl_context, ssl, proxy_headers, trace_request_ctx)
    364                                 req,
    365                                 traces=traces,
--> 366                                 timeout=timeout
    367                             )
    368                     except asyncio.TimeoutError as exc:

/opt/conda/lib/python3.6/site-packages/aiohttp/connector.py in connect(self, req, traces, timeout)
    443
    444             try:
--> 445                 proto = await self._create_connection(req, traces, timeout)
    446                 if self._closed:
    447                     proto.close()

/opt/conda/lib/python3.6/site-packages/aiohttp/connector.py in _create_connection(self, req, traces, timeout)
    755         else:
    756             _, proto = await self._create_direct_connection(
--> 757                 req, traces, timeout)
    758
    759         return proto

/opt/conda/lib/python3.6/site-packages/aiohttp/connector.py in _create_direct_connection(self, req, traces, timeout, client_error)
    877             return transp, proto
    878         else:
--> 879             raise last_exc
    880
    881     async def _create_proxy_connection(self, req, traces, timeout):

/opt/conda/lib/python3.6/site-packages/aiohttp/connector.py in _create_direct_connection(self, req, traces, timeout, client_error)
    860                     server_hostname=hinfo['hostname'] if sslcontext else None,
    861                     local_addr=self._local_addr,
--> 862                     req=req, client_error=client_error)
    863             except ClientConnectorError as exc:
    864                 last_exc = exc

/opt/conda/lib/python3.6/site-packages/aiohttp/connector.py in _wrap_create_connection(self, req, timeout, client_error, *args, **kwargs)
    827             raise ClientConnectorSSLError(req.connection_key, exc) from exc
    828         except OSError as exc:
--> 829             raise client_error(req.connection_key, exc) from exc
    830
    831     async def _create_direct_connection(self, req, traces, timeout,

ClientConnectorError: Cannot connect to host example.com:443 ssl:None [Cannot assign requested address]
rooterkyberian commented 6 years ago

your example seems to be quite overcomplicated, but even with it I cannot reproduce your error with aiodns==1.1.1, aiohttp==3.3.2, uvloop==0.11.0 on Linux. I'm getting ClientConnectorError: Cannot connect to host example.com:443 ssl:None [Network is unreachable]

Please provide more information about your environment, and remove unnecessary stuff, like uvloop, which just over complicates things here. Or is it part of the problem? please state that clearly. It would be best to remove aiohttp from the picture too, or just report error to that project instead.

srgkm commented 6 years ago

@vgoklani this is aiohttp error and not related to aiodns directly.

saghul commented 6 years ago

Closing since this is not something aiodns related.