Created
April 5, 2017 12:26
-
-
Save cbwar/3551d3f7fd7228f24bee0e69d5a1fa34 to your computer and use it in GitHub Desktop.
Python: Get all ip addresses from computer (PyQt5)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
from PyQt5.QtNetwork import QNetworkInterface, QHostAddress | |
def get_ips(): | |
"""Get all ip addresses from computer | |
:rtype: list | |
""" | |
ip_list = [] | |
for interface in QNetworkInterface().allInterfaces(): | |
flags = interface.flags() | |
is_loopback = bool(flags & QNetworkInterface.IsLoopBack) | |
is_p2p = bool(flags & QNetworkInterface.IsPointToPoint) | |
is_running = bool(flags & QNetworkInterface.IsRunning) | |
is_up = bool(flags & QNetworkInterface.IsUp) | |
if not is_running: | |
continue | |
if not interface.isValid() or is_loopback or is_p2p: | |
continue | |
for addr in interface.allAddresses(): | |
if addr == QHostAddress.LocalHost: | |
continue | |
if not addr.toIPv4Address(): | |
continue | |
ip = addr.toString() | |
if ip == '': | |
continue | |
if ip not in ip_list: | |
ip_list.append(ip) | |
return ip_list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment