获取外网可访问的IP地址
()
| 34 | |
| 35 | |
| 36 | def get_external_ip() -> str: |
| 37 | """获取外网可访问的IP地址""" |
| 38 | try: |
| 39 | # 优先使用ray获取节点IP |
| 40 | if RAY_AVAILABLE: |
| 41 | try: |
| 42 | return ray.util.get_node_ip_address().strip("[]") |
| 43 | except: |
| 44 | pass |
| 45 | |
| 46 | # 备用方法:通过连接外部服务器获取本地IP |
| 47 | with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: |
| 48 | sock.connect(("8.8.8.8", 80)) |
| 49 | return sock.getsockname()[0] |
| 50 | except Exception: |
| 51 | return "127.0.0.1" |
| 52 | |
| 53 | |
| 54 | def is_port_available(host: str, port: int) -> bool: |
no outgoing calls
no test coverage detected