Hi All,
I’m quite new to Python and trying to write a Telnet automation program for home use. Each telnet device only offers 4 connections and some are already used so I dont want to use more than one connection per device. Happy to run one async function for each device feedback/reads but am struggling to understand how to share the writer/outgoing connection between async functions. Can anyone help? Below is some working code for interaction between 2 devices but I wish to run one async function for read on each device and then share the writes connection between functions (My planned design might also be impossible or poor so let me know if that is the case).
import asyncio
import re
import telnetlib3
# Regex and wildcard supported in rules Keys eg. "~D.*ICE,PadOffice,9,3"
rules = [
("~DEVICE,PadOffice,9,3", "*1,23,1")
]
tuner_host = '0.0.0.0'
tuner_port = 23
lutron_host= '0.0.0.0'
lutron_port = 23
async def main():
tuner_reader, tuner_writer = await telnetlib3.open_connection(tuner_host, tuner_port)
lutron_reader, lutron_writer = await telnetlib3.open_connection(lutron_host, lutron_port)
print ('Ready')
while lutron_reader:
response = await lutron_reader.readline()
print(response.strip())
for prompt, action in rules:
if re.fullmatch(prompt, response.strip()):
tuner_writer.writelines(action, "\n")
print('---MATCH:', action)
else:
print ('NO MATCH')
asyncio.run(main())
print ('Finished')