Skip to content

Commit 7a845cd

Browse files
committed
feat: is_localhost function
Returns True if the given hostname is the current host
1 parent e9a2916 commit 7a845cd

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

cryosparc/platform.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,24 @@
44
from pathlib import Path
55
from typing import Callable
66

7-
__all__ = ("user_config_path",)
7+
__all__ = ("is_localhost", "user_config_path")
8+
9+
10+
def is_localhost(hostname: str) -> bool:
11+
"""
12+
Check if a hostname refers to the current host.
13+
"""
14+
import socket
15+
16+
if hostname == "localhost" or hostname.startswith("127.") or hostname == "::1":
17+
return True
18+
if hostname == socket.gethostname().replace(" ", "") or hostname == socket.getfqdn().replace(" ", ""):
19+
return True
20+
try:
21+
ip = socket.gethostbyname(hostname).strip()
22+
return ip.startswith("127.") or ip == "::1"
23+
except Exception:
24+
return False
825

926

1027
def user_config_path() -> Path:

0 commit comments

Comments
 (0)