如何在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.
但是,当我在Google上搜索时,我会不断找到以下代码:
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,但是无论哪种方式都不是我想要的.当我使用Google时,我一直使用相同的语法, 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"
您还可以通过以下方式获取所有可用接口的列表
You can also get a list of all available interfaces via
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屋!