设置oracle virtualbox时,如何在python中获取本地ip地址 [英] When oracle virtualbox are set, How to get local ip address in python
问题描述
-
我一直在寻找如何使用python获取本地IP地址。 (我的python版本是3.4)并且有一个答案。答案是:
I've searched to find out how to get my local ip address using python. (my python version is 3.4) And there is an answer. That answer says:
import socket
socket.gethostbyname(socket.gethostname())
它有效。脚本说我的IP地址是192.168.56.1。但我的本地(=内部)IP地址是192.168.0.100。
And it works. script said my ip address is 192.168.56.1 . But my local (=internal) ip address is 192.168.0.100 .
那么,那个IP地址是什么?这是我在一周前设置virtualbox时安装的Virtualbox Host-Only Network的IP地址。 (Virtualbox的版本是5.0)我现在有2个网络适配器,一个是'Virtualbox Host-Only Network',另一个是局域网
So, What was that ip address? It was an ip address of 'Virtualbox Host-Only Network' which was installed when I setup virtualbox a week ago. (Virtualbox's version is 5.0) I have now 2 network adapter, one is 'Virtualbox Host-Only Network' and the other is local area network
当我转向'Virtualbox网络面板中的仅限主机网络。 python脚本说我的ip地址是192.168.0.100我想要的。
When I turn that 'Virtualbox Host-Only Network' off in network panel. python script says my ip address is 192.168.0.100 which I want.
如何禁用'Virtualbox Host-Only Network',我的IP地址为'192.168.0.100' ?
(我真的不想关闭'Virtualbox Host-Only Network'。)
How to get my ip address '192.168.0.100' regardless of disabling 'Virtualbox Host-Only Network'? ( I really don't want to turn off 'Virtualbox Host-Only Network'. )
推荐答案
你可以使用名为 netifaces 的模块来获取任何网络接口的IP地址可在您的机器上使用。
You can use a module called netifaces to get the ip address of whatever network interfaces are available on your machine.
您可以使用 netifaces 最新/ installed.htmlrel =nofollow>点子如果您的系统上安装了点子。
You can install netifaces with pip if you have pip installed on your system.
pip install netifaces
Linux系统命令行的一个例子:
我的机器在 eth0
上有我的常规网络接口,在 eth0上有一个别名网络接口:0
(这类似于您的虚拟机主机网络接口,但名称会有所不同。)
My machine has my regular network interface on eth0
and an alias network interface on eth0:0
(this would be similar to your virtualbox host-only network interface, but the name would be different.)
获取列表m上可用的网络接口通过python解释器achine:
Getting a listing of the network interfaces available on your machine via the python interpreter:
(stackoverflow)[root@joeyoung.io stackoverflow]# python
>>> import netifaces
>>> netifaces.interfaces()
['lo', 'eth0', 'eth0:0']
获取有关常规网络接口的更多信息 eth0
:
Getting more information about my regular network interface eth0
:
>>> netifaces.ifaddresses('eth0')
{17: [{'broadcast': 'ff:ff:ff:ff:ff:ff', 'addr': '52:54:00:0d:4d:36'}], 2: [{'broadcast': '159.107.29.255', 'netmask': '255.255.254.0', 'addr': '159.107.28.65'}]}
我只对互联网协议地址系列感兴趣所以让我们使用 AF_INET $ c $缩小结果范围c>作为过滤器:
I'm only interested in the Internet Protocal Address Family so lets narrow down the results using AF_INET
as a filter:
>>> netifaces.ifaddresses('eth0')[netifaces.AF_INET]
[{'broadcast': '159.107.29.255', 'netmask': '255.255.254.0', 'addr': '159.107.28.65'}]
这给了我一个包含1个条目的列表。该条目包含字典。我想获取 addr
键所代表的值,以获取我的 eth0
界面的IP地址:
This gives me back a list with 1 entry. That entry contains a dictionary. I want to get the value represented by the addr
key in order to get the ip address of my eth0
interface:
>>> netifaces.ifaddresses('eth0')[netifaces.AF_INET][0]['addr']
'159.107.28.65'
159.107.28.65
是我的eth0接口的本地IP地址。
159.107.28.65
is the local IP address of my eth0 interface.
我们可以在上面执行相同的步骤,在 eth0:0
上获取我的别名IP地址:
We can do the same steps above to get my alias IP address on eth0:0
as well:
>>> netifaces.ifaddresses('eth0:0')
{2: [{'broadcast': '192.168.13.255', 'netmask': '255.255.255.0', 'addr': '192.168.13.2'}]}
>>> netifaces.ifaddresses('eth0:0')[netifaces.AF_INET]
[{'broadcast': '192.168.13.255', 'netmask': '255.255.255.0', 'addr': '192.168.13.2'}]
>>> netifaces.ifaddresses('eth0:0')[netifaces.AF_INET][0]['addr']
'192.168.13.2'
这篇关于设置oracle virtualbox时,如何在python中获取本地ip地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!