14adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaor"""TELNET client class.
24adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
34adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoBased on RFC 854: TELNET Protocol Specification, by J. Postel and
44adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoJ. Reynolds
54adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
64adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoExample:
74adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
84adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao>>> from telnetlib import Telnet
94adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao>>> tn = Telnet('www.python.org', 79)   # connect to finger port
104adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao>>> tn.write('guido\r\n')
114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao>>> print tn.read_all()
124adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoLogin       Name               TTY         Idle    When    Where
134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoguido    Guido van Rossum      pts/2        <Dec  2 11:10> snag.cnri.reston..
144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao>>>
164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
174adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoNote that read_all() won't read until eof -- it just reads some data
184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao-- but it guarantees to read at least one byte unless EOF is hit.
194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
204adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoIt is possible to pass a Telnet object to select.select() in order to
214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaowait until more data is available.  Note that in this case,
224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoread_eager() may return '' even if there was data on the socket,
234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaobecause the protocol negotiation may have eaten the data.  This is why
244adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoEOFError is needed in some cases to distinguish between "no data" and
254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao"connection closed" (since the socket also appears ready for reading
264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaowhen it is closed).
274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
284adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoTo do:
294adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao- option negotiation
304adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao- timeout should be intrinsic to the connection object instead of an
314adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao  option on one of the read calls only
324adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
334adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao"""
344adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
354adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
364adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# Imported modules
374adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoimport errno
384adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoimport sys
394adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoimport socket
404adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoimport select
414adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
424adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao__all__ = ["Telnet"]
434adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
444adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# Tunable parameters
454adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoDEBUGLEVEL = 0
464adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
474adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# Telnet protocol defaults
484adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoTELNET_PORT = 23
494adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
504adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# Telnet protocol characters (don't change)
514adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoIAC  = chr(255) # "Interpret As Command"
524adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoDONT = chr(254)
534adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoDO   = chr(253)
544adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoWONT = chr(252)
554adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoWILL = chr(251)
564adfde8bc82dd39f59e0445588c3e599ada477dJosh GaotheNULL = chr(0)
574adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
584adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoSE  = chr(240)  # Subnegotiation End
594adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoNOP = chr(241)  # No Operation
604adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoDM  = chr(242)  # Data Mark
614adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoBRK = chr(243)  # Break
624adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoIP  = chr(244)  # Interrupt process
634adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoAO  = chr(245)  # Abort output
644adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoAYT = chr(246)  # Are You There
654adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoEC  = chr(247)  # Erase Character
664adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoEL  = chr(248)  # Erase Line
674adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoGA  = chr(249)  # Go Ahead
684adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoSB =  chr(250)  # Subnegotiation Begin
694adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
704adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
714adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# Telnet protocol options code (don't change)
724adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# These ones all come from arpa/telnet.h
734adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoBINARY = chr(0) # 8-bit data path
744adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoECHO = chr(1) # echo
754adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoRCP = chr(2) # prepare to reconnect
764adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoSGA = chr(3) # suppress go ahead
774adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoNAMS = chr(4) # approximate message size
784adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoSTATUS = chr(5) # give status
794adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoTM = chr(6) # timing mark
804adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoRCTE = chr(7) # remote controlled transmission and echo
814adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoNAOL = chr(8) # negotiate about output line width
824adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoNAOP = chr(9) # negotiate about output page size
834adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoNAOCRD = chr(10) # negotiate about CR disposition
844adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoNAOHTS = chr(11) # negotiate about horizontal tabstops
854adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoNAOHTD = chr(12) # negotiate about horizontal tab disposition
864adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoNAOFFD = chr(13) # negotiate about formfeed disposition
874adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoNAOVTS = chr(14) # negotiate about vertical tab stops
884adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoNAOVTD = chr(15) # negotiate about vertical tab disposition
894adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoNAOLFD = chr(16) # negotiate about output LF disposition
904adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoXASCII = chr(17) # extended ascii character set
914adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoLOGOUT = chr(18) # force logout
924adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoBM = chr(19) # byte macro
934adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoDET = chr(20) # data entry terminal
944adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoSUPDUP = chr(21) # supdup protocol
954adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoSUPDUPOUTPUT = chr(22) # supdup output
964adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoSNDLOC = chr(23) # send location
974adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoTTYPE = chr(24) # terminal type
984adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoEOR = chr(25) # end or record
994adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoTUID = chr(26) # TACACS user identification
1004adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoOUTMRK = chr(27) # output marking
1014adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoTTYLOC = chr(28) # terminal location number
1024adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoVT3270REGIME = chr(29) # 3270 regime
1034adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoX3PAD = chr(30) # X.3 PAD
1044adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoNAWS = chr(31) # window size
1054adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoTSPEED = chr(32) # terminal speed
1064adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoLFLOW = chr(33) # remote flow control
1074adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoLINEMODE = chr(34) # Linemode option
1084adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoXDISPLOC = chr(35) # X Display Location
1094adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoOLD_ENVIRON = chr(36) # Old - Environment variables
1104adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoAUTHENTICATION = chr(37) # Authenticate
1114adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoENCRYPT = chr(38) # Encryption option
1124adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoNEW_ENVIRON = chr(39) # New - Environment variables
1134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# the following ones come from
1144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# http://www.iana.org/assignments/telnet-options
1154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# Unfortunately, that document does not assign identifiers
1164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# to all of them, so we are making them up
1174adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoTN3270E = chr(40) # TN3270E
1184adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoXAUTH = chr(41) # XAUTH
1194adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoCHARSET = chr(42) # CHARSET
1204adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoRSP = chr(43) # Telnet Remote Serial Port
1214adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoCOM_PORT_OPTION = chr(44) # Com Port Control Option
1224adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoSUPPRESS_LOCAL_ECHO = chr(45) # Telnet Suppress Local Echo
1234adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoTLS = chr(46) # Telnet Start TLS
1244adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoKERMIT = chr(47) # KERMIT
1254adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoSEND_URL = chr(48) # SEND-URL
1264adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoFORWARD_X = chr(49) # FORWARD_X
1274adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoPRAGMA_LOGON = chr(138) # TELOPT PRAGMA LOGON
1284adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoSSPI_LOGON = chr(139) # TELOPT SSPI LOGON
1294adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoPRAGMA_HEARTBEAT = chr(140) # TELOPT PRAGMA HEARTBEAT
1304adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoEXOPL = chr(255) # Extended-Options-List
1314adfde8bc82dd39f59e0445588c3e599ada477dJosh GaoNOOPT = chr(0)
1324adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1334adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass Telnet:
1344adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1354adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    """Telnet interface class.
1364adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1374adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    An instance of this class represents a connection to a telnet
1384adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    server.  The instance is initially not connected; the open()
1394adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    method must be used to establish a connection.  Alternatively, the
1404adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    host name and optional port number can be passed to the
1414adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    constructor, too.
1424adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1434adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    Don't try to reopen an already connected instance.
1444adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1454adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    This class has many read_*() methods.  Note that some of them
1464adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    raise EOFError when the end of the connection is read, because
1474adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    they can return an empty string for other reasons.  See the
1484adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    individual doc strings.
1494adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1504adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    read_until(expected, [timeout])
1514adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Read until the expected string has been seen, or a timeout is
1524adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        hit (default is no timeout); may block.
1534adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1544adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    read_all()
1554adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Read all data until EOF; may block.
1564adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1574adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    read_some()
1584adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Read at least one byte or EOF; may block.
1594adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1604adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    read_very_eager()
1614adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Read all data available already queued or on the socket,
1624adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        without blocking.
1634adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1644adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    read_eager()
1654adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Read either data already queued or some data available on the
1664adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        socket, without blocking.
1674adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1684adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    read_lazy()
1694adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Read all data in the raw queue (processing it first), without
1704adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        doing any socket I/O.
1714adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1724adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    read_very_lazy()
1734adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Reads all data in the cooked queue, without doing any socket
1744adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        I/O.
1754adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1764adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    read_sb_data()
1774adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Reads available data between SB ... SE sequence. Don't block.
1784adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1794adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    set_option_negotiation_callback(callback)
1804adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Each time a telnet option is read on the input flow, this callback
1814adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        (if set) is called with the following parameters :
1824adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        callback(telnet socket, command, option)
1834adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            option will be chr(0) when there is no option.
1844adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        No other action is done afterwards by telnetlib.
1854adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1864adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    """
1874adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1884adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def __init__(self, host=None, port=0,
1894adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                 timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
1904adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Constructor.
1914adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1924adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        When called without arguments, create an unconnected instance.
1934adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        With a hostname argument, it connects the instance; port number
1944adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        and timeout are optional.
1954adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
1964adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.debuglevel = DEBUGLEVEL
1974adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.host = host
1984adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.port = port
1994adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.timeout = timeout
2004adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.sock = None
2014adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.rawq = ''
2024adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.irawq = 0
2034adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.cookedq = ''
2044adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.eof = 0
2054adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.iacseq = '' # Buffer for IAC sequence.
2064adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.sb = 0 # flag for SB and SE sequence.
2074adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.sbdataq = ''
2084adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.option_callback = None
2094adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self._has_poll = hasattr(select, 'poll')
2104adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if host is not None:
2114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.open(host, port, timeout)
2124adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def open(self, host, port=0, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
2144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Connect to a host.
2154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        The optional second argument is the port number, which
2174adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        defaults to the standard telnet port (23).
2184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Don't try to reopen an already connected instance.
2204adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
2214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.eof = 0
2224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if not port:
2234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            port = TELNET_PORT
2244adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.host = host
2254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.port = port
2264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.timeout = timeout
2274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.sock = socket.create_connection((host, port), timeout)
2284adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2294adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def __del__(self):
2304adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Destructor -- close the connection."""
2314adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.close()
2324adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2334adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def msg(self, msg, *args):
2344adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Print a debug message, when the debug level is > 0.
2354adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2364adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        If extra arguments are present, they are substituted in the
2374adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        message using the standard string formatting operator.
2384adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2394adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
2404adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if self.debuglevel > 0:
2414adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            print 'Telnet(%s,%s):' % (self.host, self.port),
2424adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            if args:
2434adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                print msg % args
2444adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            else:
2454adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                print msg
2464adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2474adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def set_debuglevel(self, debuglevel):
2484adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Set the debug level.
2494adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2504adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        The higher it is, the more debug output you get (on sys.stdout).
2514adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2524adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
2534adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.debuglevel = debuglevel
2544adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2554adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def close(self):
2564adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Close the connection."""
2574adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if self.sock:
2584adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.sock.close()
2594adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.sock = 0
2604adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.eof = 1
2614adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.iacseq = ''
2624adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.sb = 0
2634adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2644adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def get_socket(self):
2654adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Return the socket object used internally."""
2664adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        return self.sock
2674adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2684adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def fileno(self):
2694adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Return the fileno() of the socket object used internally."""
2704adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        return self.sock.fileno()
2714adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2724adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def write(self, buffer):
2734adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Write a string to the socket, doubling any IAC characters.
2744adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2754adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Can block if the connection is blocked.  May raise
2764adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        socket.error if the connection is closed.
2774adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2784adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
2794adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if IAC in buffer:
2804adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            buffer = buffer.replace(IAC, IAC+IAC)
2814adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.msg("send %r", buffer)
2824adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.sock.sendall(buffer)
2834adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2844adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def read_until(self, match, timeout=None):
2854adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Read until a given string is encountered or until timeout.
2864adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2874adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        When no match is found, return whatever is available instead,
2884adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        possibly the empty string.  Raise EOFError if the connection
2894adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        is closed and no cooked data is available.
2904adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2914adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
2924adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if self._has_poll:
2934adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            return self._read_until_with_poll(match, timeout)
2944adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        else:
2954adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            return self._read_until_with_select(match, timeout)
2964adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
2974adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def _read_until_with_poll(self, match, timeout):
2984adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Read until a given string is encountered or until timeout.
2994adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
3004adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        This method uses select.poll() to implement the timeout.
3014adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
3024adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        n = len(match)
3034adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        call_timeout = timeout
3044adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if timeout is not None:
3054adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            from time import time
3064adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            time_start = time()
3074adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.process_rawq()
3084adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        i = self.cookedq.find(match)
3094adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if i < 0:
3104adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            poller = select.poll()
3114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            poll_in_or_priority_flags = select.POLLIN | select.POLLPRI
3124adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            poller.register(self, poll_in_or_priority_flags)
3134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            while i < 0 and not self.eof:
3144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                try:
3154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    ready = poller.poll(call_timeout)
3164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                except select.error as e:
3174adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    if e.errno == errno.EINTR:
3184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        if timeout is not None:
3194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            elapsed = time() - time_start
3204adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            call_timeout = timeout-elapsed
3214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        continue
3224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    raise
3234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                for fd, mode in ready:
3244adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    if mode & poll_in_or_priority_flags:
3254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        i = max(0, len(self.cookedq)-n)
3264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        self.fill_rawq()
3274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        self.process_rawq()
3284adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        i = self.cookedq.find(match, i)
3294adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                if timeout is not None:
3304adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    elapsed = time() - time_start
3314adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    if elapsed >= timeout:
3324adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        break
3334adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    call_timeout = timeout-elapsed
3344adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            poller.unregister(self)
3354adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if i >= 0:
3364adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            i = i + n
3374adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            buf = self.cookedq[:i]
3384adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.cookedq = self.cookedq[i:]
3394adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            return buf
3404adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        return self.read_very_lazy()
3414adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
3424adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def _read_until_with_select(self, match, timeout=None):
3434adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Read until a given string is encountered or until timeout.
3444adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
3454adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        The timeout is implemented using select.select().
3464adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
3474adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        n = len(match)
3484adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.process_rawq()
3494adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        i = self.cookedq.find(match)
3504adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if i >= 0:
3514adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            i = i+n
3524adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            buf = self.cookedq[:i]
3534adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.cookedq = self.cookedq[i:]
3544adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            return buf
3554adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        s_reply = ([self], [], [])
3564adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        s_args = s_reply
3574adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if timeout is not None:
3584adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            s_args = s_args + (timeout,)
3594adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            from time import time
3604adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            time_start = time()
3614adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        while not self.eof and select.select(*s_args) == s_reply:
3624adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            i = max(0, len(self.cookedq)-n)
3634adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.fill_rawq()
3644adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.process_rawq()
3654adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            i = self.cookedq.find(match, i)
3664adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            if i >= 0:
3674adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                i = i+n
3684adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                buf = self.cookedq[:i]
3694adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                self.cookedq = self.cookedq[i:]
3704adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                return buf
3714adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            if timeout is not None:
3724adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                elapsed = time() - time_start
3734adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                if elapsed >= timeout:
3744adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    break
3754adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                s_args = s_reply + (timeout-elapsed,)
3764adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        return self.read_very_lazy()
3774adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
3784adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def read_all(self):
3794adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Read all data until EOF; block until connection closed."""
3804adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.process_rawq()
3814adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        while not self.eof:
3824adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.fill_rawq()
3834adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.process_rawq()
3844adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        buf = self.cookedq
3854adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.cookedq = ''
3864adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        return buf
3874adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
3884adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def read_some(self):
3894adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Read at least one byte of cooked data unless EOF is hit.
3904adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
3914adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Return '' if EOF is hit.  Block if no data is immediately
3924adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        available.
3934adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
3944adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
3954adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.process_rawq()
3964adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        while not self.cookedq and not self.eof:
3974adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.fill_rawq()
3984adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.process_rawq()
3994adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        buf = self.cookedq
4004adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.cookedq = ''
4014adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        return buf
4024adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4034adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def read_very_eager(self):
4044adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Read everything that's possible without blocking in I/O (eager).
4054adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4064adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Raise EOFError if connection closed and no cooked data
4074adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        available.  Return '' if no cooked data available otherwise.
4084adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Don't block unless in the midst of an IAC sequence.
4094adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4104adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
4114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.process_rawq()
4124adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        while not self.eof and self.sock_avail():
4134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.fill_rawq()
4144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.process_rawq()
4154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        return self.read_very_lazy()
4164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4174adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def read_eager(self):
4184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Read readily available data.
4194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4204adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Raise EOFError if connection closed and no cooked data
4214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        available.  Return '' if no cooked data available otherwise.
4224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Don't block unless in the midst of an IAC sequence.
4234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4244adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
4254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.process_rawq()
4264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        while not self.cookedq and not self.eof and self.sock_avail():
4274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.fill_rawq()
4284adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.process_rawq()
4294adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        return self.read_very_lazy()
4304adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4314adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def read_lazy(self):
4324adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Process and return data that's already in the queues (lazy).
4334adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4344adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Raise EOFError if connection closed and no data available.
4354adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Return '' if no cooked data available otherwise.  Don't block
4364adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        unless in the midst of an IAC sequence.
4374adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4384adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
4394adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.process_rawq()
4404adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        return self.read_very_lazy()
4414adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4424adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def read_very_lazy(self):
4434adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Return any data available in the cooked queue (very lazy).
4444adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4454adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Raise EOFError if connection closed and no data available.
4464adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Return '' if no cooked data available otherwise.  Don't block.
4474adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4484adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
4494adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        buf = self.cookedq
4504adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.cookedq = ''
4514adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if not buf and self.eof and not self.rawq:
4524adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            raise EOFError, 'telnet connection closed'
4534adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        return buf
4544adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4554adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def read_sb_data(self):
4564adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Return any data available in the SB ... SE queue.
4574adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4584adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Return '' if no SB ... SE available. Should only be called
4594adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        after seeing a SB or SE command. When a new SB command is
4604adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        found, old unread SB data will be discarded. Don't block.
4614adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4624adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
4634adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        buf = self.sbdataq
4644adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.sbdataq = ''
4654adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        return buf
4664adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4674adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def set_option_negotiation_callback(self, callback):
4684adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Provide a callback function called after each receipt of a telnet option."""
4694adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.option_callback = callback
4704adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4714adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def process_rawq(self):
4724adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Transfer from raw queue to cooked queue.
4734adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4744adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Set self.eof when connection is closed.  Don't block unless in
4754adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        the midst of an IAC sequence.
4764adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4774adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
4784adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        buf = ['', '']
4794adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        try:
4804adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            while self.rawq:
4814adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                c = self.rawq_getchar()
4824adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                if not self.iacseq:
4834adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    if c == theNULL:
4844adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        continue
4854adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    if c == "\021":
4864adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        continue
4874adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    if c != IAC:
4884adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        buf[self.sb] = buf[self.sb] + c
4894adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        continue
4904adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    else:
4914adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        self.iacseq += c
4924adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                elif len(self.iacseq) == 1:
4934adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    # 'IAC: IAC CMD [OPTION only for WILL/WONT/DO/DONT]'
4944adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    if c in (DO, DONT, WILL, WONT):
4954adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        self.iacseq += c
4964adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        continue
4974adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
4984adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    self.iacseq = ''
4994adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    if c == IAC:
5004adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        buf[self.sb] = buf[self.sb] + c
5014adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    else:
5024adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        if c == SB: # SB ... SE start.
5034adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            self.sb = 1
5044adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            self.sbdataq = ''
5054adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        elif c == SE:
5064adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            self.sb = 0
5074adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            self.sbdataq = self.sbdataq + buf[1]
5084adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            buf[1] = ''
5094adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        if self.option_callback:
5104adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            # Callback is supposed to look into
5114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            # the sbdataq
5124adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            self.option_callback(self.sock, c, NOOPT)
5134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        else:
5144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            # We can't offer automatic processing of
5154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            # suboptions. Alas, we should not get any
5164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            # unless we did a WILL/DO before.
5174adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            self.msg('IAC %d not recognized' % ord(c))
5184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                elif len(self.iacseq) == 2:
5194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    cmd = self.iacseq[1]
5204adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    self.iacseq = ''
5214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    opt = c
5224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    if cmd in (DO, DONT):
5234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        self.msg('IAC %s %d',
5244adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            cmd == DO and 'DO' or 'DONT', ord(opt))
5254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        if self.option_callback:
5264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            self.option_callback(self.sock, cmd, opt)
5274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        else:
5284adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            self.sock.sendall(IAC + WONT + opt)
5294adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    elif cmd in (WILL, WONT):
5304adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        self.msg('IAC %s %d',
5314adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            cmd == WILL and 'WILL' or 'WONT', ord(opt))
5324adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        if self.option_callback:
5334adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            self.option_callback(self.sock, cmd, opt)
5344adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        else:
5354adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            self.sock.sendall(IAC + DONT + opt)
5364adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        except EOFError: # raised by self.rawq_getchar()
5374adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.iacseq = '' # Reset on EOF
5384adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.sb = 0
5394adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            pass
5404adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.cookedq = self.cookedq + buf[0]
5414adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.sbdataq = self.sbdataq + buf[1]
5424adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
5434adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def rawq_getchar(self):
5444adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Get next char from raw queue.
5454adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
5464adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Block if no data is immediately available.  Raise EOFError
5474adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        when connection is closed.
5484adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
5494adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
5504adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if not self.rawq:
5514adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.fill_rawq()
5524adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            if self.eof:
5534adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                raise EOFError
5544adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        c = self.rawq[self.irawq]
5554adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.irawq = self.irawq + 1
5564adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if self.irawq >= len(self.rawq):
5574adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.rawq = ''
5584adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.irawq = 0
5594adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        return c
5604adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
5614adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def fill_rawq(self):
5624adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Fill raw queue from exactly one recv() system call.
5634adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
5644adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Block if no data is immediately available.  Set self.eof when
5654adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        connection is closed.
5664adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
5674adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
5684adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if self.irawq >= len(self.rawq):
5694adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.rawq = ''
5704adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.irawq = 0
5714adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # The buffer size should be fairly small so as to avoid quadratic
5724adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # behavior in process_rawq() above
5734adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        buf = self.sock.recv(50)
5744adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.msg("recv %r", buf)
5754adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.eof = (not buf)
5764adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.rawq = self.rawq + buf
5774adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
5784adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def sock_avail(self):
5794adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Test whether data is available on the socket."""
5804adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        return select.select([self], [], [], 0) == ([self], [], [])
5814adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
5824adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def interact(self):
5834adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Interaction function, emulates a very dumb telnet client."""
5844adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if sys.platform == "win32":
5854adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.mt_interact()
5864adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            return
5874adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        while 1:
5884adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            rfd, wfd, xfd = select.select([self, sys.stdin], [], [])
5894adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            if self in rfd:
5904adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                try:
5914adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    text = self.read_eager()
5924adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                except EOFError:
5934adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    print '*** Connection closed by remote host ***'
5944adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    break
5954adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                if text:
5964adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    sys.stdout.write(text)
5974adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    sys.stdout.flush()
5984adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            if sys.stdin in rfd:
5994adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                line = sys.stdin.readline()
6004adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                if not line:
6014adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    break
6024adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                self.write(line)
6034adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
6044adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def mt_interact(self):
6054adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Multithreaded version of interact()."""
6064adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        import thread
6074adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        thread.start_new_thread(self.listener, ())
6084adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        while 1:
6094adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            line = sys.stdin.readline()
6104adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            if not line:
6114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                break
6124adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.write(line)
6134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
6144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def listener(self):
6154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Helper for mt_interact() -- this executes in the other thread."""
6164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        while 1:
6174adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            try:
6184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                data = self.read_eager()
6194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            except EOFError:
6204adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                print '*** Connection closed by remote host ***'
6214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                return
6224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            if data:
6234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                sys.stdout.write(data)
6244adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            else:
6254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                sys.stdout.flush()
6264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
6274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def expect(self, list, timeout=None):
6284adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Read until one from a list of a regular expressions matches.
6294adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
6304adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        The first argument is a list of regular expressions, either
6314adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        compiled (re.RegexObject instances) or uncompiled (strings).
6324adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        The optional second argument is a timeout, in seconds; default
6334adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        is no timeout.
6344adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
6354adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Return a tuple of three items: the index in the list of the
6364adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        first regular expression that matches; the match object
6374adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        returned; and the text read up till and including the match.
6384adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
6394adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        If EOF is read and no text was read, raise EOFError.
6404adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        Otherwise, when nothing matches, return (-1, None, text) where
6414adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        text is the text received so far (may be the empty string if a
6424adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        timeout happened).
6434adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
6444adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        If a regular expression ends with a greedy match (e.g. '.*')
6454adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        or if more than one expression can match the same input, the
6464adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        results are undeterministic, and may depend on the I/O timing.
6474adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
6484adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
6494adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if self._has_poll:
6504adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            return self._expect_with_poll(list, timeout)
6514adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        else:
6524adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            return self._expect_with_select(list, timeout)
6534adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
6544adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def _expect_with_poll(self, expect_list, timeout=None):
6554adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Read until one from a list of a regular expressions matches.
6564adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
6574adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        This method uses select.poll() to implement the timeout.
6584adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
6594adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        re = None
6604adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        expect_list = expect_list[:]
6614adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        indices = range(len(expect_list))
6624adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        for i in indices:
6634adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            if not hasattr(expect_list[i], "search"):
6644adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                if not re: import re
6654adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                expect_list[i] = re.compile(expect_list[i])
6664adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        call_timeout = timeout
6674adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if timeout is not None:
6684adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            from time import time
6694adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            time_start = time()
6704adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.process_rawq()
6714adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        m = None
6724adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        for i in indices:
6734adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            m = expect_list[i].search(self.cookedq)
6744adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            if m:
6754adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                e = m.end()
6764adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                text = self.cookedq[:e]
6774adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                self.cookedq = self.cookedq[e:]
6784adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                break
6794adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if not m:
6804adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            poller = select.poll()
6814adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            poll_in_or_priority_flags = select.POLLIN | select.POLLPRI
6824adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            poller.register(self, poll_in_or_priority_flags)
6834adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            while not m and not self.eof:
6844adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                try:
6854adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    ready = poller.poll(call_timeout)
6864adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                except select.error as e:
6874adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    if e.errno == errno.EINTR:
6884adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        if timeout is not None:
6894adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            elapsed = time() - time_start
6904adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            call_timeout = timeout-elapsed
6914adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        continue
6924adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    raise
6934adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                for fd, mode in ready:
6944adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    if mode & poll_in_or_priority_flags:
6954adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        self.fill_rawq()
6964adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        self.process_rawq()
6974adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        for i in indices:
6984adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            m = expect_list[i].search(self.cookedq)
6994adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            if m:
7004adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                e = m.end()
7014adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                text = self.cookedq[:e]
7024adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                self.cookedq = self.cookedq[e:]
7034adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                                break
7044adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                if timeout is not None:
7054adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    elapsed = time() - time_start
7064adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    if elapsed >= timeout:
7074adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        break
7084adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    call_timeout = timeout-elapsed
7094adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            poller.unregister(self)
7104adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if m:
7114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            return (i, m, text)
7124adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        text = self.read_very_lazy()
7134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if not text and self.eof:
7144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            raise EOFError
7154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        return (-1, None, text)
7164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
7174adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def _expect_with_select(self, list, timeout=None):
7184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """Read until one from a list of a regular expressions matches.
7194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
7204adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        The timeout is implemented using select.select().
7214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        """
7224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        re = None
7234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        list = list[:]
7244adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        indices = range(len(list))
7254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        for i in indices:
7264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            if not hasattr(list[i], "search"):
7274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                if not re: import re
7284adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                list[i] = re.compile(list[i])
7294adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if timeout is not None:
7304adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            from time import time
7314adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            time_start = time()
7324adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        while 1:
7334adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.process_rawq()
7344adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            for i in indices:
7354adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                m = list[i].search(self.cookedq)
7364adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                if m:
7374adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    e = m.end()
7384adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    text = self.cookedq[:e]
7394adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    self.cookedq = self.cookedq[e:]
7404adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    return (i, m, text)
7414adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            if self.eof:
7424adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                break
7434adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            if timeout is not None:
7444adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                elapsed = time() - time_start
7454adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                if elapsed >= timeout:
7464adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    break
7474adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                s_args = ([self.fileno()], [], [], timeout-elapsed)
7484adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                r, w, x = select.select(*s_args)
7494adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                if not r:
7504adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    break
7514adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.fill_rawq()
7524adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        text = self.read_very_lazy()
7534adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        if not text and self.eof:
7544adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            raise EOFError
7554adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        return (-1, None, text)
7564adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
7574adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
7584adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaodef test():
7594adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    """Test program for telnetlib.
7604adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
7614adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    Usage: python telnetlib.py [-d] ... [host [port]]
7624adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
7634adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    Default host is localhost; default port is 23.
7644adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
7654adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    """
7664adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    debuglevel = 0
7674adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    while sys.argv[1:] and sys.argv[1] == '-d':
7684adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        debuglevel = debuglevel+1
7694adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        del sys.argv[1]
7704adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    host = 'localhost'
7714adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    if sys.argv[1:]:
7724adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        host = sys.argv[1]
7734adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    port = 0
7744adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    if sys.argv[2:]:
7754adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        portstr = sys.argv[2]
7764adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        try:
7774adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            port = int(portstr)
7784adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        except ValueError:
7794adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            port = socket.getservbyname(portstr, 'tcp')
7804adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    tn = Telnet()
7814adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    tn.set_debuglevel(debuglevel)
7824adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    tn.open(host, port, timeout=0.5)
7834adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    tn.interact()
7844adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    tn.close()
7854adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
7864adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoif __name__ == '__main__':
7874adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    test()
788