Hello All!
I've figured out how to telnet to my esp8266 nodemcu module and control the micropython repl cli.
However, when I try to incorporate that into a regular python script, I can connect, but executing commands throws an error. Here's what I'm seeing:
####################################################
# This is where I telnet to the esp8266 successfully from the terminal of my Mac #
###################################################
lklklk@silo1650:~/Documents/python stuff$ telnet 10.0.1.20
Trying 10.0.1.20...
Connected to 10.0.1.20.
Escape character is '^]'.
>>>
>>> import os
>>> os.listdir()
['boot.py', 'webrepl_cfg.py', 'utelnetserver.py'] - (the utelnetserver.py file is from here on github --> https://github.com/cpopp/MicroTelnetServer)
>>>
telnet> quit
Connection closed.
#####################################################
# Here, I try to telnet to the esp8266 from the python interpreter and get an error #
####################################################
lklklk@silo1650:~/Documents/python stuff$ python3
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import telnetlib
>>> tn = telnetlib.Telnet(10.0.1.20)
File "<stdin>", line 1
tn = telnetlib.Telnet(10.0.1.20)
^
SyntaxError: invalid syntax
>>> host = "10.0.1.20"
>>> tn=telnetlib.Telnet(host)
>>> tn.write("import os")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/telnetlib.py", line 287, in write
if IAC in buffer:
TypeError: 'in <string>' requires string as left operand, not bytes
>>>
Anyone else able to do this successfully?
Telnet via a python script?
Re: Telnet via a python script?
The tn.write() code you are invoking expects a bytes object. See this python3 session as an example.
So when you call tn.write("import os") the string "import os" is actually a string but is treated as a bytes buffer.
Later there is a test to see if specific bytes are in the bytes buffer if IAC in buffer:, which creates an error just like the session above.
To fix it run tn.write(b"import os") (note the b"")
Code: Select all
>>> "hell" in "hello"
True
>>> b"hell" in "hello"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'in <string>' requires string as left operand, not bytes
>>> b"hell" in b"hello"
True
So when you call tn.write("import os") the string "import os" is actually a string but is treated as a bytes buffer.
Later there is a test to see if specific bytes are in the bytes buffer if IAC in buffer:, which creates an error just like the session above.
To fix it run tn.write(b"import os") (note the b"")
Re: Telnet via a python script?
Thanks! That seems to do the trick.
Now I'm running into the issue of carriage return. I've got the module connected to a terminal session, so I can see that the tn.write(b"import os") command gets typed out. But not executed. I tried running tn.write(b"import os\n") and nothing. Not until I walk over to the terminal session and hit 'Enter' does it get executed. I've tried a few variants to see if one would work
tn.write(b"\n")
tn.write("\n")
tn.write(\n) - of course, that was an error
Nothing works
-John
Now I'm running into the issue of carriage return. I've got the module connected to a terminal session, so I can see that the tn.write(b"import os") command gets typed out. But not executed. I tried running tn.write(b"import os\n") and nothing. Not until I walk over to the terminal session and hit 'Enter' does it get executed. I've tried a few variants to see if one would work
tn.write(b"\n")
tn.write("\n")
tn.write(\n) - of course, that was an error
Nothing works
-John
Re: Telnet via a python script?
Dave! Yes! Thanks you!
Man I love how it's the little things that light me up
Man I love how it's the little things that light me up