You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
the socket module should include getifaddrs because it is necessary to translate the output of if_nameindex into addresses #126713
socket.if_nameindex, socket.if_nametoindex and socket.if_indextoname all work in terms of interface names and indexes. But in order to do much of use with these values, you need to know what addresses they have, and as far as I can tell the socket module does not provide a means to do so.
This function exists in psutil, in netifaces, and there are various recipes lying around on Stack Overflow and elsewhere for accessing this function.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Reacted by Thomas Finstad Larsen, Mark A. Ziesemer and Andrey Vihrov
This is more of a general completeness issue than a specific scenario. Any time that you need to select a network interface, the user may know it under its address (but the application needs to know its name) or under its name (but the application needs to know its address). However, I will describe the specific scenario that lead me to discover it.
On a multicast socket, the sending interface is described in IPv4 as a packed address, same as the interface, via the IPPROTO_IP / IP_MULTICAST_IF socket option. However, when adding IPv6 support, and using the IPV6_MULTICAST_IF option, the sending interface must be specified via an integer interface index, such as those returned by if_nameindex.
There are no standard names for interfaces; for a notable example, BSDs tend to use lo0 for localhost and Linux tends to use lo. Therefore, when writing tests that want to bind multicast to a local interface, properly identifying such an interface involves identifying it via its addresses, which are standardized (127.0.0.1, ::1) as opposed to its name or index which are not. There is no facility in the python standard library to do this.
Activity
Zheaoli commentedon Nov 12, 2024
I am not sure the scenario you are working for. Would you mind providing more information about the API you want?
glyph commentedon Nov 12, 2024
This is more of a general completeness issue than a specific scenario. Any time that you need to select a network interface, the user may know it under its address (but the application needs to know its name) or under its name (but the application needs to know its address). However, I will describe the specific scenario that lead me to discover it.
On a multicast socket, the sending interface is described in IPv4 as a packed address, same as the interface, via the
IPPROTO_IP/IP_MULTICAST_IFsocket option. However, when adding IPv6 support, and using theIPV6_MULTICAST_IFoption, the sending interface must be specified via an integer interface index, such as those returned byif_nameindex.There are no standard names for interfaces; for a notable example, BSDs tend to use
lo0for localhost and Linux tends to uselo. Therefore, when writing tests that want to bind multicast to a local interface, properly identifying such an interface involves identifying it via its addresses, which are standardized (127.0.0.1,::1) as opposed to its name or index which are not. There is no facility in the python standard library to do this.In terms of what API is required, it's basically just this: https://github.com/twisted/twisted/blob/f10c1cf5548edf410e1a07620817f5f0aa291afe/src/twisted/internet/test/_posixifaces.py#L31-L155
On Windows, I think
Win32_NetworkAdapterConfigurationprovides similar information.Zheaoli commentedon Nov 12, 2024
I think your idea is making sense. We need a api provide full interface info like psutil do
Zheaoli commentedon Nov 12, 2024
I think we need a core dev's idea to review this idea before we design the API. I think it makes sense to add a new API .
WDYT @giampaolo ?
glyph commentedon Nov 12, 2024
Agreed; I think the stdlib's scope should just be to wrap the platform API minimally, and not to get too fancy with abstracting it.