2
我试图做一个脚本,它通过telnet连接到服务器。我正在使用python和telnetlib。我有问题超时工作。我想在连接到主机时使用可选超时,以便在主机不在线时收到异常。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.5.2。由于我们公司的政策,Python可能无法升级到新版本。也许我会尝试信号:http://pguides.net/python/timeout-a-function – makarhum
你也可以尝试使用更新后的telnetlib.py:http://hg.python.org/cpython/file/2.7/ Lib/telnetlib.py - 也许你需要回溯一些东西,但它应该工作 –