Python Telnetlib 连接超时问题
我正在尝试写一个脚本,通过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)
1 个回答
4
你可能没有使用 Python 2.6 或更高版本。文档中有这么一句:
在 2.6 版本中:增加了超时功能。