Current Path: > > opt > > alt > python33 > lib64 > python3.3 >
Operation : Linux premium131.web-hosting.com 4.18.0-553.44.1.lve.el8.x86_64 #1 SMP Thu Mar 13 14:29:12 UTC 2025 x86_64 Software : Apache Server IP : 162.0.232.56 | Your IP: 216.73.216.111 Domains : 1034 Domain(s) Permission : [ 0755 ]
Name | Type | Size | Last Modified | Actions |
---|---|---|---|---|
__pycache__ | Directory | - | - | |
collections | Directory | - | - | |
concurrent | Directory | - | - | |
config-3.3m | Directory | - | - | |
ctypes | Directory | - | - | |
curses | Directory | - | - | |
dbm | Directory | - | - | |
distutils | Directory | - | - | |
Directory | - | - | ||
encodings | Directory | - | - | |
html | Directory | - | - | |
http | Directory | - | - | |
idlelib | Directory | - | - | |
importlib | Directory | - | - | |
json | Directory | - | - | |
lib-dynload | Directory | - | - | |
lib2to3 | Directory | - | - | |
logging | Directory | - | - | |
multiprocessing | Directory | - | - | |
plat-linux | Directory | - | - | |
pydoc_data | Directory | - | - | |
site-packages | Directory | - | - | |
sqlite3 | Directory | - | - | |
test | Directory | - | - | |
unittest | Directory | - | - | |
urllib | Directory | - | - | |
venv | Directory | - | - | |
wsgiref | Directory | - | - | |
xml | Directory | - | - | |
xmlrpc | Directory | - | - | |
__future__.py | File | 4584 bytes | April 17 2024 16:58:21. | |
__phello__.foo.py | File | 64 bytes | April 17 2024 16:58:20. | |
_compat_pickle.py | File | 4338 bytes | April 17 2024 16:58:19. | |
_dummy_thread.py | File | 4769 bytes | April 17 2024 16:58:20. | |
_markupbase.py | File | 14598 bytes | April 17 2024 16:58:15. | |
_osx_support.py | File | 18855 bytes | April 17 2024 16:58:20. | |
_pyio.py | File | 72905 bytes | April 17 2024 16:58:17. | |
_strptime.py | File | 21674 bytes | April 17 2024 16:58:20. | |
_sysconfigdata.py | File | 22842 bytes | April 17 2024 16:58:20. | |
_threading_local.py | File | 7410 bytes | April 17 2024 16:58:15. | |
_weakrefset.py | File | 5705 bytes | April 17 2024 16:58:14. | |
abc.py | File | 8057 bytes | April 17 2024 16:58:15. | |
aifc.py | File | 31054 bytes | April 17 2024 16:58:21. | |
antigravity.py | File | 475 bytes | April 17 2024 16:58:16. | |
argparse.py | File | 89069 bytes | April 17 2024 16:58:20. | |
ast.py | File | 12142 bytes | April 17 2024 16:58:20. | |
asynchat.py | File | 11588 bytes | April 17 2024 16:58:19. | |
asyncore.py | File | 20753 bytes | April 17 2024 16:58:21. | |
base64.py | File | 13986 bytes | April 17 2024 16:58:17. | |
bdb.py | File | 21894 bytes | April 17 2024 16:58:19. | |
binhex.py | File | 13708 bytes | April 17 2024 16:58:14. | |
bisect.py | File | 2595 bytes | April 17 2024 16:58:13. | |
bz2.py | File | 18473 bytes | April 17 2024 16:58:20. | |
cProfile.py | File | 6361 bytes | April 17 2024 16:58:14. | |
calendar.py | File | 22940 bytes | April 17 2024 16:58:20. | |
cgi.py | File | 35554 bytes | April 17 2024 16:58:20. | |
cgitb.py | File | 12041 bytes | April 17 2024 16:58:21. | |
chunk.py | File | 5377 bytes | April 17 2024 16:58:17. | |
cmd.py | File | 14860 bytes | April 17 2024 16:58:14. | |
code.py | File | 10030 bytes | April 17 2024 16:58:16. | |
codecs.py | File | 35956 bytes | April 17 2024 16:58:15. | |
codeop.py | File | 5994 bytes | April 17 2024 16:58:14. | |
colorsys.py | File | 3691 bytes | April 17 2024 16:58:15. | |
compileall.py | File | 9743 bytes | April 17 2024 16:58:14. | |
configparser.py | File | 49437 bytes | April 17 2024 16:58:15. | |
contextlib.py | File | 9125 bytes | April 17 2024 16:58:14. | |
copy.py | File | 8991 bytes | April 17 2024 16:58:15. | |
copyreg.py | File | 6611 bytes | April 17 2024 16:58:20. | |
crypt.py | File | 1879 bytes | April 17 2024 16:58:14. | |
csv.py | File | 16185 bytes | April 17 2024 16:58:15. | |
datetime.py | File | 74954 bytes | April 17 2024 16:58:21. | |
decimal.py | File | 228558 bytes | April 17 2024 16:58:19. | |
difflib.py | File | 82519 bytes | April 17 2024 16:58:17. | |
dis.py | File | 10134 bytes | April 17 2024 16:58:15. | |
doctest.py | File | 102933 bytes | April 17 2024 16:58:15. | |
dummy_threading.py | File | 2815 bytes | April 17 2024 16:58:14. | |
filecmp.py | File | 9597 bytes | April 17 2024 16:58:15. | |
fileinput.py | File | 14256 bytes | April 17 2024 16:58:17. | |
fnmatch.py | File | 3163 bytes | April 17 2024 16:58:15. | |
formatter.py | File | 14930 bytes | April 17 2024 16:58:15. | |
fractions.py | File | 23033 bytes | April 17 2024 16:58:14. | |
ftplib.py | File | 40253 bytes | April 17 2024 16:58:15. | |
functools.py | File | 13596 bytes | April 17 2024 16:58:21. | |
genericpath.py | File | 3093 bytes | April 17 2024 16:58:21. | |
getopt.py | File | 7488 bytes | April 17 2024 16:58:20. | |
getpass.py | File | 5793 bytes | April 17 2024 16:58:14. | |
gettext.py | File | 20637 bytes | April 17 2024 16:58:20. | |
glob.py | File | 2838 bytes | April 17 2024 16:58:14. | |
gzip.py | File | 24403 bytes | April 17 2024 16:58:20. | |
hashlib.py | File | 6193 bytes | April 17 2024 16:58:21. | |
heapq.py | File | 17997 bytes | April 17 2024 16:58:13. | |
hmac.py | File | 4440 bytes | April 17 2024 16:58:17. | |
imaplib.py | File | 50111 bytes | April 17 2024 16:58:20. | |
imghdr.py | File | 3528 bytes | April 17 2024 16:58:20. | |
imp.py | File | 9727 bytes | April 17 2024 16:58:15. | |
inspect.py | File | 78960 bytes | April 17 2024 16:58:19. | |
io.py | File | 3280 bytes | April 17 2024 16:58:15. | |
ipaddress.py | File | 70303 bytes | April 17 2024 16:58:20. | |
keyword.py | File | 2060 bytes | April 17 2024 16:58:20. | |
linecache.py | File | 3864 bytes | April 17 2024 16:58:16. | |
locale.py | File | 93215 bytes | April 17 2024 16:58:19. | |
lzma.py | File | 17454 bytes | April 17 2024 16:58:20. | |
macpath.py | File | 5617 bytes | April 17 2024 16:58:15. | |
macurl2path.py | File | 2732 bytes | April 17 2024 16:58:15. | |
mailbox.py | File | 79093 bytes | April 17 2024 16:58:19. | |
mailcap.py | File | 7437 bytes | April 17 2024 16:58:14. | |
mimetypes.py | File | 20735 bytes | April 17 2024 16:58:19. | |
modulefinder.py | File | 23198 bytes | April 17 2024 16:58:14. | |
netrc.py | File | 5747 bytes | April 17 2024 16:58:17. | |
nntplib.py | File | 42786 bytes | April 17 2024 16:58:13. | |
ntpath.py | File | 20437 bytes | April 17 2024 16:58:14. | |
nturl2path.py | File | 2396 bytes | April 17 2024 16:58:20. | |
numbers.py | File | 10398 bytes | April 17 2024 16:58:20. | |
opcode.py | File | 5098 bytes | April 17 2024 16:58:21. | |
optparse.py | File | 60346 bytes | April 17 2024 16:58:20. | |
os.py | File | 34779 bytes | April 17 2024 16:58:14. | |
os2emxpath.py | File | 4659 bytes | April 17 2024 16:58:15. | |
pdb.py | File | 60653 bytes | April 17 2024 16:58:16. | |
pickle.py | File | 47858 bytes | April 17 2024 16:58:17. | |
pickletools.py | File | 81349 bytes | April 17 2024 16:58:15. | |
pipes.py | File | 8916 bytes | April 17 2024 16:58:20. | |
pkgutil.py | File | 21539 bytes | April 17 2024 16:58:15. | |
platform.py | File | 50742 bytes | April 17 2024 16:58:15. | |
plistlib.py | File | 14777 bytes | April 17 2024 16:58:13. | |
poplib.py | File | 11372 bytes | April 17 2024 16:58:13. | |
posixpath.py | File | 14254 bytes | April 17 2024 16:58:16. | |
pprint.py | File | 12700 bytes | April 17 2024 16:58:15. | |
profile.py | File | 21448 bytes | April 17 2024 16:58:17. | |
pstats.py | File | 26372 bytes | April 17 2024 16:58:14. | |
pty.py | File | 5055 bytes | April 17 2024 16:58:14. | |
py_compile.py | File | 6717 bytes | April 17 2024 16:58:19. | |
pyclbr.py | File | 13438 bytes | April 17 2024 16:58:13. | |
pydoc.py | File | 101644 bytes | April 17 2024 16:58:15. | |
queue.py | File | 8835 bytes | April 17 2024 16:58:20. | |
quopri.py | File | 7315 bytes | April 17 2024 16:58:20. | |
random.py | File | 25660 bytes | April 17 2024 16:58:14. | |
re.py | File | 14973 bytes | April 17 2024 16:58:17. | |
reprlib.py | File | 5110 bytes | April 17 2024 16:58:15. | |
rlcompleter.py | File | 5526 bytes | April 17 2024 16:58:21. | |
runpy.py | File | 10413 bytes | April 17 2024 16:58:14. | |
sched.py | File | 6399 bytes | April 17 2024 16:58:19. | |
shelve.py | File | 8243 bytes | April 17 2024 16:58:20. | |
shlex.py | File | 11502 bytes | April 17 2024 16:58:21. | |
shutil.py | File | 39147 bytes | April 17 2024 16:58:20. | |
site.py | File | 21971 bytes | April 17 2024 16:58:19. | |
smtpd.py | File | 30207 bytes | April 17 2024 16:58:16. | |
smtplib.py | File | 38021 bytes | April 17 2024 16:58:14. | |
sndhdr.py | File | 6219 bytes | April 17 2024 16:58:20. | |
socket.py | File | 14913 bytes | April 17 2024 16:58:20. | |
socketserver.py | File | 24196 bytes | April 17 2024 16:58:21. | |
sre_compile.py | File | 16345 bytes | April 17 2024 16:58:14. | |
sre_constants.py | File | 7231 bytes | April 17 2024 16:58:14. | |
sre_parse.py | File | 30212 bytes | April 17 2024 16:58:16. | |
ssl.py | File | 24478 bytes | April 17 2024 16:58:19. | |
stat.py | File | 4304 bytes | April 17 2024 16:58:19. | |
string.py | File | 9410 bytes | April 17 2024 16:58:20. | |
stringprep.py | File | 12917 bytes | April 17 2024 16:58:17. | |
struct.py | File | 238 bytes | April 17 2024 16:58:16. | |
subprocess.py | File | 67578 bytes | April 17 2024 16:58:14. | |
sunau.py | File | 17523 bytes | April 17 2024 16:58:15. | |
symbol.py | File | 2051 bytes | April 17 2024 16:58:14. | |
symtable.py | File | 7383 bytes | April 17 2024 16:58:20. | |
sysconfig.py | File | 25174 bytes | April 17 2024 16:58:20. | |
tabnanny.py | File | 11410 bytes | April 17 2024 16:58:20. | |
tarfile.py | File | 88864 bytes | April 17 2024 16:58:15. | |
telnetlib.py | File | 27349 bytes | April 17 2024 16:58:15. | |
tempfile.py | File | 23013 bytes | April 17 2024 16:58:14. | |
textwrap.py | File | 16488 bytes | April 17 2024 16:58:14. | |
this.py | File | 1003 bytes | April 17 2024 16:58:17. | |
threading.py | File | 45641 bytes | April 17 2024 16:58:19. | |
timeit.py | File | 12395 bytes | April 17 2024 16:58:15. | |
token.py | File | 3034 bytes | April 17 2024 16:58:14. | |
tokenize.py | File | 24876 bytes | April 17 2024 16:58:20. | |
trace.py | File | 31487 bytes | April 17 2024 16:58:13. | |
traceback.py | File | 11982 bytes | April 17 2024 16:58:20. | |
tty.py | File | 879 bytes | April 17 2024 16:58:13. | |
types.py | File | 3167 bytes | April 17 2024 16:58:14. | |
uu.py | File | 6766 bytes | April 17 2024 16:58:14. | |
uuid.py | File | 22349 bytes | April 17 2024 16:58:15. | |
warnings.py | File | 13825 bytes | April 17 2024 16:58:15. | |
wave.py | File | 18579 bytes | April 17 2024 16:58:15. | |
weakref.py | File | 11495 bytes | April 17 2024 16:58:19. | |
webbrowser.py | File | 22913 bytes | April 17 2024 16:58:20. | |
xdrlib.py | File | 5381 bytes | April 17 2024 16:58:21. | |
zipfile.py | File | 66424 bytes | April 17 2024 16:58:21. |
r"""TELNET client class. Based on RFC 854: TELNET Protocol Specification, by J. Postel and J. Reynolds Example: >>> from telnetlib import Telnet >>> tn = Telnet('www.python.org', 79) # connect to finger port >>> tn.write(b'guido\r\n') >>> print(tn.read_all()) Login Name TTY Idle When Where guido Guido van Rossum pts/2 <Dec 2 11:10> snag.cnri.reston.. >>> Note that read_all() won't read until eof -- it just reads some data -- but it guarantees to read at least one byte unless EOF is hit. It is possible to pass a Telnet object to select.select() in order to wait until more data is available. Note that in this case, read_eager() may return b'' even if there was data on the socket, because the protocol negotiation may have eaten the data. This is why EOFError is needed in some cases to distinguish between "no data" and "connection closed" (since the socket also appears ready for reading when it is closed). To do: - option negotiation - timeout should be intrinsic to the connection object instead of an option on one of the read calls only """ # Imported modules import errno import sys import socket import select try: from time import monotonic as _time except ImportError: from time import time as _time __all__ = ["Telnet"] # Tunable parameters DEBUGLEVEL = 0 # Telnet protocol defaults TELNET_PORT = 23 # Telnet protocol characters (don't change) IAC = bytes([255]) # "Interpret As Command" DONT = bytes([254]) DO = bytes([253]) WONT = bytes([252]) WILL = bytes([251]) theNULL = bytes([0]) SE = bytes([240]) # Subnegotiation End NOP = bytes([241]) # No Operation DM = bytes([242]) # Data Mark BRK = bytes([243]) # Break IP = bytes([244]) # Interrupt process AO = bytes([245]) # Abort output AYT = bytes([246]) # Are You There EC = bytes([247]) # Erase Character EL = bytes([248]) # Erase Line GA = bytes([249]) # Go Ahead SB = bytes([250]) # Subnegotiation Begin # Telnet protocol options code (don't change) # These ones all come from arpa/telnet.h BINARY = bytes([0]) # 8-bit data path ECHO = bytes([1]) # echo RCP = bytes([2]) # prepare to reconnect SGA = bytes([3]) # suppress go ahead NAMS = bytes([4]) # approximate message size STATUS = bytes([5]) # give status TM = bytes([6]) # timing mark RCTE = bytes([7]) # remote controlled transmission and echo NAOL = bytes([8]) # negotiate about output line width NAOP = bytes([9]) # negotiate about output page size NAOCRD = bytes([10]) # negotiate about CR disposition NAOHTS = bytes([11]) # negotiate about horizontal tabstops NAOHTD = bytes([12]) # negotiate about horizontal tab disposition NAOFFD = bytes([13]) # negotiate about formfeed disposition NAOVTS = bytes([14]) # negotiate about vertical tab stops NAOVTD = bytes([15]) # negotiate about vertical tab disposition NAOLFD = bytes([16]) # negotiate about output LF disposition XASCII = bytes([17]) # extended ascii character set LOGOUT = bytes([18]) # force logout BM = bytes([19]) # byte macro DET = bytes([20]) # data entry terminal SUPDUP = bytes([21]) # supdup protocol SUPDUPOUTPUT = bytes([22]) # supdup output SNDLOC = bytes([23]) # send location TTYPE = bytes([24]) # terminal type EOR = bytes([25]) # end or record TUID = bytes([26]) # TACACS user identification OUTMRK = bytes([27]) # output marking TTYLOC = bytes([28]) # terminal location number VT3270REGIME = bytes([29]) # 3270 regime X3PAD = bytes([30]) # X.3 PAD NAWS = bytes([31]) # window size TSPEED = bytes([32]) # terminal speed LFLOW = bytes([33]) # remote flow control LINEMODE = bytes([34]) # Linemode option XDISPLOC = bytes([35]) # X Display Location OLD_ENVIRON = bytes([36]) # Old - Environment variables AUTHENTICATION = bytes([37]) # Authenticate ENCRYPT = bytes([38]) # Encryption option NEW_ENVIRON = bytes([39]) # New - Environment variables # the following ones come from # http://www.iana.org/assignments/telnet-options # Unfortunately, that document does not assign identifiers # to all of them, so we are making them up TN3270E = bytes([40]) # TN3270E XAUTH = bytes([41]) # XAUTH CHARSET = bytes([42]) # CHARSET RSP = bytes([43]) # Telnet Remote Serial Port COM_PORT_OPTION = bytes([44]) # Com Port Control Option SUPPRESS_LOCAL_ECHO = bytes([45]) # Telnet Suppress Local Echo TLS = bytes([46]) # Telnet Start TLS KERMIT = bytes([47]) # KERMIT SEND_URL = bytes([48]) # SEND-URL FORWARD_X = bytes([49]) # FORWARD_X PRAGMA_LOGON = bytes([138]) # TELOPT PRAGMA LOGON SSPI_LOGON = bytes([139]) # TELOPT SSPI LOGON PRAGMA_HEARTBEAT = bytes([140]) # TELOPT PRAGMA HEARTBEAT EXOPL = bytes([255]) # Extended-Options-List NOOPT = bytes([0]) class Telnet: """Telnet interface class. An instance of this class represents a connection to a telnet server. The instance is initially not connected; the open() method must be used to establish a connection. Alternatively, the host name and optional port number can be passed to the constructor, too. Don't try to reopen an already connected instance. This class has many read_*() methods. Note that some of them raise EOFError when the end of the connection is read, because they can return an empty string for other reasons. See the individual doc strings. read_until(expected, [timeout]) Read until the expected string has been seen, or a timeout is hit (default is no timeout); may block. read_all() Read all data until EOF; may block. read_some() Read at least one byte or EOF; may block. read_very_eager() Read all data available already queued or on the socket, without blocking. read_eager() Read either data already queued or some data available on the socket, without blocking. read_lazy() Read all data in the raw queue (processing it first), without doing any socket I/O. read_very_lazy() Reads all data in the cooked queue, without doing any socket I/O. read_sb_data() Reads available data between SB ... SE sequence. Don't block. set_option_negotiation_callback(callback) Each time a telnet option is read on the input flow, this callback (if set) is called with the following parameters : callback(telnet socket, command, option) option will be chr(0) when there is no option. No other action is done afterwards by telnetlib. """ def __init__(self, host=None, port=0, timeout=socket._GLOBAL_DEFAULT_TIMEOUT): """Constructor. When called without arguments, create an unconnected instance. With a hostname argument, it connects the instance; port number and timeout are optional. """ self.debuglevel = DEBUGLEVEL self.host = host self.port = port self.timeout = timeout self.sock = None self.rawq = b'' self.irawq = 0 self.cookedq = b'' self.eof = 0 self.iacseq = b'' # Buffer for IAC sequence. self.sb = 0 # flag for SB and SE sequence. self.sbdataq = b'' self.option_callback = None self._has_poll = hasattr(select, 'poll') if host is not None: self.open(host, port, timeout) def open(self, host, port=0, timeout=socket._GLOBAL_DEFAULT_TIMEOUT): """Connect to a host. The optional second argument is the port number, which defaults to the standard telnet port (23). Don't try to reopen an already connected instance. """ self.eof = 0 if not port: port = TELNET_PORT self.host = host self.port = port self.timeout = timeout self.sock = socket.create_connection((host, port), timeout) def __del__(self): """Destructor -- close the connection.""" self.close() def msg(self, msg, *args): """Print a debug message, when the debug level is > 0. If extra arguments are present, they are substituted in the message using the standard string formatting operator. """ if self.debuglevel > 0: print('Telnet(%s,%s):' % (self.host, self.port), end=' ') if args: print(msg % args) else: print(msg) def set_debuglevel(self, debuglevel): """Set the debug level. The higher it is, the more debug output you get (on sys.stdout). """ self.debuglevel = debuglevel def close(self): """Close the connection.""" if self.sock: self.sock.close() self.sock = 0 self.eof = 1 self.iacseq = b'' self.sb = 0 def get_socket(self): """Return the socket object used internally.""" return self.sock def fileno(self): """Return the fileno() of the socket object used internally.""" return self.sock.fileno() def write(self, buffer): """Write a string to the socket, doubling any IAC characters. Can block if the connection is blocked. May raise socket.error if the connection is closed. """ if IAC in buffer: buffer = buffer.replace(IAC, IAC+IAC) self.msg("send %r", buffer) self.sock.sendall(buffer) def read_until(self, match, timeout=None): """Read until a given string is encountered or until timeout. When no match is found, return whatever is available instead, possibly the empty string. Raise EOFError if the connection is closed and no cooked data is available. """ if self._has_poll: return self._read_until_with_poll(match, timeout) else: return self._read_until_with_select(match, timeout) def _read_until_with_poll(self, match, timeout): """Read until a given string is encountered or until timeout. This method uses select.poll() to implement the timeout. """ n = len(match) call_timeout = timeout if timeout is not None: time_start = _time() self.process_rawq() i = self.cookedq.find(match) if i < 0: poller = select.poll() poll_in_or_priority_flags = select.POLLIN | select.POLLPRI poller.register(self, poll_in_or_priority_flags) while i < 0 and not self.eof: try: ready = poller.poll(None if timeout is None else 1000 * call_timeout) except select.error as e: if e.errno == errno.EINTR: if timeout is not None: elapsed = _time() - time_start call_timeout = timeout-elapsed continue raise for fd, mode in ready: if mode & poll_in_or_priority_flags: i = max(0, len(self.cookedq)-n) self.fill_rawq() self.process_rawq() i = self.cookedq.find(match, i) if timeout is not None: elapsed = _time() - time_start if elapsed >= timeout: break call_timeout = timeout-elapsed poller.unregister(self) if i >= 0: i = i + n buf = self.cookedq[:i] self.cookedq = self.cookedq[i:] return buf return self.read_very_lazy() def _read_until_with_select(self, match, timeout=None): """Read until a given string is encountered or until timeout. The timeout is implemented using select.select(). """ n = len(match) self.process_rawq() i = self.cookedq.find(match) if i >= 0: i = i+n buf = self.cookedq[:i] self.cookedq = self.cookedq[i:] return buf s_reply = ([self], [], []) s_args = s_reply if timeout is not None: s_args = s_args + (timeout,) time_start = _time() while not self.eof and select.select(*s_args) == s_reply: i = max(0, len(self.cookedq)-n) self.fill_rawq() self.process_rawq() i = self.cookedq.find(match, i) if i >= 0: i = i+n buf = self.cookedq[:i] self.cookedq = self.cookedq[i:] return buf if timeout is not None: elapsed = _time() - time_start if elapsed >= timeout: break s_args = s_reply + (timeout-elapsed,) return self.read_very_lazy() def read_all(self): """Read all data until EOF; block until connection closed.""" self.process_rawq() while not self.eof: self.fill_rawq() self.process_rawq() buf = self.cookedq self.cookedq = b'' return buf def read_some(self): """Read at least one byte of cooked data unless EOF is hit. Return b'' if EOF is hit. Block if no data is immediately available. """ self.process_rawq() while not self.cookedq and not self.eof: self.fill_rawq() self.process_rawq() buf = self.cookedq self.cookedq = b'' return buf def read_very_eager(self): """Read everything that's possible without blocking in I/O (eager). Raise EOFError if connection closed and no cooked data available. Return b'' if no cooked data available otherwise. Don't block unless in the midst of an IAC sequence. """ self.process_rawq() while not self.eof and self.sock_avail(): self.fill_rawq() self.process_rawq() return self.read_very_lazy() def read_eager(self): """Read readily available data. Raise EOFError if connection closed and no cooked data available. Return b'' if no cooked data available otherwise. Don't block unless in the midst of an IAC sequence. """ self.process_rawq() while not self.cookedq and not self.eof and self.sock_avail(): self.fill_rawq() self.process_rawq() return self.read_very_lazy() def read_lazy(self): """Process and return data that's already in the queues (lazy). Raise EOFError if connection closed and no data available. Return b'' if no cooked data available otherwise. Don't block unless in the midst of an IAC sequence. """ self.process_rawq() return self.read_very_lazy() def read_very_lazy(self): """Return any data available in the cooked queue (very lazy). Raise EOFError if connection closed and no data available. Return b'' if no cooked data available otherwise. Don't block. """ buf = self.cookedq self.cookedq = b'' if not buf and self.eof and not self.rawq: raise EOFError('telnet connection closed') return buf def read_sb_data(self): """Return any data available in the SB ... SE queue. Return b'' if no SB ... SE available. Should only be called after seeing a SB or SE command. When a new SB command is found, old unread SB data will be discarded. Don't block. """ buf = self.sbdataq self.sbdataq = b'' return buf def set_option_negotiation_callback(self, callback): """Provide a callback function called after each receipt of a telnet option.""" self.option_callback = callback def process_rawq(self): """Transfer from raw queue to cooked queue. Set self.eof when connection is closed. Don't block unless in the midst of an IAC sequence. """ buf = [b'', b''] try: while self.rawq: c = self.rawq_getchar() if not self.iacseq: if c == theNULL: continue if c == b"\021": continue if c != IAC: buf[self.sb] = buf[self.sb] + c continue else: self.iacseq += c elif len(self.iacseq) == 1: # 'IAC: IAC CMD [OPTION only for WILL/WONT/DO/DONT]' if c in (DO, DONT, WILL, WONT): self.iacseq += c continue self.iacseq = b'' if c == IAC: buf[self.sb] = buf[self.sb] + c else: if c == SB: # SB ... SE start. self.sb = 1 self.sbdataq = b'' elif c == SE: self.sb = 0 self.sbdataq = self.sbdataq + buf[1] buf[1] = b'' if self.option_callback: # Callback is supposed to look into # the sbdataq self.option_callback(self.sock, c, NOOPT) else: # We can't offer automatic processing of # suboptions. Alas, we should not get any # unless we did a WILL/DO before. self.msg('IAC %d not recognized' % ord(c)) elif len(self.iacseq) == 2: cmd = self.iacseq[1:2] self.iacseq = b'' opt = c if cmd in (DO, DONT): self.msg('IAC %s %d', cmd == DO and 'DO' or 'DONT', ord(opt)) if self.option_callback: self.option_callback(self.sock, cmd, opt) else: self.sock.sendall(IAC + WONT + opt) elif cmd in (WILL, WONT): self.msg('IAC %s %d', cmd == WILL and 'WILL' or 'WONT', ord(opt)) if self.option_callback: self.option_callback(self.sock, cmd, opt) else: self.sock.sendall(IAC + DONT + opt) except EOFError: # raised by self.rawq_getchar() self.iacseq = b'' # Reset on EOF self.sb = 0 pass self.cookedq = self.cookedq + buf[0] self.sbdataq = self.sbdataq + buf[1] def rawq_getchar(self): """Get next char from raw queue. 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+1] self.irawq = self.irawq + 1 if self.irawq >= len(self.rawq): self.rawq = b'' 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 = b'' 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.decode('ascii')) sys.stdout.flush() if sys.stdin in rfd: line = sys.stdin.readline().encode('ascii') 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.encode('ascii')) 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.decode('ascii')) 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. """ if self._has_poll: return self._expect_with_poll(list, timeout) else: return self._expect_with_select(list, timeout) def _expect_with_poll(self, expect_list, timeout=None): """Read until one from a list of a regular expressions matches. This method uses select.poll() to implement the timeout. """ re = None expect_list = expect_list[:] indices = range(len(expect_list)) for i in indices: if not hasattr(expect_list[i], "search"): if not re: import re expect_list[i] = re.compile(expect_list[i]) call_timeout = timeout if timeout is not None: time_start = _time() self.process_rawq() m = None for i in indices: m = expect_list[i].search(self.cookedq) if m: e = m.end() text = self.cookedq[:e] self.cookedq = self.cookedq[e:] break if not m: poller = select.poll() poll_in_or_priority_flags = select.POLLIN | select.POLLPRI poller.register(self, poll_in_or_priority_flags) while not m and not self.eof: try: ready = poller.poll(None if timeout is None else 1000 * call_timeout) except select.error as e: if e.errno == errno.EINTR: if timeout is not None: elapsed = _time() - time_start call_timeout = timeout-elapsed continue raise for fd, mode in ready: if mode & poll_in_or_priority_flags: self.fill_rawq() self.process_rawq() for i in indices: m = expect_list[i].search(self.cookedq) if m: e = m.end() text = self.cookedq[:e] self.cookedq = self.cookedq[e:] break if timeout is not None: elapsed = _time() - time_start if elapsed >= timeout: break call_timeout = timeout-elapsed poller.unregister(self) if m: return (i, m, text) text = self.read_very_lazy() if not text and self.eof: raise EOFError return (-1, None, text) def _expect_with_select(self, list, timeout=None): """Read until one from a list of a regular expressions matches. The timeout is implemented using select.select(). """ 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: 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, timeout=0.5) tn.interact() tn.close() if __name__ == '__main__': test()
SILENT KILLER Tool