-
Notifications
You must be signed in to change notification settings - Fork 3
Handle IPv6 literal parsing in network utilities #2303
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
base: staging
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| environment variables. | ||
| """ | ||
|
|
||
| import ipaddress | ||
| import logging | ||
| import os | ||
| import socket | ||
|
|
@@ -15,15 +16,45 @@ | |
|
|
||
| def _parse_target(target: str, default_port: int) -> tuple[str, int]: | ||
| """Return ``(host, port)`` tuple for ``target``.""" | ||
| if ":" in target: | ||
| host, p = target.rsplit(":", 1) | ||
| try: | ||
| return host, int(p) | ||
| except ValueError: | ||
|
|
||
| if target.startswith("["): | ||
| end = target.find("]") | ||
| if end != -1: | ||
| host = target[1:end] | ||
| port_candidate = target[end + 1 :] | ||
| if port_candidate.startswith(":"): | ||
| port_str = port_candidate[1:] | ||
| if port_str: | ||
| try: | ||
| return host, int(port_str) | ||
| except ValueError: | ||
| return host, default_port | ||
| return host, default_port | ||
|
|
||
| if ":" in target: | ||
| host_candidate, port_candidate = target.rsplit(":", 1) | ||
| if port_candidate.isdigit(): | ||
| if ":" not in host_candidate or _is_ipv6_literal(host_candidate): | ||
| try: | ||
| return host_candidate, int(port_candidate) | ||
|
Comment on lines
+28
to
+39
|
||
| except ValueError: | ||
| return host_candidate, default_port | ||
| return target, default_port | ||
| if ":" not in host_candidate: | ||
| return host_candidate, default_port | ||
| return target, default_port | ||
|
|
||
|
|
||
| def _is_ipv6_literal(value: str) -> bool: | ||
| """Return ``True`` if ``value`` is a valid IPv6 literal.""" | ||
|
|
||
| try: | ||
| ipaddress.IPv6Address(value) | ||
| except ValueError: | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
| def _check_url(url: str, timeout: int) -> bool: | ||
| """Return ``True`` if ``url`` responds to a HEAD request.""" | ||
| try: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The docstring for
_parse_targetis minimal and doesn't document the complex IPv6 parsing logic. Consider expanding it to explain the supported formats and parsing rules, for example:This would help maintainers understand the parsing logic and expected behavior.