Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3de7fb8

Browse files
committed
Victor Stinner's patch to make telnetlib use bytes 3725
1 parent 33b6450 commit 3de7fb8

3 files changed

Lines changed: 110 additions & 108 deletions

File tree

Doc/library/telnetlib.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,14 @@ A simple example illustrating typical use::
225225

226226
tn = telnetlib.Telnet(HOST)
227227

228-
tn.read_until("login: ")
229-
tn.write(user + "\n")
228+
tn.read_until(b"login: ")
229+
tn.write(user.encode('ascii') + b"\n")
230230
if password:
231-
tn.read_until("Password: ")
232-
tn.write(password + "\n")
231+
tn.read_until(b"Password: ")
232+
tn.write(password.encode('ascii') + b"\n")
233233

234-
tn.write("ls\n")
235-
tn.write("exit\n")
234+
tn.write(b"ls\n")
235+
tn.write(b"exit\n")
236236

237237
print(tn.read_all())
238238

Lib/telnetlib.py

Lines changed: 102 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
>>> from telnetlib import Telnet
99
>>> tn = Telnet('www.python.org', 79) # connect to finger port
10-
>>> tn.write('guido\r\n')
10+
>>> tn.write(b'guido\r\n')
1111
>>> print(tn.read_all())
1212
Login Name TTY Idle When Where
1313
guido Guido van Rossum pts/2 <Dec 2 11:10> snag.cnri.reston..
@@ -19,7 +19,7 @@
1919
2020
It is possible to pass a Telnet object to select.select() in order to
2121
wait until more data is available. Note that in this case,
22-
read_eager() may return '' even if there was data on the socket,
22+
read_eager() may return b'' even if there was data on the socket,
2323
because the protocol negotiation may have eaten the data. This is why
2424
EOFError is needed in some cases to distinguish between "no data" and
2525
"connection closed" (since the socket also appears ready for reading
@@ -47,87 +47,87 @@
4747
TELNET_PORT = 23
4848

4949
# Telnet protocol characters (don't change)
50-
IAC = chr(255) # "Interpret As Command"
51-
DONT = chr(254)
52-
DO = chr(253)
53-
WONT = chr(252)
54-
WILL = chr(251)
55-
theNULL = chr(0)
56-
57-
SE = chr(240) # Subnegotiation End
58-
NOP = chr(241) # No Operation
59-
DM = chr(242) # Data Mark
60-
BRK = chr(243) # Break
61-
IP = chr(244) # Interrupt process
62-
AO = chr(245) # Abort output
63-
AYT = chr(246) # Are You There
64-
EC = chr(247) # Erase Character
65-
EL = chr(248) # Erase Line
66-
GA = chr(249) # Go Ahead
67-
SB = chr(250) # Subnegotiation Begin
50+
IAC = 255 # "Interpret As Command"
51+
DONT = 254
52+
DO = 253
53+
WONT = 252
54+
WILL = 251
55+
theNULL = 0
56+
57+
SE = 240 # Subnegotiation End
58+
NOP = 241 # No Operation
59+
DM = 242 # Data Mark
60+
BRK = 243 # Break
61+
IP = 244 # Interrupt process
62+
AO = 245 # Abort output
63+
AYT = 246 # Are You There
64+
EC = 247 # Erase Character
65+
EL = 248 # Erase Line
66+
GA = 249 # Go Ahead
67+
SB = 250 # Subnegotiation Begin
6868

6969

7070
# Telnet protocol options code (don't change)
7171
# These ones all come from arpa/telnet.h
72-
BINARY = chr(0) # 8-bit data path
73-
ECHO = chr(1) # echo
74-
RCP = chr(2) # prepare to reconnect
75-
SGA = chr(3) # suppress go ahead
76-
NAMS = chr(4) # approximate message size
77-
STATUS = chr(5) # give status
78-
TM = chr(6) # timing mark
79-
RCTE = chr(7) # remote controlled transmission and echo
80-
NAOL = chr(8) # negotiate about output line width
81-
NAOP = chr(9) # negotiate about output page size
82-
NAOCRD = chr(10) # negotiate about CR disposition
83-
NAOHTS = chr(11) # negotiate about horizontal tabstops
84-
NAOHTD = chr(12) # negotiate about horizontal tab disposition
85-
NAOFFD = chr(13) # negotiate about formfeed disposition
86-
NAOVTS = chr(14) # negotiate about vertical tab stops
87-
NAOVTD = chr(15) # negotiate about vertical tab disposition
88-
NAOLFD = chr(16) # negotiate about output LF disposition
89-
XASCII = chr(17) # extended ascii character set
90-
LOGOUT = chr(18) # force logout
91-
BM = chr(19) # byte macro
92-
DET = chr(20) # data entry terminal
93-
SUPDUP = chr(21) # supdup protocol
94-
SUPDUPOUTPUT = chr(22) # supdup output
95-
SNDLOC = chr(23) # send location
96-
TTYPE = chr(24) # terminal type
97-
EOR = chr(25) # end or record
98-
TUID = chr(26) # TACACS user identification
99-
OUTMRK = chr(27) # output marking
100-
TTYLOC = chr(28) # terminal location number
101-
VT3270REGIME = chr(29) # 3270 regime
102-
X3PAD = chr(30) # X.3 PAD
103-
NAWS = chr(31) # window size
104-
TSPEED = chr(32) # terminal speed
105-
LFLOW = chr(33) # remote flow control
106-
LINEMODE = chr(34) # Linemode option
107-
XDISPLOC = chr(35) # X Display Location
108-
OLD_ENVIRON = chr(36) # Old - Environment variables
109-
AUTHENTICATION = chr(37) # Authenticate
110-
ENCRYPT = chr(38) # Encryption option
111-
NEW_ENVIRON = chr(39) # New - Environment variables
72+
BINARY = 0 # 8-bit data path
73+
ECHO = 1 # echo
74+
RCP = 2 # prepare to reconnect
75+
SGA = 3 # suppress go ahead
76+
NAMS = 4 # approximate message size
77+
STATUS = 5 # give status
78+
TM = 6 # timing mark
79+
RCTE = 7 # remote controlled transmission and echo
80+
NAOL = 8 # negotiate about output line width
81+
NAOP = 9 # negotiate about output page size
82+
NAOCRD = 10 # negotiate about CR disposition
83+
NAOHTS = 11 # negotiate about horizontal tabstops
84+
NAOHTD = 12 # negotiate about horizontal tab disposition
85+
NAOFFD = 13 # negotiate about formfeed disposition
86+
NAOVTS = 14 # negotiate about vertical tab stops
87+
NAOVTD = 15 # negotiate about vertical tab disposition
88+
NAOLFD = 16 # negotiate about output LF disposition
89+
XASCII = 17 # extended ascii character set
90+
LOGOUT = 18 # force logout
91+
BM = 19 # byte macro
92+
DET = 20 # data entry terminal
93+
SUPDUP = 21 # supdup protocol
94+
SUPDUPOUTPUT = 22 # supdup output
95+
SNDLOC = 23 # send location
96+
TTYPE = 24 # terminal type
97+
EOR = 25 # end or record
98+
TUID = 26 # TACACS user identification
99+
OUTMRK = 27 # output marking
100+
TTYLOC = 28 # terminal location number
101+
VT3270REGIME = 29 # 3270 regime
102+
X3PAD = 30 # X.3 PAD
103+
NAWS = 31 # window size
104+
TSPEED = 32 # terminal speed
105+
LFLOW = 33 # remote flow control
106+
LINEMODE = 34 # Linemode option
107+
XDISPLOC = 35 # X Display Location
108+
OLD_ENVIRON = 36 # Old - Environment variables
109+
AUTHENTICATION = 37 # Authenticate
110+
ENCRYPT = 38 # Encryption option
111+
NEW_ENVIRON = 39 # New - Environment variables
112112
# the following ones come from
113113
# http://www.iana.org/assignments/telnet-options
114114
# Unfortunately, that document does not assign identifiers
115115
# to all of them, so we are making them up
116-
TN3270E = chr(40) # TN3270E
117-
XAUTH = chr(41) # XAUTH
118-
CHARSET = chr(42) # CHARSET
119-
RSP = chr(43) # Telnet Remote Serial Port
120-
COM_PORT_OPTION = chr(44) # Com Port Control Option
121-
SUPPRESS_LOCAL_ECHO = chr(45) # Telnet Suppress Local Echo
122-
TLS = chr(46) # Telnet Start TLS
123-
KERMIT = chr(47) # KERMIT
124-
SEND_URL = chr(48) # SEND-URL
125-
FORWARD_X = chr(49) # FORWARD_X
126-
PRAGMA_LOGON = chr(138) # TELOPT PRAGMA LOGON
127-
SSPI_LOGON = chr(139) # TELOPT SSPI LOGON
128-
PRAGMA_HEARTBEAT = chr(140) # TELOPT PRAGMA HEARTBEAT
129-
EXOPL = chr(255) # Extended-Options-List
130-
NOOPT = chr(0)
116+
TN3270E = 40 # TN3270E
117+
XAUTH = 41 # XAUTH
118+
CHARSET = 42 # CHARSET
119+
RSP = 43 # Telnet Remote Serial Port
120+
COM_PORT_OPTION = 44 # Com Port Control Option
121+
SUPPRESS_LOCAL_ECHO = 45 # Telnet Suppress Local Echo
122+
TLS = 46 # Telnet Start TLS
123+
KERMIT = 47 # KERMIT
124+
SEND_URL = 48 # SEND-URL
125+
FORWARD_X = 49 # FORWARD_X
126+
PRAGMA_LOGON = 138 # TELOPT PRAGMA LOGON
127+
SSPI_LOGON = 139 # TELOPT SSPI LOGON
128+
PRAGMA_HEARTBEAT = 140 # TELOPT PRAGMA HEARTBEAT
129+
EXOPL = 255 # Extended-Options-List
130+
NOOPT = 0
131131

132132
class Telnet:
133133

@@ -197,13 +197,13 @@ def __init__(self, host=None, port=0,
197197
self.port = port
198198
self.timeout = timeout
199199
self.sock = None
200-
self.rawq = ''
200+
self.rawq = b''
201201
self.irawq = 0
202-
self.cookedq = ''
202+
self.cookedq = b''
203203
self.eof = 0
204-
self.iacseq = '' # Buffer for IAC sequence.
204+
self.iacseq = b'' # Buffer for IAC sequence.
205205
self.sb = 0 # flag for SB and SE sequence.
206-
self.sbdataq = ''
206+
self.sbdataq = b''
207207
self.option_callback = None
208208
if host is not None:
209209
self.open(host, port, timeout)
@@ -256,7 +256,7 @@ def close(self):
256256
self.sock.close()
257257
self.sock = 0
258258
self.eof = 1
259-
self.iacseq = ''
259+
self.iacseq = b''
260260
self.sb = 0
261261

262262
def get_socket(self):
@@ -325,13 +325,13 @@ def read_all(self):
325325
self.fill_rawq()
326326
self.process_rawq()
327327
buf = self.cookedq
328-
self.cookedq = ''
328+
self.cookedq = b''
329329
return buf
330330

331331
def read_some(self):
332332
"""Read at least one byte of cooked data unless EOF is hit.
333333
334-
Return '' if EOF is hit. Block if no data is immediately
334+
Return b'' if EOF is hit. Block if no data is immediately
335335
available.
336336
337337
"""
@@ -340,14 +340,14 @@ def read_some(self):
340340
self.fill_rawq()
341341
self.process_rawq()
342342
buf = self.cookedq
343-
self.cookedq = ''
343+
self.cookedq = b''
344344
return buf
345345

346346
def read_very_eager(self):
347347
"""Read everything that's possible without blocking in I/O (eager).
348348
349349
Raise EOFError if connection closed and no cooked data
350-
available. Return '' if no cooked data available otherwise.
350+
available. Return b'' if no cooked data available otherwise.
351351
Don't block unless in the midst of an IAC sequence.
352352
353353
"""
@@ -361,7 +361,7 @@ def read_eager(self):
361361
"""Read readily available data.
362362
363363
Raise EOFError if connection closed and no cooked data
364-
available. Return '' if no cooked data available otherwise.
364+
available. Return b'' if no cooked data available otherwise.
365365
Don't block unless in the midst of an IAC sequence.
366366
367367
"""
@@ -375,7 +375,7 @@ def read_lazy(self):
375375
"""Process and return data that's already in the queues (lazy).
376376
377377
Raise EOFError if connection closed and no data available.
378-
Return '' if no cooked data available otherwise. Don't block
378+
Return b'' if no cooked data available otherwise. Don't block
379379
unless in the midst of an IAC sequence.
380380
381381
"""
@@ -386,25 +386,25 @@ def read_very_lazy(self):
386386
"""Return any data available in the cooked queue (very lazy).
387387
388388
Raise EOFError if connection closed and no data available.
389-
Return '' if no cooked data available otherwise. Don't block.
389+
Return b'' if no cooked data available otherwise. Don't block.
390390
391391
"""
392392
buf = self.cookedq
393-
self.cookedq = ''
393+
self.cookedq = b''
394394
if not buf and self.eof and not self.rawq:
395395
raise EOFError('telnet connection closed')
396396
return buf
397397

398398
def read_sb_data(self):
399399
"""Return any data available in the SB ... SE queue.
400400
401-
Return '' if no SB ... SE available. Should only be called
401+
Return b'' if no SB ... SE available. Should only be called
402402
after seeing a SB or SE command. When a new SB command is
403403
found, old unread SB data will be discarded. Don't block.
404404
405405
"""
406406
buf = self.sbdataq
407-
self.sbdataq = ''
407+
self.sbdataq = b''
408408
return buf
409409

410410
def set_option_negotiation_callback(self, callback):
@@ -418,14 +418,14 @@ def process_rawq(self):
418418
the midst of an IAC sequence.
419419
420420
"""
421-
buf = ['', '']
421+
buf = [b'', b'']
422422
try:
423423
while self.rawq:
424424
c = self.rawq_getchar()
425425
if not self.iacseq:
426426
if c == theNULL:
427427
continue
428-
if c == "\021":
428+
if c == b"\021":
429429
continue
430430
if c != IAC:
431431
buf[self.sb] = buf[self.sb] + c
@@ -438,17 +438,17 @@ def process_rawq(self):
438438
self.iacseq += c
439439
continue
440440

441-
self.iacseq = ''
441+
self.iacseq = b''
442442
if c == IAC:
443443
buf[self.sb] = buf[self.sb] + c
444444
else:
445445
if c == SB: # SB ... SE start.
446446
self.sb = 1
447-
self.sbdataq = ''
447+
self.sbdataq = b''
448448
elif c == SE:
449449
self.sb = 0
450450
self.sbdataq = self.sbdataq + buf[1]
451-
buf[1] = ''
451+
buf[1] = b''
452452
if self.option_callback:
453453
# Callback is supposed to look into
454454
# the sbdataq
@@ -460,7 +460,7 @@ def process_rawq(self):
460460
self.msg('IAC %d not recognized' % ord(c))
461461
elif len(self.iacseq) == 2:
462462
cmd = self.iacseq[1]
463-
self.iacseq = ''
463+
self.iacseq = b''
464464
opt = c
465465
if cmd in (DO, DONT):
466466
self.msg('IAC %s %d',
@@ -477,7 +477,7 @@ def process_rawq(self):
477477
else:
478478
self.sock.sendall(IAC + DONT + opt)
479479
except EOFError: # raised by self.rawq_getchar()
480-
self.iacseq = '' # Reset on EOF
480+
self.iacseq = b'' # Reset on EOF
481481
self.sb = 0
482482
pass
483483
self.cookedq = self.cookedq + buf[0]
@@ -494,10 +494,10 @@ def rawq_getchar(self):
494494
self.fill_rawq()
495495
if self.eof:
496496
raise EOFError
497-
c = self.rawq[self.irawq]
497+
c = self.rawq[self.irawq:self.irawq+1]
498498
self.irawq = self.irawq + 1
499499
if self.irawq >= len(self.rawq):
500-
self.rawq = ''
500+
self.rawq = b''
501501
self.irawq = 0
502502
return c
503503

@@ -509,7 +509,7 @@ def fill_rawq(self):
509509
510510
"""
511511
if self.irawq >= len(self.rawq):
512-
self.rawq = ''
512+
self.rawq = b''
513513
self.irawq = 0
514514
# The buffer size should be fairly small so as to avoid quadratic
515515
# behavior in process_rawq() above
@@ -536,10 +536,10 @@ def interact(self):
536536
print('*** Connection closed by remote host ***')
537537
break
538538
if text:
539-
sys.stdout.write(text)
539+
sys.stdout.write(text.decode('ascii'))
540540
sys.stdout.flush()
541541
if sys.stdin in rfd:
542-
line = sys.stdin.readline()
542+
line = sys.stdin.readline().encode('ascii')
543543
if not line:
544544
break
545545
self.write(line)

0 commit comments

Comments
 (0)