Issue 3725: telnetlib module broken by str to unicode conversion

Created on 2008-08-29 12:43 by hdima, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (7) msg72131 - (view) Author: Dmitry Vasiliev (hdima) Date: 2008-08-29 12:43
Simple example:

>>> from telnetlib import Telnet
>>> t = Telnet("google.com", 80)
>>> t.write("GET / HTTP/1.1\r\n")        
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/py3k/Lib/telnetlib.py", line 280, in write
    self.sock.sendall(buffer)
TypeError: sendall() argument 1 must be string or buffer, not str
>>> t.write(b"GET / HTTP/1.1\r\n")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/py3k/Lib/telnetlib.py", line 277, in write
    if IAC in buffer:
TypeError: Type str doesn't support the buffer API
msg72132 - (view) Author: Dmitry Vasiliev (hdima) Date: 2008-08-29 13:22
I think only bytes need to be allowed for write() and read*() because of
low-level nature of Telnet. I can create a patch later.
msg74735 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2008-10-14 12:08
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)
msg74770 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-10-14 21:12
The patch looks pretty straightforward to me. If somebody else gives
their nod, I'll apply it.
msg74780 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-10-14 22:50
Yes, the patch is good.

I think that documentation (both Doc\library\telnetlib.rst and the 
docstrings in telnetlib.py) should reflect the change, at least the code 
samples.
msg74794 - (view) Author: Dmitry Vasiliev (hdima) Date: 2008-10-15 06:40
The patch is good. It's exactly what I told about in msg72132.
msg74815 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-10-15 20:54
Fixed in r66904.
History Date User Action Args 2022-04-11 14:56:38adminsetgithub: 47975 2008-10-15 20:54:40benjamin.petersonsetstatus: open -> closed
resolution: fixed
messages: + msg74815 2008-10-15 06:40:13hdimasetmessages: + msg74794 2008-10-14 22:50:50amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg74780 2008-10-14 21:12:57benjamin.petersonsetassignee: benjamin.peterson
messages: + msg74770
nosy: + benjamin.peterson 2008-10-14 12:08:35vstinnersetfiles: + telnet_bytes.patch
keywords: + patch
messages: + msg74735
nosy: + vstinner 2008-10-08 20:42:25benjamin.petersonsetpriority: critical -> release blocker 2008-08-29 13:22:48hdimasetmessages: + msg72132 2008-08-29 13:01:15amaury.forgeotdarcsetpriority: critical 2008-08-29 12:43:46hdimacreate