k3net
Network utilities for IP address detection and classification. Get host IP addresses, classify public/private IPs.
k3net is a component of pykit3 project: a python3 toolkit set.
Installation
pip install k3net
Quick Start
import k3net
# Get host IP addresses
ips = k3net.get_host_ip4()
print(ips) # ['192.168.1.100', '10.0.0.1']
# Check if IP is public or private
print(k3net.is_pub('8.8.8.8')) # True
print(k3net.is_inn('10.0.0.1')) # True
# Classify IP
print(k3net.ip_class('192.168.1.1')) # 'INN'
print(k3net.ip_class('8.8.8.8')) # 'PUB'
# Convert IP to number and back
num = k3net.ip_to_num('192.168.1.1')
ip = k3net.num_to_ip(num)
API Reference
k3net
PUB = 'PUB'
module-attribute
It is string "INN" that represents a internal ip.
IPUnreachable
Bases: NetworkError
Exception for an unreachable ip.
Source code in k3net/net.py
33 34 35 36 37 38 | |
InvalidIP4
Bases: Exception
Exception for an invalidIP4 ip.
Source code in k3net/net.py
41 42 43 44 45 46 | |
InvalidIP4Number
Bases: Exception
Exception for an invalidIP4 number.
Source code in k3net/net.py
49 50 51 52 53 54 | |
NetworkError
Bases: Exception
Super class of all network exceptions.
Source code in k3net/net.py
25 26 27 28 29 30 | |
choose_by_idc(dest_idc, local_idc, ips)
net.choose_by_idc(dest_idc, my_idc, ip_list)
:param dest_idc: is a string representing an IDC where the ips in ip_list is.
:param local_idc: is a string representing the IDC where the function is running.
:param ips: is a list of ip in the dest_idc.
:return: a list of sub set of ip_list.
Source code in k3net/net.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |
choose_by_regex(ips, ip_regexs)
net.choose_by_regex(ip_list, regex_list)
:param ips: is a list of ipv4 addresses.
:param ip_regexs: is a list of regex.
:return: a list of ipv4 addresses, in which every ip matches at least one regex from regex_list.
Source code in k3net/net.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | |
choose_inn(ips)
Return a list of all internal ip from ip_list.
:param ips: is a list of ipv4 addresses.
:return: a list of internal ipv4 addresses.
Source code in k3net/net.py
171 172 173 174 175 176 177 | |
choose_ips(ips, ip_type=None)
:param ips: is a list of ips to choose from. :param ip_type:
net.PUB: returns a list of public ip from ips.
net.INN: returns a list of internal ip from ips.
None: returns the original list.
Other value: raise ValueError.
:return: list of chosen ips.
Source code in k3net/net.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
choose_pub(ips)
Return a list of all public ip from ip_list.
:param ips: is a list of ipv4 addresses.
:return: a list of public ipv4 addresses.
Source code in k3net/net.py
162 163 164 165 166 167 168 | |
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'
}]
}
}
:param iface_prefix: is a string or '' to specify what iface should be chosen.
:return: a dictionary of iface and its address information.
Source code in k3net/net.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
get_host_ip4(iface_prefix=None, 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.
:param 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.
:param exclude_prefix: is a string or a list of string to specify what iface should not be
chosen.
By default it is None thus no iface is excluded.
:return: a list of ipv4 addresses.
Source code in k3net/net.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
ip_class(ip)
Return the class of ip: net.PUB or net.INN.
:param ip:
:return: net.PUB or net.INN.
Source code in k3net/net.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
ip_to_num(ip_str)
It converts the IP to 4-byte integer :param ip_str: ip :return: a 4-byte integer.
Source code in k3net/net.py
370 371 372 373 374 375 376 377 378 379 | |
ips_prefer(ips, preference)
Reorder ip_list according to preference.
If preference is net.PUB, it returns a new list with public ips before
internal ips.
If preference is net.INN, it returns a new list with internal ips
before public ips.
:param ips: list of ip strings.
:param preference: is one of net.PUB and net.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.
Source code in k3net/net.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
is_inn(ip)
Check if ip is an internal ipv4 address.
:param ip: string of ipv4 address
:return: True or False
Source code in k3net/net.py
130 131 132 133 134 135 136 | |
is_ip4(ip)
It checks if ip is a valid ipv4 string.
:param ip: string or other type data.
:return: True if ip is valid ipv4 address. Otherwise False.
Source code in k3net/net.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
is_pub(ip)
Check if ip is a public ipv4 address.
:param ip: string of ipv4 address
:return: True or False
Source code in k3net/net.py
121 122 123 124 125 126 127 | |
num_to_ip(ip_num)
It converts the 4-byte integer to IP :param ip_num: :return: IP.
Source code in k3net/net.py
382 383 384 385 386 387 388 389 390 391 392 393 | |
parse_ip_regex_str(ip_regexs_str)
It splits a comma separated string into a list.
Each one in the result list should be a regex string.
:param ip_regexs_str: 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. or 172.16.
:return: a list of regex string.
Source code in k3net/net.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | |
License
The MIT License (MIT) - Copyright (c) 2015 Zhang Yanpo (张炎泼)