我正在尝试制作一个脚本,通过 telnet 连接到服务器。我正在使用 python 和 telnetlib。我在超时工作方面遇到问题。我想在连接到主机时使用可选的超时,这样如果主机不在线,我就会收到异常。
我已阅读 python telnetlib 文档,但我不知道我的代码有什么问题。
这是我的简单代码:
import telnetlib
host = 'hostname'
tn = telnetlib.Telnet(host, 23, 5) # port 23, timeout 45secs
print 'Connecting to', host
tn.close()
这是我的错误消息:
Exception exceptions.AttributeError: "Telnet instance has no attribute 'sock'" in <bound method Telnet.__del__ of <telnetlib.Telnet instance at 0x7f269864b8c0>> ignored
Traceback (most recent call last):
File "test2.py", line 7, in <module>
tn = telnetlib.Telnet(host, 23, 5)
TypeError: __init__() takes at most 3 arguments (4 given)
最佳答案
您可能没有使用 python 2.6 或更高版本。引用文档:
Changed in version 2.6: timeout was added.
关于Python Telnetlib和连接超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8167453/