Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip DNS resolving if provided host is already an ip address #874

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion aiohttp/connector.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import aiohttp
import ipaddress
import functools
import http.cookies
import ssl
Expand Down Expand Up @@ -554,7 +555,7 @@ def clear_resolved_hosts(self, host=None, port=None):

@asyncio.coroutine
def _resolve_host(self, host, port):
if not self._use_resolver:
if not self._use_resolver or self._host_is_ip(host):
return [{'hostname': host, 'host': host, 'port': port,
'family': self._family, 'proto': 0, 'flags': 0}]

Expand All @@ -573,6 +574,13 @@ def _resolve_host(self, host, port):
host, port, family=self._family)
return res

def _host_is_ip(self, host):
try:
ipaddress.ip_address(host)
return True
except ValueError:
return False
Copy link
Member

@popravich popravich May 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everybody most of their time use host names not IP addresses,
so most of time this method will raise and except ValueError what is not very efficient.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything else to suggest? The ipaddress module does not have a way to verify the format while avoiding the exception. Custom-made validation does not seem like a better route.


@asyncio.coroutine
def _create_connection(self, req):
"""Create connection.
Expand Down
15 changes: 15 additions & 0 deletions tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,3 +839,18 @@ def test_both_use_dns_cache_only(self):
self.assertTrue(conn.use_dns_cache)
with self.assertWarns(DeprecationWarning):
self.assertTrue(conn.resolve)

def test_resolver_not_called_with_address_is_ip(self):
resolver = unittest.mock.MagicMock()
connector = aiohttp.TCPConnector(resolver=resolver, loop=self.loop)

class Req:
host = '127.0.1.2'
port = 63830
ssl = False
response = unittest.mock.Mock()

with self.assertRaises(OSError):
self.loop.run_until_complete(connector.connect(Req()))

resolver.resolve.assert_not_called()