Python Glossary Русский

Python 3.3+socketfunction

socket.if_indextoname()

network interfaces

Returns the name of a network interface by its numeric index, for example 1 gives 'lo'.

Syntax

socket.if_indextoname(if_index) -> str

It helps when an index comes from elsewhere, such as the scope_id of an IPv6 address or an OS response. A missing index raises OSError instead of returning an empty string. Indexes need not be consecutive from one, because interfaces come and go, so looping over range() is pointless; if_nameindex() gives the list.

Examples

import socket
index, name = socket.if_nameindex()[0]
print(socket.if_indextoname(index) == name)  # → True
socket.if_indextoname(999999)  # → OSError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy