简体   繁体   中英

How to get the host's IP address using python?

I'm trying to get a host's ip address using this code:

import os
import socket
ip = socket.gethostbyname(os.environ['COMPUTERNAME'])

the problem is that this host has VMware installed and the IP I get with the code above is that belonging to VMware, and not to the host. How can I fix this without uninstalling VMware? :P

如果您使用的是 Windows,则可以进行此操作

iphost = socket.gethostbyname(socket.gethostname())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM