[Python] How to get the physical interface IP address from an interface

Discussão em 'Python' iniciado por Stack, Maio 8, 2025.

  1. Stack

    Stack Membro Participativo

    What I have done so far, using PyQt classes:

    all_Addresses = QNetworkInterface.allAddresses() #list-of-QHostAddress

    for addr in all_Addresses:
    print(addr.toString())


    Output:

    172.16.0.186 - Virtual Interface IP address
    192.168.10.2 - Physical interface IP address. I want this one.
    127.0.0.1


    Using socket:

    import socket
    print(socket.gethostbyname(socket.gethostname()))


    Output:

    172.16.0.186 - When openVPN is on
    192.168.10.2 - When its off

    1. Is there a way to distinguish between them?
    2. Can this be done with ordinary Python, instead of using PyQt classes?
    3. How can I get the IPv6 address as well?

    Continue reading...

Compartilhe esta Página