net
This library is considered stable for internal use only.
Utility functions for network related operation.
syntax:
net.INN
It is string "INN" that represents a internal ip.
syntax:
net.PUB
It is string "PUB" that represents a public ip.
syntax:
net.NetworkError
Super class of all network exceptions.
syntax:
net.IPUnreachable
Exception for an unreachable ip.
syntax:
net.choose_ips(ips, ip_type=None)
arguments:
-
ips
: is a list of ips to choose from. -
ip_type
:net.PUB
: returns a list of public ip fromips
.net.INN
: returns a list of internal ip fromips
.None
: returns the original list.- Other value: raise
ValueError
.
return: list of chosen ips.
syntax:
net.choose_by_idc(dest_idc, my_idc, ip_list)
Return a new list with all public ips in ip_list
, if dest_idc
is not
my_idc
.
Return a new list with all internal ips in ip_list
, if dest_idc
is
my_idc
.
arguments:
-
dest_idc
: is a string representing an IDC where the ips inip_list
is. -
my_idc
: is a string representing the IDC where the function is running. -
ip_list
: is a list of ip in thedest_idc
.
return:
a list of sub set of ip_list
.
syntax:
net.choose_by_regex(ip_list, regex_list)
It returns a sub set list of ip_list
.
Each ip in the result list matches at least one of the regex-es from
regex_list
.
arguments:
-
ip_list
: is a list of ipv4 addresses. Seenet.parse_ip_regex_str
. -
regex_list
: is a list of regex.
return:
a list of ipv4 addresses, in which every ip matches at least one regex from
regex_list
.
syntax:
net.choose_inn(ip_list)
Return a list of all internal ip from ip_list
.
arguments:
ip_list
: is a list of ipv4 addresses.
return: a list of internal ipv4 addresses.
syntax:
net.choose_pub(ip_list)
Return a list of all public ip from ip_list
.
arguments:
ip_list
: is a list of ipv4 addresses.
return: a list of public ipv4 addresses.
syntax:
net.get_host_devices(iface_prefix='')
Returns a dictionary of all iface, and address information those are binded to it.
{
'en0': {
'LINK': [
{ 'addr': 'ac:bc:32:8f:e5:71'}
],
'INET': [
{
'broadcast': '172.18.5.255',
'netmask': '255.255.255.0',
'addr': '172.18.5.252'
}
]
}
}
arguments:
-
iface_prefix
: is a string or''
to specify what iface should be chosen.net.get_host_devices(iface_prefix="eth")
returns onlyeth0
,eth1
etc.
return: a dictionary of iface and its address information.
syntax:
net.get_host_ip4(iface_prefix='', exclude_prefix=None)
Get ipv4 addresses on local host.
If iface_prefix
is specified, it returns only those whose iface name
starts with iface_prefix
.
If exclude_prefix
is specified, it does not return those whose iface name
starts with exclude_prefix
.
127.0.0.1
will not be returned.
arguments:
-
iface_prefix
: is a string or a list of string to specify what iface should be chosen. By default it is""
thus it returns ips of all iface.net.get_host_ip4(iface_prefix="eth0")
returns only ipv4 addresses those are binded toeth0
. -
exclude_prefix
: is a string or a list of string to specify what iface should not be chosen. By default it isNone
thus no iface is excluded.
return: a list of ipv4 addresses.
syntax:
net.ip_class(ip)
Return the class of ip
: net.PUB
or net.INN
.
return:
net.PUB
or net.INN
.
syntax:
net.ips_prefer(ip_list, preference)
Reorder ip_list
according to preference
.
- If
preference
isnet.PUB
, it returns a new list with public ips before internal ips. - If
preference
isnet.INN
, it returns a new list with internal ips before public ips.
arguments:
-
ip_list
: list of ip strings. -
preference
: is one ofnet.PUB
andnet.INN
, to specify what ip should be added into the list returned.
return:
a new list of ips in ip_list
reordered according to preference
.
syntax:
net.is_inn(ip)
Check if ip
is an internal ipv4 address.
arguments:
ip
: string of ipv4 address
return:
True
or False
syntax:
net.is_pub(ip)
Check if ip
is a public ipv4 address.
arguments:
ip
: string of ipv4 address
return:
True
or False
syntax:
net.is_ip4(ip)
It checks if ip
is a valid ipv4 string.
arguments:
ip
: string or other type data.
return:
True
if ip
is valid ipv4 address. Otherwise False
.
syntax:
net.parse_ip_regex_str(regexs)
It splits a comma separated string into a list. Each one in the result list should be a regex string.
arguments:
-
regexs
: is a comma separated string, such as:192[.]168[.],172[.]16[.]
. With this argument, it returns:['192[.]168[.]', '172[.]16[.]']
.These two regex matches all ipv4 addresses those are started with
192.168.
or172.16.
return: a list of regex string.
syntax:
net.ip_to_num(ip)
It converts the IP to 4-byte integer
return: a 4-byte integer.
syntax:
net.num_to_ip(ip_num)
It converts the 4-byte integer to IP
return: IP.
syntax:
python -m net ip
It prints to stdout all ipv4 addresses, in yaml format.
See net.get_host_ip4
.
syntax:
python -m net device
It prints to stdout all device, in yaml format.
See net.get_host_devices
.
Zhang Yanpo (张炎泼) [email protected]
The MIT License (MIT)
Copyright (c) 2015 Zhang Yanpo (张炎泼) [email protected]