博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python获取主机ip_Python –从主机名获取IP地址
阅读量:2530 次
发布时间:2019-05-11

本文共 3009 字,大约阅读时间需要 10 分钟。

python获取主机ip

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

可用于从主机名获取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.7Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) [Clang 6.0 (clang-600.0.57)] on darwinType "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 sockethostname = input("Please enter website address:\n")# IP lookup from hostnameprint(f'The {hostname} IP Address is {socket.gethostbyname(hostname)}')
Python Get Ip Address Hostname
Python Get Ip Address from Hostname
Python从主机名获取IP地址

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

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

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

Output:

输出

# python3.7 ip_address.py facebook.comThe 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 block.

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

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

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

import socketimport syshostname = sys.argv[1]# IP lookup from hostnametry:    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#

Reference:

参考

翻译自:

python获取主机ip

转载地址:http://ywlzd.baihongyu.com/

你可能感兴趣的文章
vs无法加载项目
查看>>
Beanutils基本用法
查看>>
玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
查看>>
《BI那点儿事》数据流转换——百分比抽样、行抽样
查看>>
哈希(1) hash的基本知识回顾
查看>>
Leetcode 6——ZigZag Conversion
查看>>
dockerfile_nginx+PHP+mongo数据库_完美搭建
查看>>
Http协议的学习
查看>>
【转】轻松记住大端小端的含义(附对大端和小端的解释)
查看>>
设计模式那点事读书笔记(3)----建造者模式
查看>>
ActiveMQ学习笔记(1)----初识ActiveMQ
查看>>
Java与算法之(2) - 快速排序
查看>>
Windows之IOCP
查看>>
机器学习降维之主成分分析
查看>>
CTP2交易所成交回报
查看>>
WebSocket & websockets
查看>>
openssl 升级
查看>>
ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
查看>>
CVE-2010-2883Adobe Reader和Acrobat CoolType.dll栈缓冲区溢出漏洞分析
查看>>
使用正确的姿势跨域
查看>>