简体   繁体   English

Python解析器从文本文件中的主机名解析IP地址

[英]Python Parser to resolve IP address from hostnames in text file

I'm attempting to resolve the hostnames I have in a text file to IPs using sockets. 我正在尝试使用套接字将文本文件中的主机名解析为IP。

file.txt : file.txt

google.com
yahoo.com
iliketurtles.com

import socket
with open("file.txt", "r") as ins:
  for line in ins:
    print  socket.gethostbyname(line.strip())

I'm getting the following gaierror: 我收到以下gaierror:

print socket.gethostbyname(line.strip()) socket.gaierror: [Errno 8] nodename nor servname provided, or not known 打印socket.gethostbyname(line.strip())socket.gaierror:[错误号8]提供的节点名或服务名,或者未知

What do I need to do differently to resolve the hostname on each line of the file and print to standard output? 我需要采取其他措施来解析文件每一行上的主机名并打印到标准输出吗?

Thanks in advance! 提前致谢!

This code works for me. 该代码对我有用。 Sounds like it's a network issue. 听起来像是网络问题。 Check your internet connection - try to ping this servers and check that your DNS server works correctly. 检查您的互联网连接-尝试ping此服务器并检查您的DNS服务器是否正常运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 需要帮助来创建一个Python脚本,该脚本从txt文件中读取主机名,对主机名执行ping操作,解析IP并将其打印到另一个文本文件 - Need help creating a Python script that reads hostnames from a txt file, pings the hostname, resolves the IP and prints it to another text file 如何在 Python 中从此文本文件中获取接口名称和 IP 地址 - How to get interface name and IP address from this text file in Python 使用python从文本文件解析IP地址/网络 - IP address/network parsing from text file using python 从文本文件中提取IP地址并将其用作Python中的输入 - extracting IP address from text file and use them as input in Python 从python中的文本文件读取/ping多个IP地址的问题 - Issue with reading/pinging multiple ip address from text file in python 删除文本文件中的ip地址并替换为python中的随机ip地址 - Remove ip address in a text file and replace it with the random ip address in python Python帮助从文本文件中获取IP地址范围,然后输出该范围内的每个IP地址 - Python help to take in a range of IP addresses from a text file and then output each ip address in the range Python中本地网络的IP地址/主机名列表 - List of IP addresses/hostnames from local network in Python 读取主机名列表并解析为 IP 地址 - Read a list of hostnames and resolve to IP addresses Python Email 解析器未从文本文件中读取 - Python Email Parser not reading from text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM