[Tutor] Looking for some comments on my code segment...
Dave (NK7Z)
dave at nk7z.net
Thu Feb 23 22:14:51 EST 2023
Hi,
New to Python programming, (this is part of my first Python program,
beyond hello world), and I am working on some telnet stuff...
I would like to trap all errors, and think I am on to a start for this,
but need a bit of help, too new to know what all I need to look for....
In the code below I am creating a telnet object for use later on in the
program. I believe I have it correct, but I am sure I have forgotten
something. It seems to run fine..
Here is the function for that:
def createtelnet():
global tn, failcode
try:
tn = telnetlib.Telnet(HOST, PORT, TIMEOUT)
logdata = "Telnet object created"
logit(logdata)
except socket.timeout: # Times out, exit with error.
failcode = 1 # Let error handler know what is happening.
logdata = 'Unable to create telnet object, timed out'
logit(logdata)
exitscript() # Leave script.
except EOFError:
logdata = 'Timeout reached in login.'
logit(logdata)
failcode = 10
exitscript() # Leave script.
All variables are set elsewhere, and the above code seems to work fine.
What am I leaving out, or what don't I understand, looking for some
comments on my code.
Later on I am accessing the connection via:
try:
result = tn.read_until(b"\r\n", TIMEOUT) # Get a line.
if result == 'b\'\'':
logdata = 'Stream has failed...'
logit(logdata)
failcode = 7
exitscript() # Leave script.
except socket.error: # If socket error-- exit.
logdata = 'Socket Error in watchstream, at read.'
logit(logdata)
failcode = 8
exitscript() # Leave script.
except EOFError: # If time out-- exit with error.
failcode = 6 # Let error handler know what is happening.
logdata = 'Timeout reached in watchstream'
logit(logdata)
exitscript() # Leave script.
logdata = 'Telnet read succeeded, we got:\n' + str(result)
logit(logdata)
error = sys.stderr
logit(str(error))
Code to take apart the string follows this. logit(), is a function to
write a log as to what happened...
--
73, and thanks,
Dave (NK7Z)
https://www.nk7z.net
ARRL Volunteer Examiner
ARRL Technical Specialist, RFI
ARRL Asst. Director, NW Division, Technical Resources
More information about the Tutor
mailing list