Python netifaces: Difference between revisions
From wikinotes
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
python library to get information about network interfaces. | |||
network | |||
= Concepts = | = Concepts = | ||
Line 21: | Line 10: | ||
Internally, these various types are represented by an ID (this differs on different platforms and chip manufacturers) | Internally, these various types are represented by an ID (this differs on different platforms and chip manufacturers) | ||
* Multiple addresses can be bound to the same network interface (ip aliasing etc, ipv4 && ipv6 etc) | * Multiple addresses can be bound to the same network interface (ip aliasing etc, ipv4 && ipv6 etc) | ||
</blockquote><!-- Concepts --> | </blockquote><!-- Concepts --> | ||
= Usage = | = Usage = | ||
<blockquote> | <blockquote> | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
ip_addrs = {} | ip_addrs = {} | ||
ifaces = netifaces.interfaces() ## get a list of all interfaces ['eth0','lo','wlp3s0',...] | ifaces = netifaces.interfaces() ## get a list of all interfaces ['eth0','lo','wlp3s0',...] | ||
ipaddr_typeids = (netifaces.AF_INET, netifaces.AF_INET6) ## ids representing ipv4 or ipv6 | ipaddr_typeids = (netifaces.AF_INET, netifaces.AF_INET6) ## ids representing ipv4 or ipv6 | ||
for iface in ifaces: | for iface in ifaces: | ||
Line 45: | Line 29: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Usage --> | </blockquote><!-- Usage --> | ||
= Elements = | = Elements = | ||
<blockquote> | <blockquote> | ||
== Interfaces == | == Interfaces == | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
Line 62: | Line 44: | ||
netifaces.AF_INET ## ipv4 | netifaces.AF_INET ## ipv4 | ||
netifaces.AF_INET6 ## ipv6 | netifaces.AF_INET6 ## ipv6 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Elements --> | </blockquote><!-- Elements --> |
Latest revision as of 06:16, 19 July 2021
python library to get information about network interfaces.
Concepts
Some of this library's ackwardness is attributed to the spindly nature of the various implementations of network-interfaces.
- Network connections are made to your computer through network-interfaces.
- Network interfaces are capable of binding different types of connections (ipv4,ipv6,bluetooth,...)
Internally, these various types are represented by an ID (this differs on different platforms and chip manufacturers)
- Multiple addresses can be bound to the same network interface (ip aliasing etc, ipv4 && ipv6 etc)
Usage
ip_addrs = {} ifaces = netifaces.interfaces() ## get a list of all interfaces ['eth0','lo','wlp3s0',...] ipaddr_typeids = (netifaces.AF_INET, netifaces.AF_INET6) ## ids representing ipv4 or ipv6 for iface in ifaces: bindings[ iface ] = [] bound_addrs = netifaces.ifaddresses( 'eth0' ) ## get a list of all bindings to an address for addr_typeid in bound_addrs: if addr_typeid in ipaddr_typeids: bindings[ iface ].extend( bound_addrs[ addr_typeid ] )
Elements
Interfaces
ifaces = netifaces.interfaces() ## list of all interfaces ['eth0','lo',...] netifaces.ifaddresses( 'eth0' ) ## list of all bindings to interface { binding_typeid : [ {...}, {...} ] }Address TypeIds
netifaces.AF_LINK ## hardware addresses(MAC), bluetooth, ... netifaces.AF_INET ## ipv4 netifaces.AF_INET6 ## ipv6