From ad4ae4b01bd4dea18c271d5fdd561e5fe1802fde Mon Sep 17 00:00:00 2001 From: Corey Goldberg <1113081+cgoldberg@users.noreply.github.com> Date: Tue, 19 Aug 2025 15:35:24 -0400 Subject: [PATCH 1/3] [py] Add args to is_url_connectable --- py/selenium/webdriver/common/utils.py | 33 ++++++++++++++++----------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/py/selenium/webdriver/common/utils.py b/py/selenium/webdriver/common/utils.py index 801b88ce2d027..2e9c04cec71b8 100644 --- a/py/selenium/webdriver/common/utils.py +++ b/py/selenium/webdriver/common/utils.py @@ -14,9 +14,11 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -"""The Utils methods.""" + +"""Utility functions.""" import socket +import urllib from collections.abc import Iterable from typing import Optional, Union @@ -67,8 +69,8 @@ def find_connectable_ip(host: Union[str, bytes, bytearray, None], port: Optional port are considered. :Args: - - host - A hostname. - - port - Optional port number. + - host - hostname + - port - port number :Returns: A single IP address, as a string. If any IPv4 address is found, one is @@ -100,8 +102,8 @@ def join_host_port(host: str, port: int) -> str: example, _join_host_port('::1', 80) == '[::1]:80'. :Args: - - host - A hostname. - - port - An integer port. + - host - hostname or IP + - port - port number """ if ":" in host and not host.startswith("["): return f"[{host}]:{port}" @@ -112,7 +114,8 @@ def is_connectable(port: int, host: Optional[str] = "localhost") -> bool: """Tries to connect to the server at port to see if it is running. :Args: - - port - The port to connect. + - port - port number + - host - hostname or IP """ socket_ = None try: @@ -130,17 +133,21 @@ def is_connectable(port: int, host: Optional[str] = "localhost") -> bool: return result -def is_url_connectable(port: Union[int, str]) -> bool: - """Tries to connect to the HTTP server at /status path and specified port - to see if it responds successfully. +def is_url_connectable( + port: Union[int, str], + host: Optional[str] = "127.0.0.1", + scheme: Optional[str] = "http", +) -> bool: + """Sends a request to the HTTP server at the /status endpoint to see if it + responds successfully. :Args: - - port - The port to connect. + - port - port number + - host - hostname or IP + - scheme - URL scheme """ - from urllib import request as url_request - try: - res = url_request.urlopen(f"http://127.0.0.1:{port}/status") + res = urllib.request.urlopen(f"{scheme}://{host}:{port}/status") return res.getcode() == 200 except Exception: return False From b7f21d3d221e4f34155d4142517b462321d9d602 Mon Sep 17 00:00:00 2001 From: Corey Goldberg <1113081+cgoldberg@users.noreply.github.com> Date: Tue, 19 Aug 2025 15:50:27 -0400 Subject: [PATCH 2/3] [py] Update import --- py/selenium/webdriver/common/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/selenium/webdriver/common/utils.py b/py/selenium/webdriver/common/utils.py index 2e9c04cec71b8..3435e1ff1501b 100644 --- a/py/selenium/webdriver/common/utils.py +++ b/py/selenium/webdriver/common/utils.py @@ -18,7 +18,7 @@ """Utility functions.""" import socket -import urllib +import urllib.request from collections.abc import Iterable from typing import Optional, Union From c02cf5622079c34808fabd6fabf5de7ee386466f Mon Sep 17 00:00:00 2001 From: Corey Goldberg <1113081+cgoldberg@users.noreply.github.com> Date: Tue, 19 Aug 2025 15:55:41 -0400 Subject: [PATCH 3/3] [py] Use context manager --- py/selenium/webdriver/common/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/py/selenium/webdriver/common/utils.py b/py/selenium/webdriver/common/utils.py index 3435e1ff1501b..fd5ac0b0607f2 100644 --- a/py/selenium/webdriver/common/utils.py +++ b/py/selenium/webdriver/common/utils.py @@ -147,8 +147,8 @@ def is_url_connectable( - scheme - URL scheme """ try: - res = urllib.request.urlopen(f"{scheme}://{host}:{port}/status") - return res.getcode() == 200 + with urllib.request.urlopen(f"{scheme}://{host}:{port}/status") as res: + return res.getcode() == 200 except Exception: return False