By default, telnetlib3 uses encoding="utf8". In this mode, the shell callback receives TelnetReaderUnicode and TelnetWriterUnicode objects, which work with Python str objects.
To work with raw bytes (useful for low-level protocols like XMODEM or legacy systems), pass encoding=False to create_server or open_connection. The shell will then receive TelnetReader and TelnetWriter objects which work with bytes.
Important: Line Endings
Regardless of mode, the Telnet protocol (RFC 854) requires \r\n (CR LF) for NVT output. The write() method does not automatically convert \n to \r\n. Always use \r\n for maximum compatibility.
# Unicode Mode (Default)
async def shell(reader, writer):
writer.write("Hello, world!\r\n") # writes str
data = await reader.read(1) # returns str
# Binary Mode
async def binary_shell(reader, writer):
writer.write(b"Hello, world!\r\n") # writes bytes
data = await reader.read(1) # returns bytes
await telnetlib3.create_server(
host="127.0.0.1", port=6023,
shell=binary_shell, encoding=False
)