Files
kvcache-ai--ktransformers/kt-kernel/python/cli/utils/port_checker.py
T
wehub-resource-sync ec436095dd
Book-CI / test (macos-latest) (push) Has been cancelled
Book-CI / test (ubuntu-latest) (push) Has been cancelled
Book-CI / test (windows-latest) (push) Has been cancelled
Release Fake Tag / publish (push) Has been cancelled
Deploy / deploy (macos-latest) (push) Has been cancelled
Deploy / deploy (ubuntu-latest) (push) Has been cancelled
Deploy / deploy (windows-latest) (push) Has been cancelled
Release to PyPI / Build & publish sglang-kt (push) Has been cancelled
Release to PyPI / Build kt-kernel (Python 3.11) (push) Has been cancelled
Release to PyPI / Build kt-kernel (Python 3.12) (push) Has been cancelled
Release to PyPI / Publish kt-kernel to PyPI (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:30:03 +08:00

51 lines
1.4 KiB
Python

"""
Port availability checking utilities.
"""
import socket
import sys
from typing import Tuple
def is_port_available(host: str, port: int) -> bool:
"""Check if a port is available on the given host.
Args:
host: Host address (e.g., "0.0.0.0", "127.0.0.1")
port: Port number to check
Returns:
True if port is available, False if occupied
"""
try:
bind_host = "" if host == "0.0.0.0" else host
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
if sys.platform != "win32":
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((bind_host, port))
return True
except OSError:
# If any error occurs, assume port is not available
return False
def find_available_port(host: str, start_port: int, max_attempts: int = 100) -> Tuple[bool, int]:
"""Find an available port starting from start_port.
Args:
host: Host address
start_port: Starting port number to check
max_attempts: Maximum number of ports to try
Returns:
Tuple of (found, port_number)
- found: True if an available port was found
- port_number: The available port number (or start_port if not found)
"""
for port in range(start_port, start_port + max_attempts):
if is_port_available(host, port):
return True, port
return False, start_port