How to process DNS lookup or client connection errors? #5179
-
🐞 Describe the bug Connection Errors with known subdomains: socket.gaierror: [Errno -2] Name or service not known 💡 To Reproduce Scan a domain, any domain, using sublist3r. Pick a subdomain from the output of sublis3r and use with the following example code: import aiohttp
import asyncio
async def main():
async with aiohttp.ClientSession() as session:
async with session.get('http://<domain from sublist3r>') as response:
print("Status:", response.status)
print("Content-type:", response.headers['content-type'])
html = await response.text()
print("Body:", html[:15], "...")
loop = asyncio.get_event_loop()
loop.run_until_complete(main()) 💡 Expected behavior I expect to get a 404 status code 📋 Logs/tracebacks future: <Task finished name='Task-8' coro=<get() done, defined at subforce.py:229> exception=ClientConnectorError(ConnectionKey(host='airviewstatus.resmed.com', port=443, is_ssl=True, ssl=None, proxy=None, proxy_auth=None, proxy_headers_hash=None), gaierror(-2, 'Name or service not known')) created at subforce.py:264>
source_traceback: Object created at (most recent call last):
File "subforce.py", line 284, in <module>
domains_loop.run_until_complete(iterate_domains())
File "/usr/lib/python3.8/asyncio/base_events.py", line 603, in run_until_complete
self.run_forever()
File "/usr/lib/python3.8/asyncio/base_events.py", line 570, in run_forever
self._run_once()
File "/usr/lib/python3.8/asyncio/base_events.py", line 1851, in _run_once
handle._run()
File "/usr/lib/python3.8/asyncio/events.py", line 81, in _run
self._context.run(self._callback, *self._args)
File "subforce.py", line 264, in iterate_domains
ret = domains_loop.create_task(get(domains_list))
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/aiohttp/connector.py", line 1001, in _create_direct_connection
hosts = await asyncio.shield(host_resolved)
File "/usr/lib/python3.8/site-packages/aiohttp/connector.py", line 1001, in _create_direct_connection
hosts = await asyncio.shield(host_resolved)
File "/usr/lib/python3.8/site-packages/aiohttp/connector.py", line 1001, in _create_direct_connection
hosts = await asyncio.shield(host_resolved)
[Previous line repeated 1 more time]
File "/usr/lib/python3.8/site-packages/aiohttp/connector.py", line 854, in _resolve_host
await event.wait()
File "/usr/lib/python3.8/site-packages/aiohttp/locks.py", line 38, in wait
raise self._exc
File "/usr/lib/python3.8/site-packages/aiohttp/connector.py", line 854, in _resolve_host
await event.wait()
File "/usr/lib/python3.8/site-packages/aiohttp/locks.py", line 38, in wait
raise self._exc
File "/usr/lib/python3.8/site-packages/aiohttp/connector.py", line 854, in _resolve_host
await event.wait()
File "/usr/lib/python3.8/site-packages/aiohttp/locks.py", line 38, in wait
raise self._exc
File "/usr/lib/python3.8/site-packages/aiohttp/connector.py", line 867, in _resolve_host
addrs = await self._resolver.resolve(host, port, family=self._family)
File "/usr/lib/python3.8/site-packages/aiohttp/resolver.py", line 31, in resolve
infos = await self._loop.getaddrinfo(
File "/usr/lib/python3.8/asyncio/base_events.py", line 825, in getaddrinfo
return await self.run_in_executor(
File "/usr/lib/python3.8/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/usr/lib/python3.8/asyncio/base_events.py", line 808, in _getaddrinfo_debug
addrinfo = socket.getaddrinfo(host, port, family, type, proto, flags)
File "/usr/lib/python3.8/socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "subforce.py", line 239, in get
results = await asyncio.gather(*tasks) # gather task responses
File "subforce.py", line 225, in fetch
async with session.get(url) as response:
File "/usr/lib/python3.8/site-packages/aiohttp/client.py", line 1124, in __aenter__
self._resp = await self._coro
File "/usr/lib/python3.8/site-packages/aiohttp/client.py", line 527, in _request
conn = await self._connector.connect(
File "/usr/lib/python3.8/site-packages/aiohttp/connector.py", line 537, in connect
proto = await self._create_connection(req, traces, timeout)
File "/usr/lib/python3.8/site-packages/aiohttp/connector.py", line 894, in _create_connection
_, proto = await self._create_direct_connection(req, traces, timeout)
File "/usr/lib/python3.8/site-packages/aiohttp/connector.py", line 1013, in _create_direct_connection
raise ClientConnectorError(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host airviewstatus.resmed.com:443 ssl:default [Name or service not known]
exception calling callback for <Future at 0x7f2fc8585940 state=finished returned list>
📋 Your version of the Python $ python --version
Python 3.8.6 📋 Your version of the aiohttp/yarl/multidict distributions $ python -m pip show aiohttp
...Name: aiohttp
Version: 3.7.2
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: Nikolay Kim
Author-email: [email protected]
License: Apache 2
Location: /usr/lib/python3.8/site-packages
Requires: multidict, attrs, async-timeout, yarl, typing-extensions, chardet
Required-by:
$ python -m pip show multidict
Name: multidict
Version: 5.0.0
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache 2
Location: /usr/lib/python3.8/site-packages
Requires:
Required-by: yarl, aiohttp
$ python -m pip show yarl
Name: yarl
Version: 1.6.2
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl/
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache 2
Location: /usr/lib/python3.8/site-packages
Requires: multidict, idna
Required-by: aiohttp
Linux |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
That is a DNS lookup error, not 404. |
Beta Was this translation helpful? Give feedback.
-
There's nothing else to expect in such cases. If you cannot find a server, there's no way that server could send you 404. |
Beta Was this translation helpful? Give feedback.
-
Just use try/except to process |
Beta Was this translation helpful? Give feedback.
Just use try/except to process
aiohttp.ClientConnectionError
per https://docs.aiohttp.org/en/stable/client_reference.html#connection-errors and that's it.