Dec-06-2022, 10:25 AM
(This post was last modified: Dec-06-2022, 10:28 AM by DeaD_EyE.
Edit Reason: Vaccuum catastrophy happened
)
Answer to wrong topic or the op has changed something?
I don't know what happened :-D
The answer is related to: How to get the IP-Address from Windows.
The easiest approach to obtain the IP-Address is:
You could use wexpect.
I don't know if this works. I think the better approach is to use
Easier is to use netifaces.
But this does not work out of the box. A compiler is required.
You could download an unofficial compiled binary here: https://www.lfd.uci.edu/~gohlke/pythonlibs/#netifaces
This is actually (2022/12) only precompiled for Python 3.4 - 3.9
Much easier and platform independent is psutil.
The names of the interfaces are not unique, and they also depend on language settings.
A network interface card could have more than one ip. This is why properties is a list. The objects in properties (the values of the resulting dict) are custom objects, where you can access the attributes like address, broadcast, .. etc.
To get more details if the interface is wireless or not, pywin32 or ctypes must be used.
If your Windows Desktop has only one NIC, you're lucky and do not require this detail.
If you know the name of the interface, you could filter for it. But then this code does not work, if you change the name.
I don't know what happened :-D
The answer is related to: How to get the IP-Address from Windows.
The easiest approach to obtain the IP-Address is:
import socket def get_ip(): return socket.gethostbyname(socket.gethostname()) print(get_ip())pexpect does not support Windows.
You could use wexpect.
I don't know if this works. I think the better approach is to use
subprocess
if you want to parse it by yourself.Easier is to use netifaces.
But this does not work out of the box. A compiler is required.
You could download an unofficial compiled binary here: https://www.lfd.uci.edu/~gohlke/pythonlibs/#netifaces
This is actually (2022/12) only precompiled for Python 3.4 - 3.9
Much easier and platform independent is psutil.
import socket import psutil def get_ipv4(): result = [] for name, properties in psutil.net_if_addrs().items(): for property in properties: if property.family == socket.AF_INET and property.ptp is None: result.append((name, property.address)) return result if __name__ == "__main__": for name, ip in get_ipv4(): print(name, "->", ip)The
property.ptp is None
if it is not a VPN connection or something else ptp based.The names of the interfaces are not unique, and they also depend on language settings.
A network interface card could have more than one ip. This is why properties is a list. The objects in properties (the values of the resulting dict) are custom objects, where you can access the attributes like address, broadcast, .. etc.
To get more details if the interface is wireless or not, pywin32 or ctypes must be used.
If your Windows Desktop has only one NIC, you're lucky and do not require this detail.
If you know the name of the interface, you could filter for it. But then this code does not work, if you change the name.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!