2

I am trying to make a script, which connects to a server by telnet. I am using python and telnetlib. I have problems getting timeout to work. I want to use the optional timeout when connecting to host, so that I get an exception if the host isn't online.

I have read the python telnetlib documentation but I have no idea what's wrong with my code.

Here is my simple code:

import telnetlib

host = 'hostname'
tn = telnetlib.Telnet(host, 23, 5) # port 23, timeout 45secs
print 'Connecting to', host
tn.close()

And here is my error message:

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)
apaderno
  • 28,547
  • 16
  • 75
  • 90
makarhum
  • 68
  • 1
  • 4

1 Answers1

4

You probably aren't using python 2.6 or above. Quote from the docs:

Changed in version 2.6: timeout was added.

Otto Allmendinger
  • 27,448
  • 7
  • 68
  • 79
  • That's it! I am running python 2.5.2. Due to policy of our company Python may not be upgraded to new version. Maybe I will try signals: http://pguides.net/python/timeout-a-function – makarhum Nov 18 '11 at 07:39
  • you could also try using the the updated telnetlib.py: http://hg.python.org/cpython/file/2.7/Lib/telnetlib.py - maybe you need to backport some things but it should work – Otto Allmendinger Nov 18 '11 at 09:48