Using python 3.8.10 and telnetlib I want to connect to a host and just print out the message the host is sending upon connection. I have been trying the following code:
import telnetlib3
import asyncio
async def run_telnet_session():
total_output = ""
# Establish a Telnet connection with timeout
reader, writer = await telnetlib3.open_connection("192.168.200.10", 9000)
try:
# Read the Telnet output
while True:
output = await reader.read(4096)
if output:
print(output)
total_output += output
else:
break
finally:
writer.close()
await writer.wait_closed()
return total_output
output = asyncio.run(run_telnet_session())
print(output)
and I get the expected output, but the code is blocked! The function never returns. How to change the code so there is a timeout and the function returns the string?