python - sockect.gethostbyaddr 在从文件读取的 for 循环中
问题描述
尝试通过 socket.gethostbyaddr 提供文本文件列表,我似乎陷入了数据类型问题。
当尝试遍历列表时,除了最后一个测试行上的异常错误外,我没有得到任何输出
这只是一个小脚本,用于将 ip 解析为名称(如果存在)。测试我打开的文件只是:
1.1.1.1
2.2.2.2
10.1.1.1
我的本地主机在前两行中有一个“test1”或“test2”名称的条目
只是运行 socket.gethostbyaddr('1.1.1.1') 将“test1”作为主机返回,所以我知道该部分对于测试是正确的
import subprocess
import socket
tstout = open('testout.txt','w+')
with open("testip.txt", "r") as ins:
for l in ins:
line = str(l)
try:
tstout.write (socket.gethostbyaddr(line))
except Exception as e:
print (e)
tstout.close()
subprocess.Popen([r'c:\Windows\notepad.exe',r'c:\scripts\testout.txt'])
期望输出文件将解析结果写入其中,感谢我搞砸的任何指针
简化为:
import socket
tstout = open('testout.txt','w+')
with open("testip.txt", "r") as ins:
for l in ins:
l = str(l)
# print(l)
tstout.write (socket.gethostbyaddr(l))
解决方案
推荐阅读
- c# - Visual Studio 2019 不显示错误 - 中断模式
- java - 在 java 的 pom 中找不到工件 org.codehaus.groovy:groovy-all:jar:2.5.6
- scala - 下载 net.cakesolutions:scala-kafka-client 时出错 - 未找到
- java - 用户在 if 语句中的输入
- arrays - Javascript - 删除第二个大写字符串
- elasticsearch - Logstash 不使用 docker-compose.yaml 文件读取文件输入
- javascript - 我为此自行车配置研究过哪些技术
- web-component - stenciljs:插槽问题
- mysql - 语句终止,因为数据将被截断
- jms - 我们可以让 JMS Producer 使用“AmazonSQSAsyncClient”的方法“sendMessageAsync”来发送消息吗?