Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the methods for Python to obtain the native IP address

Shulou Source: shulou.com Published: 2022-06-01 05:12:12 10月01日 Update

This article mainly introduces "what are the methods for Python to obtain the local IP address". In the daily operation, I believe that many people have doubts about the method of obtaining the native IP address by Python. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what is the way for Python to obtain the local IP address?" Next, please follow the editor to study!

1. Use a dedicated website

The public network IP is obtained.

Web site: http://myip.ipip.net

Code:

Import requestsres = requests.get ('http://myip.ipip.net', timeout=5) .textprint (res)

I prefer to use this, and you can also use it in the command window:

Curl http://myip.ipip.net

2. Use the built-in socket library

What is obtained is the LAN IP.

The import socket# function gethostname () returns the system hostname res = socket.gethostbyname (socket.gethostname ()) print (res) that is currently executing Python.

However, the above IP is not obtained from the public network, and the result is unstable (here the IP under the virtual machine is obtained), so it is not recommended to use it.

If the computer device has a route to the Internet, you can use:

Import sockets = socket.socket (socket.AF_INET, socket.SOCK_DGRAM) s.connect (("8.8.8.8", 80)) print (s.getsockname () [0])

This is the local area network IP:

You can take a look at ipconfig:

The other applies to all interfaces. It also applies to all public, private, and external IP. This method works well on Linux, Windows, and OSX.

Import socketdef extract_ip (): st = socket.socket (socket.AF_INET, socket.SOCK_DGRAM) try: st.connect (('10.255.255.255, 1)) IP = st.getsockname () [0] except Exception: IP =' 127.0.0.1 'finally: st.close () return IPprint (extract_ip ())

Note: if the local area network IP is the public network IP, then this acquisition can be considered as the public network IP.

3. Use third-party netifaces libraries

What is obtained is the LAN IP.

The netifaces module is used to provide information about the network interface and its status.

Code:

If you don't install it, install the library first.

Pip3 install netifaces

Then use:

From netifaces import interfaces, ifaddresses, AF_INETfor ifaceName in interfaces (): addresses = [I ['addr'] for i in ifaddresses (ifaceName) .setdefault (AF_INET, [{' addr':'No IP addr'}])] print (''.join (addresses))

At this point, the study on "what are the methods for Python to obtain native IP addresses" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Method address local area local area local area network public network learning code interface more website help practical effective next host information function command that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS OPPO Reno Shulou Tech Info Microsoft