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

Support to control the use of ipv6 or ipv4 #83

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions qingstor/sdk/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
'# Valid levels are "debug", "info", "warn", "error", and "fatal".\n'
'log_level: "warn"\n'
'enable_virtual_host_style: false\n'
'enable_dual_stack: false\n'
'zone: ""\n'
)

Expand Down
31 changes: 31 additions & 0 deletions qingstor/sdk/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

from .build import Builder
from .utils.helper import url_quote
import requests.packages.urllib3.util.connection as urllib3_cn
import socket


class Request:
Expand All @@ -34,6 +36,9 @@ def __init__(self, config, operation):
self.secret_access_key = config.secret_access_key
self.logger = logging.getLogger("qingstor-sdk")
self.enable_virtual_host_style = config.enable_virtual_host_style
urllib3_cn.allowed_gai_family = rewrite_allowed_gai_family(
config.enable_dual_stack, self.req.headers["Host"]
)

def __repr__(self):
return "<Request %s>" % self.req.method
Expand Down Expand Up @@ -176,3 +181,29 @@ def is_sub_resource(self, key):
"replication", "append", "position", "cname"
]
return key in keys_map


def has_ipv6(host):
Copy link
Contributor

Choose a reason for hiding this comment

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

Add unittests here please.

Copy link
Contributor

Choose a reason for hiding this comment

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

ping @wangmq0719 for this review.

""" Returns True if the system resolves host to an IPv6 address by default. """
Copy link
Contributor

Choose a reason for hiding this comment

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

Single line comment can use:

# Returns True if the system resolves host to an IPv6 address by default.

resolves_to_ipv6 = False
try:
for res in socket.getaddrinfo(host, None, socket.AF_UNSPEC):
af, _, _, _, _ = res
if af == socket.AF_INET6:
resolves_to_ipv6 = True
except socket.gaierror:
pass
return resolves_to_ipv6


def rewrite_allowed_gai_family(use_ipv6, host):
""" This function is designed to control the choice of address family. """
def allowed_gai_family():
family = socket.AF_INET
if has_ipv6(host):
if use_ipv6:
family = socket.AF_INET6
else:
family = socket.AF_UNSPEC
return family
return allowed_gai_family