[Tutor] Looking for some comments on my code segment...

dn PythonList at DancesWithMice.info
Fri Feb 24 15:14:30 EST 2023


On 24/02/2023 16.14, Dave (NK7Z) wrote:
> Hi,
> New to Python programming, (this is part of my first Python program, 
> beyond hello world), and I am working on some telnet stuff...

Welcome!

It is quite a BIG step to go from 'hello world' to simulating telnet! 
However, picking a topic which 'floats your boat' is good motivation for 
learning!


> 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....

This seems to be more of a question about the protocol than Python.
(and it's a somewhat specialised topic/little-corner of the PSL)

If you want to cover all the possibilities, then consider what each of 
the calls to library-methods might return.


> 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..

Which is as good a judgement as any other.

What tests are you using to prove the code?


> Here is the function for that:
> 
> def createtelnet():
>      global tn, failcode

Why globals, cf passing these in as parameters/arguments and/or 
return-ing the values?


>      try:
>          tn = telnetlib.Telnet(HOST, PORT, TIMEOUT)

(following-on from the above) where are these constants coming-from?


>          logdata = "Telnet object created"
>          logit(logdata)

Is "logdata" used later?
If not, how about:

     logit( "Telnet object created" )


>      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.

What is the purpose/use of failcode?


> 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...

Good to see use of logging.
(we are talking about the logging-library, aren't we?)

What is exitscript(), and what is its purpose?
Could the calling routine wrap the call in its own try-except?

-- 
Regards,
=dn


More information about the Tutor mailing list