简体   繁体   English

如何使用python获取主机的IP地址?

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

I'm trying to get a host's ip address using this code:我正在尝试使用以下代码获取主机的 IP 地址:

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.问题是这台主机安装了VMware,我用上面的代码得到的IP是属于VMware的,而不是主机的。 How can I fix this without uninstalling VMware?如何在不卸载 VMware 的情况下解决此问题? :P :P

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python中从其IP地址查找主机的FQDN - How to find a host's FQDN from its IP address in Python 如果使用python / scapy知道主机的Mac地址,如何获取主机的IP地址 - how to get IP adress of a Host if its Mac address is known using python / scapy 如何使用python从主机为vm分配IP地址? - How to assign an ip address to a vm from the host with using python? Python - 在运行相同应用程序的网络上获取计算机的IP地址和主机名 - Python - Get computer's IP address and host name on network running same application 如何使用 Python 3.x 获取网站的 IP 地址? - How do I get a website's IP address using Python 3.x? 如何使用Python在Docker容器中获取主机ip? - How can I get the host ip in a docker container using Python? 如何使用python从日志文件中的ip地址列表中获取唯一的ip地址? - how to get unique ip address from list of ip address present in log file using python? 如何使用python Bottle框架获取客户端IP地址 - How to get client IP address using python bottle framework 如何使用python获取请求机器的IP地址(On Sanic) - How to get the ip address of a request machine using python (On Sanic) 如何在不使用请求的情况下在python中获取IP地址的时区 - How to get the timezone of an IP Address in python without using requests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM