guru
guru

Reputation: 309

Python telnet connection refuse exception

I'm using following function to make telnet connection verification

telnetlib.Telnet("172.28.5.240", "8080")

When the connection refused it shows exception message. Is it possible to hide the message and detect as success or failed through if condition?

Upvotes: 1

Views: 4464

Answers (3)

Oleg Tsittser
Oleg Tsittser

Reputation: 1

None of the options helped me. Maybe someone will come in handy. 100% working version: Used to check the availability of the RDP server in ZABBIX:

import telnetlib
response = ''
HOST = '192.168.1.201'
PORT = 3389
tn = telnetlib.Telnet()
try:
    tn.open(HOST, PORT, 3)
    response = '2'
except Exception:
    response = '0'
finally:
    tn.close()
    print(response)

Upvotes: 0

guru
guru

Reputation: 309

Based on Suku's answer I develop my code. that is a working answer. And following is my script for reference.

try:
    conn = telnetlib.Telnet("172.28.5.240", "80")
    response = 'Success'
except:
    response = 'Failed'
finally:
    print response

Upvotes: 0

Suku
Suku

Reputation: 3880

You can use try-except-finally blocks

 try:
     #
     #
     response = 'Success'
 except:
     response = 'Failed'
 finally:
     print response

Upvotes: 2

Related Questions