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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.