[newbie] trying socket as a replacement for nc

Jean-Michel Pichavant jeanmichel at sequans.com
Mon Dec 16 14:21:15 EST 2013



----- Original Message -----
> On Tue, Dec 17, 2013 at 5:26 AM, Jean Dubois
> <jeandubois314 at gmail.com> wrote:
> >> Try something simple first:
> >> import telnetlib
> >> host = '10.128.59.63'
> >> port = 7000
> >> t = Telnet(host, port)
> >> def flush()
> >>   t.read_very_eager()
> >> def sendCmd(cmd)
> >>   t.write('%s\n' % cmd)
> >>   return flush()
> >> flush()
> >> print sendCmd('*IDN?')
> >> print sendCmd('*OPC?')
> > Still no success:
> > jean at mantec:~$ ./test.py
> >   File "./test.py", line 7
> >     def flush()
> >               ^
> > SyntaxError: invalid syntax
> >
> >
> > Tried it both with python2 and python3, same error...
> 
> Folks, the OP isn't an expert. Please test your scripts before
> posting!
> 
> I don't have everything I need to test this fully, but here's a
> variant of the above that's at least syntactically correct:
> 
> from telnetlib import *
> host = '10.128.59.63'
> port = 7000
> t = Telnet(host, port)
> def flush():
>   t.read_very_eager()
> def sendCmd(cmd):
>   t.write('%s\n' % cmd)
>   return flush()
> flush()
> print sendCmd('*IDN?')
> print sendCmd('*OPC?')
> 
> It's written for Python 2, so use that interpreter.
> 
> ChrisA

It was done on purpose, for educational purpose... :) 
My bad, however I should point that learning the very basic of a language by implementing a low level equipment remote protocol is rather ambitious.
By experience I know that you are annoyed by a crapload of nasty details without even caring about the python syntax, including:
  * LF/CR sequence
  * Inconsistent  answer pattern, depending on the equipment vendor
  * broken netcode that can block the remote server
  * timeouts
  * poor equipment feedback
I still wish Jean a great success :)


JM  


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


More information about the Python-list mailing list