【问题标题】:Why do i get this error:gaierror: [Errno 11001] getaddrinfo failed为什么我会收到此错误:gaierror: [Errno 11001] getaddrinfo failed
【发布时间】:2018-02-01 00:11:45
【问题描述】:

如果我手动将 ips 放入列表中,则脚本正在运行,如果我尝试从 txt 文件中读取它们,则会收到错误:gaierror: [Errno 11001] getaddrinfo failed

这是我的代码:

import telnetlib
from __future__ import with_statement 

file = open('ips.txt', 'r')
HOST = file.readlines()
print HOST


user = "root"
password = "root"

for i in range(len(HOST)):
    tn = telnetlib.Telnet(HOST[i])


    tn.read_until("login: ")
    tn.write(user + "\n")
    tn.read_until("Password: ")
    tn.write(password + "\n")
    tn.write("show slot info\n")
    tn.write("exit\n")
    string = str(tn.read_all())
    print string

    for line in string.splitlines():
        if line.startswith('Temperature:'):
            Temperature = line[34:36]
            print Temperature

【问题讨论】:

  • 请正确格式化您的代码。
  • 还有,HOST[i] 中有什么?您是否尝试过使用 .strip() 删除换行符?
  • 可能你传递的HOST无法解析
  • 所以如果我使用:HOST=["IP1", "IP2"] 脚本工作得很好。如果我尝试从 txt 或 csv 文件获取 HOST,我会收到这些错误。

标签: python sockets


【解决方案1】:

您可能需要从主机中删除换行符; readlines() 返回整行,包括任何换行符,因此您正在查找像 example.com\n 这样的主机。

tn = telnetlib.Telnet(HOST[i].strip())

【讨论】:

  • 好的,我做到了。现在我得到这个唯一的错误: AttributeError: 'list' object has no attribute 'strip'
  • 你做了,还是做了 HOST.strip()?您需要在剥离之前将字符串从列表中取出。
猜你喜欢
  • 2023-02-10
  • 2021-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多