How does python get native IP?

Source: Internet
Author: User
Tags socket

Python has several methods to obtain the host's IP address, Python socket contains rich functions and methods to obtain the IP address information, socket object GetHostByName method can be based on the host name to obtain the local IP address, The GETHOSTBYNAME_EX method of the socket object can obtain a list of all IP addresses on this machine

One of my common uses is to use Socket.socket (). Inet_ntoa (), which is very convenient, but there is a limit to this method is to pass the network card name (such as eth0) as a parameter. So add a condition to judge, if found above method obtains IP throws an exception, that uses to adjust the shell command (ifconfig) and pipeline, the IP that wants to filter out.


Import socket

Import Fcntl

Import struct


Get host IP Address

PARAMETER ifname: The IP address of the host obtained via ' Lo ' for loopback address, via ' eth0 '


def get_ip_address (ifname= ' eth0 '):


s = socket.socket (socket.af_inet, socket. SOCK_DGRAM)

Return Socket.inet_ntoa (Fcntl.ioctl (

S.fileno (),


Struct.pack (' 256s ', ifname[:15])

) [20:24])


IPS = Os.popen ("Lang=c ifconfig | grep "inet addr" | Grep-v "" | Awk-f ":" ' {print $} ' | awk ' {print '} ' "). ReadLines ()

If Len (IPs) > 0:

return Ips[0]

Return ""

Python obtains information such as domain IP and local IP host name

#coding: GBK

Import socket

Socket.gethostbyname (' ') #获取域名对应的IP

Socket.gethostbyname (Socket.gethostname ()) #获取主机名

SOCKET.GETHOSTBYNAME_EX (Socket.gethostname ()) #根据主机名判断出IP

How to obtain an IP address under Windows

Method One

The use of dial-up Internet, there are generally a local IP and an extranet IP, using Python can easily get these two IP using gethostbyname and GETHOSTBYNAME_EX two functions can be implemented

Import socket

Localip = Socket.gethostbyname (Socket.gethostname ()) #得到本地ip

Print "Local ip:%s"%localip

IPList = SOCKET.GETHOSTBYNAME_EX (Socket.gethostname ())

For I in IPList:

If I!= localip:

print "External ip:%s"%i

Method Two

Import socket

MyName = Socket.getfqdn (Socket.gethostname ())

Myaddr = Socket.gethostbyname (my


Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.