I'm using telnetlib
to communicate with some equipment which has Telnet implemented as single user.
Therefore, I have to detect and remove the telnet object created from a previous attempt to establish communication.(Ex: tn = telnetlib.Telnet(HOST)
)
My attempt is something like bellow but it does not work:
if 'tn' in loccals() or 'tn' in globals():
print "Telnet object exists. Remove it."
tn.close()
del tn
else:
print "Create a Telnet object."
global tn
tn = telnetlib.Telnet(HOST)
print tn
Don't delete, rebind. Start with tn
set to None
:
tn = None
Now you can test for the value:
if tn is not None:
tn.close()
tn = None
else:
tn = telnetlib.Telnet(HOST)
Note that the global
keyword marks a name as global in the whole scope, not just from that line onwards.