如何在 Python 中从 NIC 获取 IP 地址? [英] How can I get the IP address from NIC in Python?
问题描述
当 Unix 上的 Python 脚本发生错误时,会发送一封电子邮件.
When an error occurs in a Python script on Unix , an email is sent.
如果 IP 地址是测试服务器 192.168.100.37,我被要求在电子邮件的主题行中添加 {Testing Environment}.通过这种方式,我们可以拥有一个脚本版本和一种方法来判断电子邮件是否来自测试服务器上的混乱数据.
I have been asked to add {Testing Environment} to the subject line of the email if the IP address is 192.168.100.37 which is the testing server. This way we can have one version of a script and a way to tell if the email is coming from messed up data on the testing server.
然而,当我用谷歌搜索时,我一直在寻找这个代码:
However, when I google I keep finding this code:
import socket
socket.gethostbyname(socket.gethostname())
然而,这给了我 127.0.1.1 的 IP 地址.当我使用 ifconfig
我得到这个
However, that's giving me the IP address of 127.0.1.1. When I use ifconfig
I get this
eth0 Link encap:Ethernet HWaddr 00:1c:c4:2c:c8:3e
inet addr:192.168.100.37 Bcast:192.168.100.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:75760697 errors:0 dropped:411180 overruns:0 frame:0
TX packets:23166399 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:59525958247 (59.5 GB) TX bytes:10142130096 (10.1 GB)
Interrupt:19 Memory:f0500000-f0520000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:25573544 errors:0 dropped:0 overruns:0 frame:0
TX packets:25573544 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:44531490070 (44.5 GB) TX bytes:44531490070 (44.5 GB)
首先,我不知道它从哪里得到 127.0.1.1,但无论哪种方式,这都不是我想要的.当我用谷歌搜索时,我一直使用相同的语法,Bash 脚本或 netifaces 和我正在尝试使用标准库.
Firstly, I don't know where it got 127.0.1.1 from, but either way that's not what I want. When I google I keep coming to the same syntax, Bash scripts or netifaces and I'm trying to use standard libraries.
那么如何在Python中获取eth0的IP地址呢?
So how can I get the IP address of eth0 in Python?
推荐答案
两种方法:
您需要询问绑定到您的 eth0
接口的 IP 地址.这可以从 netifaces 包
You need to ask for the IP address that is bound to your eth0
interface. This is available from the netifaces package
import netifaces as ni
ni.ifaddresses('eth0')
ip = ni.ifaddresses('eth0')[ni.AF_INET][0]['addr']
print(ip) # should print "192.168.100.37"
您还可以通过
ni.interfaces()
方法#2(无外部包)
这是一种无需使用python包即可获取IP地址的方法:
Method #2 (no external package)
Here's a way to get the IP address without using a python package:
import socket
import fcntl
import struct
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
get_ip_address('eth0') # '192.168.0.110'
注意:通过检测 IP 地址来确定您正在使用的环境是一个非常棘手的问题.几乎所有框架都提供了一种非常简单的方法来设置/修改环境变量以指示当前环境.尝试查看您的文档.做起来应该很简单
Note: detecting the IP address to determine what environment you are using is quite a hack. Almost all frameworks provide a very simple way to set/modify an environment variable to indicate the current environment. Try and take a look at your documentation for this. It should be as simple as doing
if app.config['ENV'] == 'production':
#send production email
else:
#send development email
这篇关于如何在 Python 中从 NIC 获取 IP 地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!