ISO-8859-1 or latin1 encoding not working
I installed the pip package and can't get this to print correct output:
import asyncio, telnetlib3
async def shell(reader, writer):
while True:
outp = await reader.read(1024)
print(outp, flush=True)
loop = asyncio.get_event_loop()
coro = telnetlib3.open_connection('svenskmud.com', 2046, shell=shell, encoding='ISO-8859-1')
reader, writer = loop.run_until_complete(coro)
loop.run_until_complete(writer.protocol.waiter_closed)
It shows � where there should be å,ä,ö and so forth.
A:\telnet\example.py:8: DeprecationWarning: There is no current event loop
loop = asyncio.get_event_loop()
V�lkommen till SvenskMUD, muddet p� svenska.
V�lj g�rna ett namn med anknytning till den nordiska
mytologin eller de nordiska traditionerna f�r att det
ska passa in s� bra som m�jligt.
Namn som �r engelska glosor ses som direkt opassande
och dessa karakt�rer riskerar att bli borttagna s�
fort de p�tr�ffas.
Vad heter du:
The expected output is:
Välkommen till SvenskMUD, muddet på svenska.
Välj gärna ett namn med anknytning till den nordiska
mytologin eller de nordiska traditionerna för att det
ska passa in så bra som möjligt.
Namn som är engelska glosor ses som direkt opassande
och dessa karaktärer riskerar att bli borttagna så
fort de påträffas.
Vad heter du:
It's entirely possible I do some misconfiguration, I am a new user to telnetlib3.
Add force_binary=True argument to open_connection and that will work,
In [3]:
...:
...: import asyncio, telnetlib3
...:
...: async def shell(reader, writer):
...: while True:
...: outp = await reader.read(1024)
...: print(outp, flush=True)
...:
...: loop = asyncio.get_event_loop()
...: coro = telnetlib3.open_connection('svenskmud.com', 2046, shell=shell, encoding='ISO-8859-1', force_binary=True)
...: reader, writer = loop.run_until_complete(coro)
...: loop.run_until_complete(writer.protocol.waiter_closed)
Välkommen till SvenskMUD, muddet på svenska.
Välj gärna ett namn med anknytning till den nordiska
mytologin eller de nordiska traditionerna för att det
ska passa in så bra som möjligt.
Namn som är engelska glosor ses som direkt opassande
och dessa karaktärer riskerar att bli borttagna så
fort de påträffas.
Vad heter du:
Sorry this isn't more clear. I really need to add documentation about this relationship between force_binary and encoding.
If you enable debug logging, you will see hardly nothing logged. This is because the server, (svenskmud.com), does not instigate any telnet option negotiation.
And so neither end gets far enough to negotiate BINARY support, according to RFC you cannot transmit bytes with 8-bit set high unless BINARY is negotiated, so this library is being strict about it. The server, svenskmud.com sends latin1-encoded text with 8th bit set high anyway, using the force_binary=True argument with telnetlib3 client causes it to force decoding it as latin1, anyway, "non-compliant" i guess
If the server only speaks raw bytes without negotiation of telnet protocol features, then there is no need for a telnet protocol library like this one! something to keep in mind. Use asyncio's built-in open_connection with a tcp socket, and use codecs.getincrementaldecoder('latin1') instance to decode bytes if that is the case.
If the server is capable of advanced telnet negotiation but needs the client to initiate it, though unusual it is possible, send IAC bytes through the writer, the protocol "senses" this and negotiates about them on your behalv, enable debugging logging to see it detected an whether the server is responding
writer.write(IAC + DO + BINARY)
writer.write(IAC + WILL + BINARY)
writer.write(IAC + WILL + NEW_ENVIRON)
best wishes
"Encoding" section has been added to readme in version 2.0.5 and released to pypi https://pypi.org/project/telnetlib3/