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

BugFix: Add support for both IPv4 and IPv6 connections #229

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
28 changes: 21 additions & 7 deletions redshift_connector/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,19 +395,28 @@ def client_os_version(self: "Connection") -> str:
os_version = "unknown"
return os_version


@staticmethod
def __get_host_address_info(host: str, port: int):
def __get_host_address_info(host: str, port: int) -> typing.Tuple:
"""
Returns IPv4 address and port given a host name and port
Returns address and port given a host name and port, supports both IPv4 and IPv6.
"""
# https://docs.python.org/3/library/socket.html#socket.getaddrinfo
response = socket.getaddrinfo(host=host, port=port, family=socket.AF_INET)
response = socket.getaddrinfo(host=host, port=port)
_logger.debug("getaddrinfo response %s", response)

if not response:
raise InterfaceError("Unable to determine ip for host %s port %s", host, port)
raise InterfaceError(
"Unable to determine IP for host %s port %s" % (host, port)
)

return response[0][4]
for res in response:
af, socktype, proto, canonname, sockaddr = res
if af == socket.AF_INET or af == socket.AF_INET6:
return sockaddr, af

raise InterfaceError(
"No suitable address found for host %s port %s" % (host, port)
)

def __init__(
self: "Connection",
Expand Down Expand Up @@ -623,8 +632,13 @@ def get_calling_module() -> str:
self._usock.settimeout(timeout)

if unix_sock is None and host is not None:
hostport: typing.Tuple[str, int] = Connection.__get_host_address_info(host, port)
# hostport: typing.Tuple[str, int] = Connection.__get_host_address_info(host, port)
# _logger.debug("Attempting to create connection socket with address %s", hostport)
# self._usock.connect(hostport)
hostport, address_family = Connection.__get_host_address_info(host, port)
_logger.debug("Attempting to create connection socket with address %s", hostport)

self._usock = socket.socket(address_family, socket.SOCK_STREAM)
self._usock.connect(hostport)
elif unix_sock is not None:
_logger.debug("connecting to socket with unix socket")
Expand Down