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, but c:\windows\system32\drivers\etc\hosts on Windows.

get_hosts

hbutils.system.network.get_hosts() → Dict[str, str][source]
Overview:

Get hosts content in form of dictionary.

Returns:

A dictionary of the hosts file.

Examples::
>>> from hbutils.system import get_hosts
>>> get_hosts()
{'hansbug-VirtualBox': '127.0.0.1', 'localhost': '127.0.0.1'}

get_localhost_ip

hbutils.system.network.get_localhost_ip() → str[source]
Overview:

Get ip address of localhost. It will be checked in hosts, and just return 127.0.0.1 is not found.

Returns:

IP address of localhost.

Examples::
>>> from hbutils.system import get_localhost_ip
>>> get_localhost_ip()
'127.0.0.1'

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 is False, otherwise the ports not in ports 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.

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.

__repr__()[source]

Repr format of SplitURL.

__str__()[source]

Original url.

property filename

Filename of current url, return None when path is empty.

property path_segments

Separated path segments.

property query_dict

Query dict.