hbutils.system.network¶
- Overview:
Some useful tools for network.
hostfile¶
-
hbutils.system.network.
hostfile
() → str[source]¶ - Overview:
Get host file in this os.
- Returns:
Host file path in this os.
Note
This should be
/etc/hosts
on Linux and macOS, butc:\windows\system32\drivers\etc\hosts
on Windows.get_hosts¶
get_localhost_ip¶
is_free_port¶
-
hbutils.system.network.
is_free_port
(port: int) → bool[source]¶ - Overview:
Check if given
port
is currently not in use and able to be allocated.
- Parameters:
port – Port to be checked.
- Returns:
In use or not.
- Examples::
>>> from hbutils.system import is_free_port >>> is_free_port(22) False >>> is_free_port(80) False >>> is_free_port(8080) True >>> is_free_port(35022) True
get_free_port¶
-
hbutils.system.network.
get_free_port
(ports: Optional[Iterable[int]] = None, strict: bool = True) → int[source]¶ - Overview:
Get allocatable port inside the given
port
.
- Parameters:
ports – Range of ports to select.
strict – Strictly use the ports in
ports
. Default isFalse
, otherwise the ports not inports
will probably be used.
- Param:
A usable port.
- Raises:
OSError – Raise
OSError
when no ports can be allocated.
- Examples::
>>> from hbutils.system import get_free_port >>> get_free_port(range(22, 1060)) 1024 >>> get_free_port(range(1080, 2080, 10)) # assume that 1080 is in use 1090 >>> get_free_port(range(22, 80)) OSError: No free port can be allocated with in range(22, 80). >>> get_free_port(range(22, 80), strict=False) 44317
Note
Due to the safety consideration of OS, only ports over 1024 will be used.
telnet¶
-
hbutils.system.network.
telnet
(host, port: int, timeout: float = 5.0) → bool[source]¶ - Overview:
Perform a telnet operation on the given port and return
True
if a response is received within the timeout period, otherwise returnFalse
.
- Parameters:
host – Host, e.g.
127.0.0.1
.port – Port to test, e.g.
32768
.timeout – Maximum timeout duration in seconds.
- Returns:
Port is on or not.
wait_for_port_online¶
-
hbutils.system.network.
wait_for_port_online
(host, port: int, timeout: Optional[float] = 5, interval: float = 0.3)[source]¶ - Overview:
Wait for the given interface to be in an online state.
- Parameters:
host – Host, e.g.
127.0.0.1
.port – Port to test, e.g.
32768
.timeout – Maximum timeout duration in seconds.
None
means wait it forever.interval – Timeout for every
telnet()
operation.
- Raises:
TimeoutError – Raise this when timeout is reached and the port is still offline.
urlsplit¶
-
hbutils.system.network.
urlsplit
(url: str) → hbutils.system.network.url.SplitURL[source]¶ - Overview:
Split url into 5 parts (scheme, host, path, query and fragment).
- Parameters:
url – Original url string.
- Returns:
SplitURL
object.
- Examples::
>>> from hbutils.system import urlsplit >>> >>> sp = urlsplit('https://www.baidu.com/dslkjf/sdfhk/asdasd.png?q=1&v=kdjf&q=2#fff') >>> sp SplitURL(scheme='https', host='www.baidu.com', path='/dslkjf/sdfhk/asdasd.png', query={'q': ['1', '2'], 'v': 'kdjf'}, fragment='fff') >>> repr(sp) "SplitURL(scheme='https', host='www.baidu.com', path='/dslkjf/sdfhk/asdasd.png', query={'q': ['1', '2'], 'v': 'kdjf'}, fragment='fff')" >>> >>> sp.scheme 'https' >>> sp.host 'www.baidu.com' >>> sp.path '/dslkjf/sdfhk/asdasd.png' >>> sp.query 'q=1&v=kdjf&q=2' >>> sp.fragment 'fff' >>> >>> sp.query_dict {'q': ['1', '2'], 'v': 'kdjf'} >>> sp.path_segments ['', 'dslkjf', 'sdfhk', 'asdasd.png'] >>> sp.filename 'asdasd.png'
SplitURL¶
-
class
hbutils.system.network.
SplitURL
(url: str, scheme: str, host: str, path: str, query: str, fragment: str)[source]¶ - Overview:
Splitted url object.
-
property
filename
¶ Filename of current url, return
None
when path is empty.
-
property
path_segments
¶ Separated path segments.
-
property
query_dict
¶ Query dict.