In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.