Hello,

I am new to python. I have been trying to connect to telnet and feed some data from a server. Its working properly. I used the connectionObj.read_until() method to read the data from the server and made the connection as

self.conn = telnetlib.Telnet(so.telnetHost, so.telnetPort)

thats too fine.

But in case of any network failure it throws an error as follows,

Error (10054, 'Connection reset by peer')

How can I overcome this error or how can I reconnect to the server when recovering the network connection?
Any help would be appreciated.

Thanks & Regards,
Sreejith

You can do something like

for myCommand in myCommandList:
    try:
        self.conn.telnet(myCommand) # I don't know the exact syntax 
                             # but you'll know that easily
    except theTelnetExceptionRaised:
        self.conn = telnetlib.Telnet(so.telnetHost, so.telnetPort)

This should work ok

Hi,

Thanks for the reply.

More than handling the exception by showing some custom messages, I want to reconnect to the telnet service. Actually the problem occurs in case of network failure where the exception is unavoidable and if system regain the connection then how can I check the connection and reconnect to the telnet to continue feeding the data from the telnet server without restarting the service or without any interaction from me?

Thanks & Regards,
Sreejith

Sorry to answer that late.
I don't understant how you can't reach an exception...
You certainly have, in the telnetlib module (maybe something like telnetlib.Telnet.EOFError

I don't use telnet in python but i use a little ftp. I imagine it is the same idea.
In my previous post, the exception handling was not to display message but to reconnect (self.conn = telnetlib.Telnet(so.telnetHost, so.telnetPort)). Reconnection is exactly the same as connection.

The idea is to loop until your commands are all executed.
When one fails because of a net failure (maybe recognised in the exception type), You can try to reconnect (self.conn = telnetlib.Telnet(so.telnetHost, so.telnetPort)) in a loop process with a timer (every 10 s for example).

You may be interested by this post :
http://www.velocityreviews.com/forums/t348486-how-to-know-if-connection-is-active-when-using-telnetlib.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.