-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetutils.py
41 lines (33 loc) · 1.07 KB
/
netutils.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
33
34
35
36
37
38
39
40
import socket
import ipaddress
from typing import Optional, Union, Dict, List
def reverse_dns(
ip: str,
timeout: float = 1.0,
detailed: bool = False
) -> Optional[Union[str, Dict[str, Union[str, List[str]]]]]:
ip = ip.strip()
if not ip:
return None
try:
ip_obj = ipaddress.ip_address(ip)
with _SocketTimeout(timeout):
hostname, aliases, _ = socket.gethostbyaddr(str(ip_obj))
if detailed:
return {
"ip": str(ip_obj),
"hostname": hostname,
"aliases": aliases if aliases else []
}
return hostname
except (ValueError, socket.herror, socket.gaierror, OSError):
return None
class _SocketTimeout:
__slots__ = ("timeout", "original_timeout")
def __init__(self, timeout: float):
self.timeout = timeout
self.original_timeout = socket.getdefaulttimeout()
def __enter__(self):
socket.setdefaulttimeout(self.timeout)
def __exit__(self, *_):
socket.setdefaulttimeout(self.original_timeout)