-
Notifications
You must be signed in to change notification settings - Fork 7.4k
[1/n] IPv6 support: address <-> host:port conversion for Python Part #55281
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
Merged
jjyao
merged 3 commits into
ray-project:master
from
Yicheng-Lu-llll:python-ip-port-utils
Aug 8, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| from typing import Optional, Tuple, Union | ||
|
|
||
|
|
||
| def parse_address(address: str) -> Optional[Tuple[str, str]]: | ||
| """Parse a network address string into host and port. | ||
|
|
||
| Args: | ||
| address: The address string to parse (e.g., "localhost:8000", "[::1]:8000"). | ||
|
|
||
| Returns: | ||
| Tuple with (host, port) if port found, None if no colon separator. | ||
| """ | ||
| pos = address.rfind(":") | ||
| if pos == -1: | ||
| return None | ||
|
|
||
| host = address[:pos] | ||
| port = address[pos + 1 :] | ||
|
|
||
| if ":" in host: | ||
| if host.startswith("[") and host.endswith("]"): | ||
| host = host[1:-1] | ||
| else: | ||
| # Invalid IPv6 (missing brackets) or colon is part of the address, not a host:port split. | ||
| return None | ||
|
|
||
| return (host, port) | ||
|
|
||
|
|
||
| def build_address(host: str, port: Union[int, str]) -> str: | ||
| """Build a network address string from host and port. | ||
|
|
||
| Args: | ||
| host: The hostname or IP address. | ||
| port: The port number (int or string). | ||
|
|
||
| Returns: | ||
| Formatted address string (e.g., "localhost:8000" or "[::1]:8000"). | ||
| """ | ||
| if host is not None and ":" in host: | ||
| # IPv6 address | ||
| return f"[{host}]:{port}" | ||
| # IPv4 address or hostname | ||
| return f"{host}:{port}" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import pytest | ||
| import sys | ||
|
|
||
| from ray._common.network_utils import parse_address, build_address | ||
|
|
||
|
|
||
| class TestBuildAddress: | ||
| """Test cases for build_address function, matching C++ tests exactly.""" | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "host,port,expected", | ||
| [ | ||
| # IPv4 | ||
| ("192.168.1.1", 8080, "192.168.1.1:8080"), | ||
| ("192.168.1.1", "8080", "192.168.1.1:8080"), | ||
| # IPv6 | ||
| ("::1", 8080, "[::1]:8080"), | ||
| ("::1", "8080", "[::1]:8080"), | ||
| ("2001:db8::1", 8080, "[2001:db8::1]:8080"), | ||
| ("2001:db8::1", "8080", "[2001:db8::1]:8080"), | ||
| # Hostname | ||
| ("localhost", 9000, "localhost:9000"), | ||
| ("localhost", "9000", "localhost:9000"), | ||
| ], | ||
| ) | ||
| def test_build_address(self, host, port, expected): | ||
| """Test building address strings from host and port.""" | ||
| result = build_address(host, port) | ||
| assert result == expected | ||
|
|
||
|
|
||
| class TestParseAddress: | ||
| """Test cases for parse_address function, matching C++ tests exactly.""" | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "address,expected", | ||
| [ | ||
| # IPv4 | ||
| ("192.168.1.1:8080", ("192.168.1.1", "8080")), | ||
| # IPv6:loopback address | ||
| ("[::1]:8080", ("::1", "8080")), | ||
| # IPv6 | ||
| ("[2001:db8::1]:8080", ("2001:db8::1", "8080")), | ||
| # Hostname:Port | ||
| ("localhost:9000", ("localhost", "9000")), | ||
| ], | ||
| ) | ||
| def test_parse_valid_addresses(self, address, expected): | ||
| """Test parsing valid addresses.""" | ||
| result = parse_address(address) | ||
| assert result == expected | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "address", | ||
| [ | ||
| # bare IP or hostname | ||
| # should return None when no port is found | ||
| "::1", | ||
| "2001:db8::1", | ||
| "192.168.1.1", | ||
| "localhost", | ||
| ], | ||
| ) | ||
| def test_parse_bare_addresses(self, address): | ||
| """Test parsing bare addresses returns None.""" | ||
| result = parse_address(address) | ||
| assert result is None | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you need main function |
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(pytest.main(["-v", __file__])) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can you add some tests? In the follow-up, we can explore combining this with the c++ version via cython.