python获取主机ip_Python –从主机名获取IP地址

python获取主机ip

Python socket module can be used to get the IP address from a hostname.

Python套接字模块可用于从主机名获取IP地址。

The socket module is part of the Python core libraries, so we don’t need to install it separately.

socket模块是Python核心库的一部分,因此我们不需要单独安装它。

Python套接字模块从主机名获取IP地址 (Python Socket Module to Get IP Address from Hostname)

Python socket module gethostbyname() function accepts hostname argument and returns the IP address in the string format.

Python套接字模块的gethostbyname()函数接受主机名参数,并以字符串格式返回IP地址。

Here is a simple example in the Python interpreter to find out the IP address of some of the websites.

这是Python解释器中的一个简单示例,用于查找某些网站的IP地址。

 
# python3.7
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import socket
>>> socket.gethostbyname('journaldev.com')
'45.79.77.230'
>>> socket.gethostbyname('google.com')
'172.217.166.110'
>>> 

Note: If the website is behind a load-balancer or working in the cloud, you might get a different result for the IP address lookup.

注意 :如果网站在负载均衡器后面或在云中运行,则IP地址查找可能会得到不同的结果。

For example, try to run the above command for google.com or facebook.com. If you are not in the same location as mine (India), chances are that you will get a different IP address as output.

例如,尝试对google.com或facebook.com运行以上命令。 如果您与我的位置不同(印度),则可能会获得不同的IP地址作为输出。

找出网站IP地址的Python脚本 (Python Script to Find Out the IP Address of a Website)

Let’s look at an example where we ask user to enter a website address and then print its IP address.

让我们看一个示例,其中要求用户输入一个网站地址,然后打印其IP地址。

 
import socket
 
hostname = input("Please enter website address:\n")
 
# IP lookup from hostname
print(f'The {hostname} IP Address is {socket.gethostbyname(hostname)}')

Here is another example to pass the hostname as a command-line argument to the script. The script will find the IP address and print it.

这是将主机名作为命令行参数传递给脚本的另一个示例。 该脚本将找到IP地址并进行打印。

 
import socket
import sys
 
# no error handling is done here, excuse me for that
hostname = sys.argv[1]
 
# IP lookup from hostname
print(f'The {hostname} IP Address is {socket.gethostbyname(hostname)}')

Output:

输出 :

 
# python3.7 ip_address.py facebook.com
The facebook.com IP Address is 157.240.23.35

socket.gethostbyname()的错误方案 (Error Scenarios with socket.gethostbyname())

If the hostname doesn’t resolve to a valid IP address, socket.gaierror is raised. We can catch this error in our program using try-except block.

如果主机名不能解析为有效的IP地址,则会引发socket.gaierror 。 我们可以使用try-except块在程序中捕获此错误。

Here is the updated script with exception handling for invalid hostname.

这是更新的脚本,其中包含对无效主机名的异常处理。

 
import socket
import sys
 
hostname = sys.argv[1]
 
# IP lookup from hostname
try:
    ip = socket.gethostbyname(hostname)
    print(f'The {hostname} IP Address is {ip}')
except socket.gaierror as e:
    print(f'Invalid hostname, error raised is {e}')

Output:

输出:

 
# python3.7 ip_address.py jasjdkks.com               
Invalid hostname, error raised is [Errno 8] nodename nor servname provided, or not known
#

ReferenceSocket Module API Docs

参考 : 套接字模块API文档

翻译自: https://www.journaldev.com/39296/python-get-ip-address-from-hostname

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇