cinap_lenrekadd hg and python4bb9e2215 years agoHistory
669 lines · 21.6 KB · Python
RawDownload
        Block if no data is immediately available.  Raise EOFError        when connection is closed.         """        if not self.rawq:            self.fill_rawq()            if self.eof:                raise EOFError        c = self.rawq[self.irawq]        self.irawq = self.irawq + 1        if self.irawq >= len(self.rawq):            self.rawq = ''            self.irawq = 0        return c     def fill_rawq(self):        """Fill raw queue from exactly one recv() system call.         Block if no data is immediately available.  Set self.eof when        connection is closed.         """        if self.irawq >= len(self.rawq):            self.rawq = ''            self.irawq = 0        # The buffer size should be fairly small so as to avoid quadratic        # behavior in process_rawq() above        buf = self.sock.recv(50)        self.msg("recv %r", buf)        self.eof = (not buf)        self.rawq = self.rawq + buf     def sock_avail(self):        """Test whether data is available on the socket."""        return select.select([self], [], [], 0) == ([self], [], [])     def interact(self):        """Interaction function, emulates a very dumb telnet client."""        if sys.platform == "win32":            self.mt_interact()            return        while 1:            rfd, wfd, xfd = select.select([self, sys.stdin], [], [])            if self in rfd:                try:                    text = self.read_eager()                except EOFError:                    print '*** Connection closed by remote host ***'                    break                if text:                    sys.stdout.write(text)                    sys.stdout.flush()            if sys.stdin in rfd:                line = sys.stdin.readline()                if not line:                    break                self.write(line)     def mt_interact(self):        """Multithreaded version of interact()."""        import thread        thread.start_new_thread(self.listener, ())        while 1:            line = sys.stdin.readline()            if not line:                break            self.write(line)     def listener(self):        """Helper for mt_interact() -- this executes in the other thread."""        while 1:            try:                data = self.read_eager()            except EOFError:                print '*** Connection closed by remote host ***'                return            if data:                sys.stdout.write(data)            else:                sys.stdout.flush()     def expect(self, list, timeout=None):        """Read until one from a list of a regular expressions matches.         The first argument is a list of regular expressions, either        compiled (re.RegexObject instances) or uncompiled (strings).        The optional second argument is a timeout, in seconds; default        is no timeout.         Return a tuple of three items: the index in the list of the        first regular expression that matches; the match object        returned; and the text read up till and including the match.         If EOF is read and no text was read, raise EOFError.        Otherwise, when nothing matches, return (-1, None, text) where        text is the text received so far (may be the empty string if a        timeout happened).         If a regular expression ends with a greedy match (e.g. '.*')        or if more than one expression can match the same input, the        results are undeterministic, and may depend on the I/O timing.         """        re = None        list = list[:]        indices = range(len(list))        for i in indices:            if not hasattr(list[i], "search"):                if not re: import re                list[i] = re.compile(list[i])        if timeout is not None:            from time import time            time_start = time()        while 1:            self.process_rawq()            for i in indices:                m = list[i].search(self.cookedq)                if m:                    e = m.end()                    text = self.cookedq[:e]                    self.cookedq = self.cookedq[e:]                    return (i, m, text)            if self.eof:                break            if timeout is not None:                elapsed = time() - time_start                if elapsed >= timeout:                    break                s_args = ([self.fileno()], [], [], timeout-elapsed)                r, w, x = select.select(*s_args)                if not r:                    break            self.fill_rawq()        text = self.read_very_lazy()        if not text and self.eof:            raise EOFError        return (-1, None, text)  def test():    """Test program for telnetlib.     Usage: python telnetlib.py [-d] ... [host [port]]     Default host is localhost; default port is 23.     """    debuglevel = 0    while sys.argv[1:] and sys.argv[1] == '-d':        debuglevel = debuglevel+1        del sys.argv[1]    host = 'localhost'    if sys.argv[1:]:        host = sys.argv[1]    port = 0    if sys.argv[2:]:        portstr = sys.argv[2]        try:            port = int(portstr)        except ValueError:            port = socket.getservbyname(portstr, 'tcp')    tn = Telnet()    tn.set_debuglevel(debuglevel)    tn.open(host, port)    tn.interact()    tn.close() if __name__ == '__main__':    test()

Lines 501–669 of 669. Previous lines

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t