blob: deac0431a3381ee3400f063ec76bc78d9e14982a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# 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
|