-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
__init__.py
32 lines (23 loc) · 881 Bytes
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from __future__ import annotations
import socket
import ipaddress
from .coro import get_hosts
__all__ = ["get_hosts", "address2net", "get_lan_ip"]
def get_lan_ip() -> ipaddress.IPv4Address | ipaddress.IPv6Address:
"""
get IP address of currently used LAN interface
ref: http://stackoverflow.com/a/23822431
"""
name = socket.gethostname()
host = socket.gethostbyname(name)
return ipaddress.ip_address(host)
def address2net(
addr: ipaddress.IPv4Address, mask: str = "24"
) -> ipaddress.IPv4Network | ipaddress.IPv6Network:
if isinstance(addr, ipaddress.IPv4Address):
net = ipaddress.ip_network(addr.exploded.rsplit(".", 1)[0] + f".0/{mask}")
elif isinstance(addr, ipaddress.IPv6Address):
net = ipaddress.ip_network(addr.exploded.rsplit(":", 1)[0] + f":0/{mask}")
else:
raise TypeError(addr)
return net