Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/python27/lib64/python2....
File: telnetlib.py
r"""TELNET client class.
[0] Fix | Delete
[1] Fix | Delete
Based on RFC 854: TELNET Protocol Specification, by J. Postel and
[2] Fix | Delete
J. Reynolds
[3] Fix | Delete
[4] Fix | Delete
Example:
[5] Fix | Delete
[6] Fix | Delete
>>> from telnetlib import Telnet
[7] Fix | Delete
>>> tn = Telnet('www.python.org', 79) # connect to finger port
[8] Fix | Delete
>>> tn.write('guido\r\n')
[9] Fix | Delete
>>> print tn.read_all()
[10] Fix | Delete
Login Name TTY Idle When Where
[11] Fix | Delete
guido Guido van Rossum pts/2 <Dec 2 11:10> snag.cnri.reston..
[12] Fix | Delete
[13] Fix | Delete
>>>
[14] Fix | Delete
[15] Fix | Delete
Note that read_all() won't read until eof -- it just reads some data
[16] Fix | Delete
-- but it guarantees to read at least one byte unless EOF is hit.
[17] Fix | Delete
[18] Fix | Delete
It is possible to pass a Telnet object to select.select() in order to
[19] Fix | Delete
wait until more data is available. Note that in this case,
[20] Fix | Delete
read_eager() may return '' even if there was data on the socket,
[21] Fix | Delete
because the protocol negotiation may have eaten the data. This is why
[22] Fix | Delete
EOFError is needed in some cases to distinguish between "no data" and
[23] Fix | Delete
"connection closed" (since the socket also appears ready for reading
[24] Fix | Delete
when it is closed).
[25] Fix | Delete
[26] Fix | Delete
To do:
[27] Fix | Delete
- option negotiation
[28] Fix | Delete
- timeout should be intrinsic to the connection object instead of an
[29] Fix | Delete
option on one of the read calls only
[30] Fix | Delete
[31] Fix | Delete
"""
[32] Fix | Delete
[33] Fix | Delete
[34] Fix | Delete
# Imported modules
[35] Fix | Delete
import errno
[36] Fix | Delete
import sys
[37] Fix | Delete
import socket
[38] Fix | Delete
import select
[39] Fix | Delete
[40] Fix | Delete
__all__ = ["Telnet"]
[41] Fix | Delete
[42] Fix | Delete
# Tunable parameters
[43] Fix | Delete
DEBUGLEVEL = 0
[44] Fix | Delete
[45] Fix | Delete
# Telnet protocol defaults
[46] Fix | Delete
TELNET_PORT = 23
[47] Fix | Delete
[48] Fix | Delete
# Telnet protocol characters (don't change)
[49] Fix | Delete
IAC = chr(255) # "Interpret As Command"
[50] Fix | Delete
DONT = chr(254)
[51] Fix | Delete
DO = chr(253)
[52] Fix | Delete
WONT = chr(252)
[53] Fix | Delete
WILL = chr(251)
[54] Fix | Delete
theNULL = chr(0)
[55] Fix | Delete
[56] Fix | Delete
SE = chr(240) # Subnegotiation End
[57] Fix | Delete
NOP = chr(241) # No Operation
[58] Fix | Delete
DM = chr(242) # Data Mark
[59] Fix | Delete
BRK = chr(243) # Break
[60] Fix | Delete
IP = chr(244) # Interrupt process
[61] Fix | Delete
AO = chr(245) # Abort output
[62] Fix | Delete
AYT = chr(246) # Are You There
[63] Fix | Delete
EC = chr(247) # Erase Character
[64] Fix | Delete
EL = chr(248) # Erase Line
[65] Fix | Delete
GA = chr(249) # Go Ahead
[66] Fix | Delete
SB = chr(250) # Subnegotiation Begin
[67] Fix | Delete
[68] Fix | Delete
[69] Fix | Delete
# Telnet protocol options code (don't change)
[70] Fix | Delete
# These ones all come from arpa/telnet.h
[71] Fix | Delete
BINARY = chr(0) # 8-bit data path
[72] Fix | Delete
ECHO = chr(1) # echo
[73] Fix | Delete
RCP = chr(2) # prepare to reconnect
[74] Fix | Delete
SGA = chr(3) # suppress go ahead
[75] Fix | Delete
NAMS = chr(4) # approximate message size
[76] Fix | Delete
STATUS = chr(5) # give status
[77] Fix | Delete
TM = chr(6) # timing mark
[78] Fix | Delete
RCTE = chr(7) # remote controlled transmission and echo
[79] Fix | Delete
NAOL = chr(8) # negotiate about output line width
[80] Fix | Delete
NAOP = chr(9) # negotiate about output page size
[81] Fix | Delete
NAOCRD = chr(10) # negotiate about CR disposition
[82] Fix | Delete
NAOHTS = chr(11) # negotiate about horizontal tabstops
[83] Fix | Delete
NAOHTD = chr(12) # negotiate about horizontal tab disposition
[84] Fix | Delete
NAOFFD = chr(13) # negotiate about formfeed disposition
[85] Fix | Delete
NAOVTS = chr(14) # negotiate about vertical tab stops
[86] Fix | Delete
NAOVTD = chr(15) # negotiate about vertical tab disposition
[87] Fix | Delete
NAOLFD = chr(16) # negotiate about output LF disposition
[88] Fix | Delete
XASCII = chr(17) # extended ascii character set
[89] Fix | Delete
LOGOUT = chr(18) # force logout
[90] Fix | Delete
BM = chr(19) # byte macro
[91] Fix | Delete
DET = chr(20) # data entry terminal
[92] Fix | Delete
SUPDUP = chr(21) # supdup protocol
[93] Fix | Delete
SUPDUPOUTPUT = chr(22) # supdup output
[94] Fix | Delete
SNDLOC = chr(23) # send location
[95] Fix | Delete
TTYPE = chr(24) # terminal type
[96] Fix | Delete
EOR = chr(25) # end or record
[97] Fix | Delete
TUID = chr(26) # TACACS user identification
[98] Fix | Delete
OUTMRK = chr(27) # output marking
[99] Fix | Delete
TTYLOC = chr(28) # terminal location number
[100] Fix | Delete
VT3270REGIME = chr(29) # 3270 regime
[101] Fix | Delete
X3PAD = chr(30) # X.3 PAD
[102] Fix | Delete
NAWS = chr(31) # window size
[103] Fix | Delete
TSPEED = chr(32) # terminal speed
[104] Fix | Delete
LFLOW = chr(33) # remote flow control
[105] Fix | Delete
LINEMODE = chr(34) # Linemode option
[106] Fix | Delete
XDISPLOC = chr(35) # X Display Location
[107] Fix | Delete
OLD_ENVIRON = chr(36) # Old - Environment variables
[108] Fix | Delete
AUTHENTICATION = chr(37) # Authenticate
[109] Fix | Delete
ENCRYPT = chr(38) # Encryption option
[110] Fix | Delete
NEW_ENVIRON = chr(39) # New - Environment variables
[111] Fix | Delete
# the following ones come from
[112] Fix | Delete
# http://www.iana.org/assignments/telnet-options
[113] Fix | Delete
# Unfortunately, that document does not assign identifiers
[114] Fix | Delete
# to all of them, so we are making them up
[115] Fix | Delete
TN3270E = chr(40) # TN3270E
[116] Fix | Delete
XAUTH = chr(41) # XAUTH
[117] Fix | Delete
CHARSET = chr(42) # CHARSET
[118] Fix | Delete
RSP = chr(43) # Telnet Remote Serial Port
[119] Fix | Delete
COM_PORT_OPTION = chr(44) # Com Port Control Option
[120] Fix | Delete
SUPPRESS_LOCAL_ECHO = chr(45) # Telnet Suppress Local Echo
[121] Fix | Delete
TLS = chr(46) # Telnet Start TLS
[122] Fix | Delete
KERMIT = chr(47) # KERMIT
[123] Fix | Delete
SEND_URL = chr(48) # SEND-URL
[124] Fix | Delete
FORWARD_X = chr(49) # FORWARD_X
[125] Fix | Delete
PRAGMA_LOGON = chr(138) # TELOPT PRAGMA LOGON
[126] Fix | Delete
SSPI_LOGON = chr(139) # TELOPT SSPI LOGON
[127] Fix | Delete
PRAGMA_HEARTBEAT = chr(140) # TELOPT PRAGMA HEARTBEAT
[128] Fix | Delete
EXOPL = chr(255) # Extended-Options-List
[129] Fix | Delete
NOOPT = chr(0)
[130] Fix | Delete
[131] Fix | Delete
class Telnet:
[132] Fix | Delete
[133] Fix | Delete
"""Telnet interface class.
[134] Fix | Delete
[135] Fix | Delete
An instance of this class represents a connection to a telnet
[136] Fix | Delete
server. The instance is initially not connected; the open()
[137] Fix | Delete
method must be used to establish a connection. Alternatively, the
[138] Fix | Delete
host name and optional port number can be passed to the
[139] Fix | Delete
constructor, too.
[140] Fix | Delete
[141] Fix | Delete
Don't try to reopen an already connected instance.
[142] Fix | Delete
[143] Fix | Delete
This class has many read_*() methods. Note that some of them
[144] Fix | Delete
raise EOFError when the end of the connection is read, because
[145] Fix | Delete
they can return an empty string for other reasons. See the
[146] Fix | Delete
individual doc strings.
[147] Fix | Delete
[148] Fix | Delete
read_until(expected, [timeout])
[149] Fix | Delete
Read until the expected string has been seen, or a timeout is
[150] Fix | Delete
hit (default is no timeout); may block.
[151] Fix | Delete
[152] Fix | Delete
read_all()
[153] Fix | Delete
Read all data until EOF; may block.
[154] Fix | Delete
[155] Fix | Delete
read_some()
[156] Fix | Delete
Read at least one byte or EOF; may block.
[157] Fix | Delete
[158] Fix | Delete
read_very_eager()
[159] Fix | Delete
Read all data available already queued or on the socket,
[160] Fix | Delete
without blocking.
[161] Fix | Delete
[162] Fix | Delete
read_eager()
[163] Fix | Delete
Read either data already queued or some data available on the
[164] Fix | Delete
socket, without blocking.
[165] Fix | Delete
[166] Fix | Delete
read_lazy()
[167] Fix | Delete
Read all data in the raw queue (processing it first), without
[168] Fix | Delete
doing any socket I/O.
[169] Fix | Delete
[170] Fix | Delete
read_very_lazy()
[171] Fix | Delete
Reads all data in the cooked queue, without doing any socket
[172] Fix | Delete
I/O.
[173] Fix | Delete
[174] Fix | Delete
read_sb_data()
[175] Fix | Delete
Reads available data between SB ... SE sequence. Don't block.
[176] Fix | Delete
[177] Fix | Delete
set_option_negotiation_callback(callback)
[178] Fix | Delete
Each time a telnet option is read on the input flow, this callback
[179] Fix | Delete
(if set) is called with the following parameters :
[180] Fix | Delete
callback(telnet socket, command, option)
[181] Fix | Delete
option will be chr(0) when there is no option.
[182] Fix | Delete
No other action is done afterwards by telnetlib.
[183] Fix | Delete
[184] Fix | Delete
"""
[185] Fix | Delete
[186] Fix | Delete
def __init__(self, host=None, port=0,
[187] Fix | Delete
timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
[188] Fix | Delete
"""Constructor.
[189] Fix | Delete
[190] Fix | Delete
When called without arguments, create an unconnected instance.
[191] Fix | Delete
With a hostname argument, it connects the instance; port number
[192] Fix | Delete
and timeout are optional.
[193] Fix | Delete
"""
[194] Fix | Delete
self.debuglevel = DEBUGLEVEL
[195] Fix | Delete
self.host = host
[196] Fix | Delete
self.port = port
[197] Fix | Delete
self.timeout = timeout
[198] Fix | Delete
self.sock = None
[199] Fix | Delete
self.rawq = ''
[200] Fix | Delete
self.irawq = 0
[201] Fix | Delete
self.cookedq = ''
[202] Fix | Delete
self.eof = 0
[203] Fix | Delete
self.iacseq = '' # Buffer for IAC sequence.
[204] Fix | Delete
self.sb = 0 # flag for SB and SE sequence.
[205] Fix | Delete
self.sbdataq = ''
[206] Fix | Delete
self.option_callback = None
[207] Fix | Delete
self._has_poll = hasattr(select, 'poll')
[208] Fix | Delete
if host is not None:
[209] Fix | Delete
self.open(host, port, timeout)
[210] Fix | Delete
[211] Fix | Delete
def open(self, host, port=0, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
[212] Fix | Delete
"""Connect to a host.
[213] Fix | Delete
[214] Fix | Delete
The optional second argument is the port number, which
[215] Fix | Delete
defaults to the standard telnet port (23).
[216] Fix | Delete
[217] Fix | Delete
Don't try to reopen an already connected instance.
[218] Fix | Delete
"""
[219] Fix | Delete
self.eof = 0
[220] Fix | Delete
if not port:
[221] Fix | Delete
port = TELNET_PORT
[222] Fix | Delete
self.host = host
[223] Fix | Delete
self.port = port
[224] Fix | Delete
self.timeout = timeout
[225] Fix | Delete
self.sock = socket.create_connection((host, port), timeout)
[226] Fix | Delete
[227] Fix | Delete
def __del__(self):
[228] Fix | Delete
"""Destructor -- close the connection."""
[229] Fix | Delete
self.close()
[230] Fix | Delete
[231] Fix | Delete
def msg(self, msg, *args):
[232] Fix | Delete
"""Print a debug message, when the debug level is > 0.
[233] Fix | Delete
[234] Fix | Delete
If extra arguments are present, they are substituted in the
[235] Fix | Delete
message using the standard string formatting operator.
[236] Fix | Delete
[237] Fix | Delete
"""
[238] Fix | Delete
if self.debuglevel > 0:
[239] Fix | Delete
print 'Telnet(%s,%s):' % (self.host, self.port),
[240] Fix | Delete
if args:
[241] Fix | Delete
print msg % args
[242] Fix | Delete
else:
[243] Fix | Delete
print msg
[244] Fix | Delete
[245] Fix | Delete
def set_debuglevel(self, debuglevel):
[246] Fix | Delete
"""Set the debug level.
[247] Fix | Delete
[248] Fix | Delete
The higher it is, the more debug output you get (on sys.stdout).
[249] Fix | Delete
[250] Fix | Delete
"""
[251] Fix | Delete
self.debuglevel = debuglevel
[252] Fix | Delete
[253] Fix | Delete
def close(self):
[254] Fix | Delete
"""Close the connection."""
[255] Fix | Delete
sock = self.sock
[256] Fix | Delete
self.sock = 0
[257] Fix | Delete
self.eof = 1
[258] Fix | Delete
self.iacseq = ''
[259] Fix | Delete
self.sb = 0
[260] Fix | Delete
if sock:
[261] Fix | Delete
sock.close()
[262] Fix | Delete
[263] Fix | Delete
def get_socket(self):
[264] Fix | Delete
"""Return the socket object used internally."""
[265] Fix | Delete
return self.sock
[266] Fix | Delete
[267] Fix | Delete
def fileno(self):
[268] Fix | Delete
"""Return the fileno() of the socket object used internally."""
[269] Fix | Delete
return self.sock.fileno()
[270] Fix | Delete
[271] Fix | Delete
def write(self, buffer):
[272] Fix | Delete
"""Write a string to the socket, doubling any IAC characters.
[273] Fix | Delete
[274] Fix | Delete
Can block if the connection is blocked. May raise
[275] Fix | Delete
socket.error if the connection is closed.
[276] Fix | Delete
[277] Fix | Delete
"""
[278] Fix | Delete
if IAC in buffer:
[279] Fix | Delete
buffer = buffer.replace(IAC, IAC+IAC)
[280] Fix | Delete
self.msg("send %r", buffer)
[281] Fix | Delete
self.sock.sendall(buffer)
[282] Fix | Delete
[283] Fix | Delete
def read_until(self, match, timeout=None):
[284] Fix | Delete
"""Read until a given string is encountered or until timeout.
[285] Fix | Delete
[286] Fix | Delete
When no match is found, return whatever is available instead,
[287] Fix | Delete
possibly the empty string. Raise EOFError if the connection
[288] Fix | Delete
is closed and no cooked data is available.
[289] Fix | Delete
[290] Fix | Delete
"""
[291] Fix | Delete
if self._has_poll:
[292] Fix | Delete
return self._read_until_with_poll(match, timeout)
[293] Fix | Delete
else:
[294] Fix | Delete
return self._read_until_with_select(match, timeout)
[295] Fix | Delete
[296] Fix | Delete
def _read_until_with_poll(self, match, timeout):
[297] Fix | Delete
"""Read until a given string is encountered or until timeout.
[298] Fix | Delete
[299] Fix | Delete
This method uses select.poll() to implement the timeout.
[300] Fix | Delete
"""
[301] Fix | Delete
n = len(match)
[302] Fix | Delete
call_timeout = timeout
[303] Fix | Delete
if timeout is not None:
[304] Fix | Delete
from time import time
[305] Fix | Delete
time_start = time()
[306] Fix | Delete
self.process_rawq()
[307] Fix | Delete
i = self.cookedq.find(match)
[308] Fix | Delete
if i < 0:
[309] Fix | Delete
poller = select.poll()
[310] Fix | Delete
poll_in_or_priority_flags = select.POLLIN | select.POLLPRI
[311] Fix | Delete
poller.register(self, poll_in_or_priority_flags)
[312] Fix | Delete
while i < 0 and not self.eof:
[313] Fix | Delete
try:
[314] Fix | Delete
# Poll takes its timeout in milliseconds.
[315] Fix | Delete
ready = poller.poll(None if timeout is None
[316] Fix | Delete
else 1000 * call_timeout)
[317] Fix | Delete
except select.error as e:
[318] Fix | Delete
if e[0] == errno.EINTR:
[319] Fix | Delete
if timeout is not None:
[320] Fix | Delete
elapsed = time() - time_start
[321] Fix | Delete
call_timeout = timeout-elapsed
[322] Fix | Delete
continue
[323] Fix | Delete
raise
[324] Fix | Delete
for fd, mode in ready:
[325] Fix | Delete
if mode & poll_in_or_priority_flags:
[326] Fix | Delete
i = max(0, len(self.cookedq)-n)
[327] Fix | Delete
self.fill_rawq()
[328] Fix | Delete
self.process_rawq()
[329] Fix | Delete
i = self.cookedq.find(match, i)
[330] Fix | Delete
if timeout is not None:
[331] Fix | Delete
elapsed = time() - time_start
[332] Fix | Delete
if elapsed >= timeout:
[333] Fix | Delete
break
[334] Fix | Delete
call_timeout = timeout-elapsed
[335] Fix | Delete
poller.unregister(self)
[336] Fix | Delete
if i >= 0:
[337] Fix | Delete
i = i + n
[338] Fix | Delete
buf = self.cookedq[:i]
[339] Fix | Delete
self.cookedq = self.cookedq[i:]
[340] Fix | Delete
return buf
[341] Fix | Delete
return self.read_very_lazy()
[342] Fix | Delete
[343] Fix | Delete
def _read_until_with_select(self, match, timeout=None):
[344] Fix | Delete
"""Read until a given string is encountered or until timeout.
[345] Fix | Delete
[346] Fix | Delete
The timeout is implemented using select.select().
[347] Fix | Delete
"""
[348] Fix | Delete
n = len(match)
[349] Fix | Delete
self.process_rawq()
[350] Fix | Delete
i = self.cookedq.find(match)
[351] Fix | Delete
if i >= 0:
[352] Fix | Delete
i = i+n
[353] Fix | Delete
buf = self.cookedq[:i]
[354] Fix | Delete
self.cookedq = self.cookedq[i:]
[355] Fix | Delete
return buf
[356] Fix | Delete
s_reply = ([self], [], [])
[357] Fix | Delete
s_args = s_reply
[358] Fix | Delete
if timeout is not None:
[359] Fix | Delete
s_args = s_args + (timeout,)
[360] Fix | Delete
from time import time
[361] Fix | Delete
time_start = time()
[362] Fix | Delete
while not self.eof and select.select(*s_args) == s_reply:
[363] Fix | Delete
i = max(0, len(self.cookedq)-n)
[364] Fix | Delete
self.fill_rawq()
[365] Fix | Delete
self.process_rawq()
[366] Fix | Delete
i = self.cookedq.find(match, i)
[367] Fix | Delete
if i >= 0:
[368] Fix | Delete
i = i+n
[369] Fix | Delete
buf = self.cookedq[:i]
[370] Fix | Delete
self.cookedq = self.cookedq[i:]
[371] Fix | Delete
return buf
[372] Fix | Delete
if timeout is not None:
[373] Fix | Delete
elapsed = time() - time_start
[374] Fix | Delete
if elapsed >= timeout:
[375] Fix | Delete
break
[376] Fix | Delete
s_args = s_reply + (timeout-elapsed,)
[377] Fix | Delete
return self.read_very_lazy()
[378] Fix | Delete
[379] Fix | Delete
def read_all(self):
[380] Fix | Delete
"""Read all data until EOF; block until connection closed."""
[381] Fix | Delete
self.process_rawq()
[382] Fix | Delete
while not self.eof:
[383] Fix | Delete
self.fill_rawq()
[384] Fix | Delete
self.process_rawq()
[385] Fix | Delete
buf = self.cookedq
[386] Fix | Delete
self.cookedq = ''
[387] Fix | Delete
return buf
[388] Fix | Delete
[389] Fix | Delete
def read_some(self):
[390] Fix | Delete
"""Read at least one byte of cooked data unless EOF is hit.
[391] Fix | Delete
[392] Fix | Delete
Return '' if EOF is hit. Block if no data is immediately
[393] Fix | Delete
available.
[394] Fix | Delete
[395] Fix | Delete
"""
[396] Fix | Delete
self.process_rawq()
[397] Fix | Delete
while not self.cookedq and not self.eof:
[398] Fix | Delete
self.fill_rawq()
[399] Fix | Delete
self.process_rawq()
[400] Fix | Delete
buf = self.cookedq
[401] Fix | Delete
self.cookedq = ''
[402] Fix | Delete
return buf
[403] Fix | Delete
[404] Fix | Delete
def read_very_eager(self):
[405] Fix | Delete
"""Read everything that's possible without blocking in I/O (eager).
[406] Fix | Delete
[407] Fix | Delete
Raise EOFError if connection closed and no cooked data
[408] Fix | Delete
available. Return '' if no cooked data available otherwise.
[409] Fix | Delete
Don't block unless in the midst of an IAC sequence.
[410] Fix | Delete
[411] Fix | Delete
"""
[412] Fix | Delete
self.process_rawq()
[413] Fix | Delete
while not self.eof and self.sock_avail():
[414] Fix | Delete
self.fill_rawq()
[415] Fix | Delete
self.process_rawq()
[416] Fix | Delete
return self.read_very_lazy()
[417] Fix | Delete
[418] Fix | Delete
def read_eager(self):
[419] Fix | Delete
"""Read readily available data.
[420] Fix | Delete
[421] Fix | Delete
Raise EOFError if connection closed and no cooked data
[422] Fix | Delete
available. Return '' if no cooked data available otherwise.
[423] Fix | Delete
Don't block unless in the midst of an IAC sequence.
[424] Fix | Delete
[425] Fix | Delete
"""
[426] Fix | Delete
self.process_rawq()
[427] Fix | Delete
while not self.cookedq and not self.eof and self.sock_avail():
[428] Fix | Delete
self.fill_rawq()
[429] Fix | Delete
self.process_rawq()
[430] Fix | Delete
return self.read_very_lazy()
[431] Fix | Delete
[432] Fix | Delete
def read_lazy(self):
[433] Fix | Delete
"""Process and return data that's already in the queues (lazy).
[434] Fix | Delete
[435] Fix | Delete
Raise EOFError if connection closed and no data available.
[436] Fix | Delete
Return '' if no cooked data available otherwise. Don't block
[437] Fix | Delete
unless in the midst of an IAC sequence.
[438] Fix | Delete
[439] Fix | Delete
"""
[440] Fix | Delete
self.process_rawq()
[441] Fix | Delete
return self.read_very_lazy()
[442] Fix | Delete
[443] Fix | Delete
def read_very_lazy(self):
[444] Fix | Delete
"""Return any data available in the cooked queue (very lazy).
[445] Fix | Delete
[446] Fix | Delete
Raise EOFError if connection closed and no data available.
[447] Fix | Delete
Return '' if no cooked data available otherwise. Don't block.
[448] Fix | Delete
[449] Fix | Delete
"""
[450] Fix | Delete
buf = self.cookedq
[451] Fix | Delete
self.cookedq = ''
[452] Fix | Delete
if not buf and self.eof and not self.rawq:
[453] Fix | Delete
raise EOFError, 'telnet connection closed'
[454] Fix | Delete
return buf
[455] Fix | Delete
[456] Fix | Delete
def read_sb_data(self):
[457] Fix | Delete
"""Return any data available in the SB ... SE queue.
[458] Fix | Delete
[459] Fix | Delete
Return '' if no SB ... SE available. Should only be called
[460] Fix | Delete
after seeing a SB or SE command. When a new SB command is
[461] Fix | Delete
found, old unread SB data will be discarded. Don't block.
[462] Fix | Delete
[463] Fix | Delete
"""
[464] Fix | Delete
buf = self.sbdataq
[465] Fix | Delete
self.sbdataq = ''
[466] Fix | Delete
return buf
[467] Fix | Delete
[468] Fix | Delete
def set_option_negotiation_callback(self, callback):
[469] Fix | Delete
"""Provide a callback function called after each receipt of a telnet option."""
[470] Fix | Delete
self.option_callback = callback
[471] Fix | Delete
[472] Fix | Delete
def process_rawq(self):
[473] Fix | Delete
"""Transfer from raw queue to cooked queue.
[474] Fix | Delete
[475] Fix | Delete
Set self.eof when connection is closed. Don't block unless in
[476] Fix | Delete
the midst of an IAC sequence.
[477] Fix | Delete
[478] Fix | Delete
"""
[479] Fix | Delete
buf = ['', '']
[480] Fix | Delete
try:
[481] Fix | Delete
while self.rawq:
[482] Fix | Delete
c = self.rawq_getchar()
[483] Fix | Delete
if not self.iacseq:
[484] Fix | Delete
if c == theNULL:
[485] Fix | Delete
continue
[486] Fix | Delete
if c == "\021":
[487] Fix | Delete
continue
[488] Fix | Delete
if c != IAC:
[489] Fix | Delete
buf[self.sb] = buf[self.sb] + c
[490] Fix | Delete
continue
[491] Fix | Delete
else:
[492] Fix | Delete
self.iacseq += c
[493] Fix | Delete
elif len(self.iacseq) == 1:
[494] Fix | Delete
# 'IAC: IAC CMD [OPTION only for WILL/WONT/DO/DONT]'
[495] Fix | Delete
if c in (DO, DONT, WILL, WONT):
[496] Fix | Delete
self.iacseq += c
[497] Fix | Delete
continue
[498] Fix | Delete
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function