0

In windows 10 I want to write an async-free version of telnetlib3 with python 3.10.11. I created the code I will attach to the end of my question, which seem to work fine in creating a new telnet connection and read data. However, when I use the write method of the Telnet3 class I get the following error:

Traceback (most recent call last):
    session.write("test")
  File "C:\Users\Test\example_telnet3_sync.py", line 66, in write
    response = loop.run_until_complete(_write(self.writer, command))
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 649, in run_until_complete
    return future.result()
  File "C:\Users\Test\example_telnet3_sync.py", line 41, in _write
    writer.write(command + "\r\n")
  File "C:\Users\Test\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\telnetlib3\stream_writer.py", line 2614, in write
    self._write(self.encode(string, errors))
  File "C:\Users\Test\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\telnetlib3\stream_writer.py", line 1735, in _write
    self._transport.write(buf)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\asyncio\proactor_events.py", line 365, in write
    self._loop_writing(data=bytes(data))
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\asyncio\proactor_events.py", line 401, in _loop_writing
    self._write_fut = self._loop._proactor.send(self._sock, data)
AttributeError: 'NoneType' object has no attribute 'send'

I am not familiar with telnetlib3 or asyncio. Is there a way to fix this error?

Here is the complete code:

import asyncio
import telnetlib3

async def _open(host, port):
    reader, writer = await telnetlib3.open_connection(host, port)
    data = await asyncio.wait_for(reader.read(4096), timeout=2)
    return reader, writer, data

async def _read(reader, expected="myprompt >>"):
    reply = ""
    while True:
        data = await reader.read(4096)
        if data:
            reply += data
        if expected in reply:
            break
    return reply

async def _read_timeout(reader, timeout=2):
    try:
        return await asyncio.wait_for(_read(reader), timeout=timeout)
    except (asyncio.exceptions.TimeoutError, RuntimeError):
        print("TimeoutError while reading from telnet!")
        return None
    
async def _write(writer, command):
    writer.write(command + "\r\n")
    
class Telnet3:

    def __init__(self, host, port):
        self.host = host
        self.port = port
        self.reader = None
        self.writer = None
        self.message = ""
    
    def connect(self):
        loop = asyncio.new_event_loop()
        self.reader, self.writer, self.message = loop.run_until_complete(_open(self.host, self.port))
        loop.close()

    def read(self):
        loop = asyncio.new_event_loop()
        response = loop.run_until_complete(_read_timeout(self.reader))
        loop.close()
        return response
        
    def write(self, command):
        loop = asyncio.new_event_loop()
        loop.run_until_complete(_write(self.writer, command))
        loop.close()
    
    def write_read(self, command):
        self.write(command)
        return self.read()

if __name__ == "__main__":
    session = Telnet3("100.200.10.10", 9000)
    session.connect()
    session.write("test")
Alex
  • 41,580
  • 88
  • 260
  • 469

1 Answers1

0

Can't say for sure without testing, but I'm pretty sure that telnet.write() takes in a byte-string. Maybe Session.write(b"test\n") might be the syntax you're looking for.

Xinthral
  • 438
  • 2
  • 10
  • No that is not it. When using a byte-string I get the error `TypeError: encoding without a string argument` – Alex Jul 19 '23 at 13:59
  • It seems that `telnetlib3` converts a string already into bytes, hence the error when you provide bytes. – Alex Jul 20 '23 at 05:39