[issue3725] telnetlib module broken by str to unicode conversion

STINNER Victor report at bugs.python.org
Tue Oct 14 14:08:36 CEST 2008


STINNER Victor <victor.stinner at haypocalc.com> added the comment:

I think that telnet should only use bytes (and not characters). For an 
HTTP connection, the charset is only known after parsing the HTTP 
headers. So telnet should use bytes, and your HTTP browser will 
convert bytes to characters using the charset from the HTTP headers. 
My patch only uses bytes for internal buffering and special codes 
(IAC, DONT, ENCRYPT, etc.).

Example to test the library (Starwars, telnet, ISO-8859-1):
    from telnetlib import Telnet
    from sys import stdout
    ipv4 = "towel.blinkenlights.nl"
    ipv6 = "2001:980:ffe:1::42"
    t = Telnet(ipv6, 23)
    while True:
        command = t.read_some()
        command = str(command, "ISO-8859-1")
        stdout.write(command)

Example to test the library (Google, HTTP, ASCII):
    from telnetlib import Telnet
    t = Telnet("www.google.com", 80)
    t.write(b'GET / HTTP/1.0\r\n\r\n')
    answer = t.read_all()
    answer = str(answer, "ASCII")
    print(answer)

----------
keywords: +patch
nosy: +haypo
Added file: http://bugs.python.org/file11788/telnet_bytes.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3725>
_______________________________________


More information about the Python-bugs-list mailing list