[issue13109] telnetlib insensitive to connection loss

2011-10-05 Thread xy zzy

New submission from xy zzy :

Using python's telnetlib I can connect and communicate with a device.

While the telnet session is active I can disconnect the network cable of the 
device.

At this point, I would expect read_until() with a timeout to throw a 
socket.error, EOFError or perhaps an IOError, but what I actually get is a null 
string.

Because I'm reading in a loop, when the cable is reconnected the device will 
resume communicating, and the program will continue.

My best guess ts that read_until() or perhaps everything except open() is 
insensitive to the loss of a connection.

--
components: IO
messages: 144972
nosy: xy.zzy
priority: normal
severity: normal
status: open
title: telnetlib insensitive to connection loss
type: behavior

___
Python tracker 
<http://bugs.python.org/issue13109>
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13109] telnetlib insensitive to connection loss

2011-10-06 Thread xy zzy

xy zzy  added the comment:

Cfrom class():

# see if we can connect to pcPart
try:
self.pcPart = telnetlib.Telnet(IP, PORT)
# clear the buffer 
for i in range(10):
self.pcPart.write('\n')
r = self.pcPart.read_until('Prompt>', 1)

except socket.error, e:
logging.debug('socket.error: %d: %s' % (e.args[0], e.args[1]))
self.pcPart.close()
self.pcPart = None

from init():

def talk(self,cmd,ret):

"""talk to the device"""

read_chars = ""

while (read_chars == ""):
try:
read_chars=""
# get to the Prompt> prompt
# logging.debug('seeking prompt')
while (read_chars != 'Prompt>'):
self.pcPart.write("\n")
raw_data = self.pcPart.read_until('Prompt>', 1).split('\n')
# logging.debug('raw_data: %i %s' % (len(raw_data), 
raw_data))
read_chars = raw_data[2] 
# logging.debug('read_chars: %s' % (read_chars))

# send the command
# logging.debug('found prompt')
cmdx = (('xyzzy:%s\n') % cmd)
self.pcPart.write(cmdx)
# logging.debug('command %s, %s' % (cmd, cmdx))
if (ret):
while ((len(raw_data) > 0) and ('{' not in read_chars)):
raw_data = self.pcPart.read_until('Prompt>', 1) 
# logging.debug('raw_data: %i %s' % (len(raw_data), 
raw_data))
try: 
read_chars = str(raw_data.split('\n\r')[1][1:-1])
except:
read_chars = ''
# logging.debug('read_chars: %s' % (read_chars))
else:
raw_data = self.pcPart.read_until('Prompt>', 1)
# logging.debug('ret read: %s' % (raw_data))
read_chars = '@'
return read_chars

except IndexError, e:
logging.debug('IndexError: %d: %s' % (e.args[0], e.args[1]))
traceback.print_exc(file=open(LOG_FILENAME, 'a'))
read_chars = '@'
time.sleep(1)

except (IOError, socket.error), e:
logging.debug('socket.error: %d: %s' % (e.args[0], e.args[1]))
traceback.print_exc(file=open(LOG_FILENAME, 'a'))
self.pcPart.close()
self.pcPart = None
logging.debug('reconnecting...')
self.pcPart = telnetlib.Telnet(IP, PORT) 
# clear the buffer 
for i in range(10):
self.pcPart.write('\n')
r = self.pcPart.read_until('Prompt>', 1)
read_chars = '@'
logging.debug('reconnected')

# clear the buffer 
for i in range(2):
self.pcPart.write('\n')
r = self.pcPart.read_until('Prompt>', 1)
# logging.debug('Data Read: ' + read_chars)
return read_chars  

called from:

DATA = self.talk('cmd', True)
logging.debug('talk: %s' % (DATA))

--

___
Python tracker 
<http://bugs.python.org/issue13109>
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com