You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
telnetlib module broken by str to unicode conversion #47975
>>> 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
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)
Activity
hdima commentedon Aug 29, 2008
Simple example:
hdima commentedon Aug 29, 2008
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.
vstinner commentedon Oct 14, 2008
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)
benjaminp commentedon Oct 14, 2008
The patch looks pretty straightforward to me. If somebody else gives
their nod, I'll apply it.
amauryfa commentedon Oct 14, 2008
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.
hdima commentedon Oct 15, 2008
The patch is good. It's exactly what I told about in msg72132.
benjaminp commentedon Oct 15, 2008
Fixed in r66904.