检测端口是否可用(可绑定)。
(host: str, port: int)
| 46 | |
| 47 | |
| 48 | def _can_bind_port(host: str, port: int) -> bool: |
| 49 | """检测端口是否可用(可绑定)。""" |
| 50 | try: |
| 51 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: |
| 52 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 53 | sock.bind((host, port)) |
| 54 | return True |
| 55 | except OSError: |
| 56 | return False |
| 57 | |
| 58 | |
| 59 | def _find_available_port(host: str, preferred_port: int, max_scan: int = 100) -> int: |
no outgoing calls
no test coverage detected