Python 3.3+socketfunction
socket.if_nametoindex()
network interfaces
Returns the numeric index of a network interface by its name, for example 'lo' gives 1.
Syntax
socket.if_nametoindex(if_name) -> int
Indexes are assigned by the OS and change after a reboot or when an adapter is plugged in, so store the name and look up the index with this function at start-up. An unknown name raises OSError rather than returning 0. On Windows the name must be in the 'ethernet_32770' form that if_nameindex() reports.
Examples
import socket
index, name = socket.if_nameindex()[0]
print(socket.if_nametoindex(name) == index) # → True
socket.if_nametoindex('nosuch0') # → OSError: no interface with this name
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy