Find an available port starting from start_port. Args: start_port: Starting port number to check max_attempts: Maximum number of ports to try host: Host address to check Returns: Available port number, or None if no port found
(start_port: int = 8888,
max_attempts: int = 100,
host: str = '127.0.0.1')
| 56 | |
| 57 | |
| 58 | def find_available_port(start_port: int = 8888, |
| 59 | max_attempts: int = 100, |
| 60 | host: str = '127.0.0.1') -> Optional[int]: |
| 61 | """ |
| 62 | Find an available port starting from start_port. |
| 63 | |
| 64 | Args: |
| 65 | start_port: Starting port number to check |
| 66 | max_attempts: Maximum number of ports to try |
| 67 | host: Host address to check |
| 68 | |
| 69 | Returns: |
| 70 | Available port number, or None if no port found |
| 71 | """ |
| 72 | for port in range(start_port, start_port + max_attempts): |
| 73 | if check_port_available(port, host): |
| 74 | logger.info(f'Found available port: {port}') |
| 75 | return port |
| 76 | |
| 77 | logger.error( |
| 78 | f'Could not find available port in range {start_port}-{start_port + max_attempts - 1}' |
| 79 | ) |
| 80 | return None |
| 81 | |
| 82 | |
| 83 | class CodeExecutionTool(ToolBase): |
no test coverage detected