Replies: 4 comments 2 replies
-
Good options are to use asynctelnet or telnetlib3, since those are non-blocking async implementation. You could start with a standalone program to make sure it works before trying it with An alternative is to use the older standard (now deprecated) telnetlib, but it blocks on I/O. So the only way to safely use that in |
Beta Was this translation helpful? Give feedback.
-
It looks like you need to install On your code, you shouldn't be manipulating the event loop directly - If you just want to write a client to connect to a tcp port, it might be simplest to just use a tcp socket. The documentation has this example: import asyncio
Reader, Writer = None, None
@time_trigger('startup')
def do_client_connection():
global Reader, Writer
Reader, Writer = asyncio.open_connection('127.0.0.1', 23)
def client_send(message):
if not Writer:
raise("Client is not connected")
Writer.write(message.encode())
Writer.drain()
return Reader.readline().decode() This initiates the connection at startup, and provides function you can call that writes and reads from the socket. If you want to continually listen to the socket, you can do that in the startup function: import asyncio
@time_trigger('startup')
def client_connect_and_listen():
reader, writer = asyncio.open_connection('127.0.0.1', 23)
while True:
outp = await reader.read(1024)
writer.write('OUT{output}FR{input}\r\n')
# display all server output
# print(outp, flush=True) |
Beta Was this translation helpful? Give feedback.
-
Yes, the I/O functions expect Writer.write('OUT{output}FR{input}\r\n'.encode()) Like many things in Python, there are several ways to do this (eg: |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot for explanations so I modified it to be like that:
Unhappy when I run the script with the 2 arguments it doesn't send the command at device (device doesn't react) and I get an error in logs:
I got also sometimes that extra error:
It looks like it's not able to connect at device no ? although it should be able to connect at it without any problems ! |
Beta Was this translation helpful? Give feedback.
-
Hi
I'm just starting with pyscript and Python too :D I need to control a device by connecting through telnet to it and sending a command with 2 parameters (input and output).
I have seen I can get it show like a service in HA so I started it like that but is there a way to do a telnet in pyscript ?
Thanks for suggestions
Vincèn
Beta Was this translation helpful? Give feedback.
All reactions