Each call to asyncio.run()
is spawning a different event loop. You need to put all your code into the same loop:
import asyncio
import telnetlib3
async def main():
reader, writer = await telnetlib3.open_connection('localhost', 2300)
writer.write("$SYS,INFO")
reply = await reader.read()
print('reply:', reply)
asyncio.run(main())
Note that read()
is a blocking method; if the connection isn't closed after the reply is sent, it may end up blocking indefinitely. You can read character-by-character instead, if there is a specific delimiter that marks the end of the reply. E.g., if we expect the reply to be terminated with an EOL character:
import asyncio
import telnetlib3
async def main():
reader, writer = await telnetlib3.open_connection('localhost', 2300)
writer.write("$SYS,INFO")
reply = []
while True:
c = await reader.read(1)
if not c:
break
if c in ['\r', '\n']:
break
reply.append(c)
print('reply:', ''.join(reply))
asyncio.run(main())
As for "how do I create a class out of it?", I'm not sure how to answer that. The example above could be written like:
import asyncio
import telnetlib3
class Communicator:
def __init__(self, host, port):
self.host = host
self.port = port
async def connect(self):
self.reader, self.writer = await telnetlib3.open_connection(self.host, self.port)
async def get_info(self):
self.writer.write("$SYS,INFO")
reply = []
while True:
c = await self.reader.read(1)
if not c:
break
if c in ['\r', '\n']:
break
reply.append(c)
return ''.join(reply)
async def main():
conn = Communicator('localhost', 2300)
await conn.connect()
print('info:', await conn.get_info())
asyncio.run(main())
That's not necessarily a great example, but it works. There are additional examples in the telnetlib3
documentation that may also be of interest.