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

Add IPv6 support to Connection class #3562

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
13 changes: 12 additions & 1 deletion redis/connection.py
Original file line number Diff line number Diff line change
@@ -735,15 +735,26 @@ def _connect(self):
# ipv4/ipv6, but we want to set options prior to calling
# socket.connect()
err = None
try:
ip = ipaddress.ip_address(self.host)
is_ipv6 = isinstance(ip, ipaddress.IPv6Address)
except ValueError:
is_ipv6 = False
for info in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM):
if info[0] == socket.AF_INET6:
is_ipv6 = True
break
for res in socket.getaddrinfo(
self.host, self.port, self.socket_type, socket.SOCK_STREAM
self.host, self.port, socket.AF_INET6 if is_ipv6 else socket.AF_INET, socket.SOCK_STREAM
):
family, socktype, proto, canonname, socket_address = res
sock = None
try:
sock = socket.socket(family, socktype, proto)
# TCP_NODELAY
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
if family == socket.AF_INET6:
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)

# TCP_KEEPALIVE
if self.socket_keepalive: