diff --git a/asynceapi/aio_portcheck.py b/asynceapi/aio_portcheck.py deleted file mode 100644 index 0cab94cb3..000000000 --- a/asynceapi/aio_portcheck.py +++ /dev/null @@ -1,63 +0,0 @@ -# Copyright (c) 2024 Arista Networks, Inc. -# Use of this source code is governed by the Apache License 2.0 -# that can be found in the LICENSE file. -# Initially written by Jeremy Schulman at https://github.com/jeremyschulman/aio-eapi -"""Utility function to check if a port is open.""" -# ----------------------------------------------------------------------------- -# System Imports -# ----------------------------------------------------------------------------- - -from __future__ import annotations - -import asyncio -import socket -from typing import TYPE_CHECKING - -# ----------------------------------------------------------------------------- -# Public Imports -# ----------------------------------------------------------------------------- - -if TYPE_CHECKING: - from httpx import URL - -# ----------------------------------------------------------------------------- -# Exports -# ----------------------------------------------------------------------------- - -__all__ = ["port_check_url"] - -# ----------------------------------------------------------------------------- -# -# CODE BEGINS -# -# ----------------------------------------------------------------------------- - - -async def port_check_url(url: URL, timeout: int = 5) -> bool: - """ - Open the port designated by the URL given the timeout in seconds. - - Parameters - ---------- - url - The URL that provides the target system. - timeout - Time to await for the port to open in seconds. - - Returns - ------- - bool - If the port is available then return True; False otherwise. - """ - port = url.port or socket.getservbyname(url.scheme) - - try: - wr: asyncio.StreamWriter - _, wr = await asyncio.wait_for(asyncio.open_connection(host=url.host, port=port), timeout=timeout) - - # MUST close if opened! - wr.close() - - except TimeoutError: - return False - return True diff --git a/asynceapi/device.py b/asynceapi/device.py index 933ae649c..b2bf15930 100644 --- a/asynceapi/device.py +++ b/asynceapi/device.py @@ -20,7 +20,6 @@ # ----------------------------------------------------------------------------- # Private Imports # ----------------------------------------------------------------------------- -from .aio_portcheck import port_check_url from .config_session import SessionConfig from .errors import EapiCommandError @@ -122,7 +121,11 @@ async def check_connection(self) -> bool: bool True when the device eAPI is accessible, False otherwise. """ - return await port_check_url(self.base_url) + try: + await self.head(self.base_url, timeout=5) + except httpx.RequestError: + return False + return True async def cli( # noqa: PLR0913 self,