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,

Recommended Answers

All 3 Replies

You can do something like

for myCommand in myCommandList:
        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


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,

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 :

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.