/Doc/library/telnetlib.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 247 lines · 145 code · 102 blank · 0 comment · 0 complexity · 1b657494c829579387aee8cf548261c3 MD5 · raw file

  1. :mod:`telnetlib` --- Telnet client
  2. ==================================
  3. .. module:: telnetlib
  4. :synopsis: Telnet client class.
  5. .. sectionauthor:: Skip Montanaro <skip@pobox.com>
  6. .. index:: single: protocol; Telnet
  7. The :mod:`telnetlib` module provides a :class:`Telnet` class that implements the
  8. Telnet protocol. See :rfc:`854` for details about the protocol. In addition, it
  9. provides symbolic constants for the protocol characters (see below), and for the
  10. telnet options. The symbolic names of the telnet options follow the definitions
  11. in ``arpa/telnet.h``, with the leading ``TELOPT_`` removed. For symbolic names
  12. of options which are traditionally not included in ``arpa/telnet.h``, see the
  13. module source itself.
  14. The symbolic constants for the telnet commands are: IAC, DONT, DO, WONT, WILL,
  15. SE (Subnegotiation End), NOP (No Operation), DM (Data Mark), BRK (Break), IP
  16. (Interrupt process), AO (Abort output), AYT (Are You There), EC (Erase
  17. Character), EL (Erase Line), GA (Go Ahead), SB (Subnegotiation Begin).
  18. .. class:: Telnet([host[, port[, timeout]]])
  19. :class:`Telnet` represents a connection to a Telnet server. The instance is
  20. initially not connected by default; the :meth:`open` method must be used to
  21. establish a connection. Alternatively, the host name and optional port
  22. and timeout can be passed to the constructor, in which case the connection to
  23. the server will be established before the constructor returns. The optional
  24. *timeout* parameter specifies a timeout in seconds for the connection attempt (if
  25. not specified, the global default timeout setting will be used).
  26. number can be passed to the constructor, to, in which case the connection to
  27. the server will be established before the constructor returns. The optional
  28. *timeout* parameter specifies a timeout in seconds for blocking operations
  29. like the connection attempt (if not specified, or passed as None, the global
  30. default timeout setting will be used).
  31. Do not reopen an already connected instance.
  32. This class has many :meth:`read_\*` methods. Note that some of them raise
  33. :exc:`EOFError` when the end of the connection is read, because they can return
  34. an empty string for other reasons. See the individual descriptions below.
  35. .. versionchanged:: 2.6
  36. *timeout* was added.
  37. .. seealso::
  38. :rfc:`854` - Telnet Protocol Specification
  39. Definition of the Telnet protocol.
  40. .. _telnet-objects:
  41. Telnet Objects
  42. --------------
  43. :class:`Telnet` instances have the following methods:
  44. .. method:: Telnet.read_until(expected[, timeout])
  45. Read until a given string, *expected*, is encountered or until *timeout* seconds
  46. have passed.
  47. When no match is found, return whatever is available instead, possibly the empty
  48. string. Raise :exc:`EOFError` if the connection is closed and no cooked data is
  49. available.
  50. .. method:: Telnet.read_all()
  51. Read all data until EOF; block until connection closed.
  52. .. method:: Telnet.read_some()
  53. Read at least one byte of cooked data unless EOF is hit. Return ``''`` if EOF is
  54. hit. Block if no data is immediately available.
  55. .. method:: Telnet.read_very_eager()
  56. Read everything that can be without blocking in I/O (eager).
  57. Raise :exc:`EOFError` if connection closed and no cooked data available. Return
  58. ``''`` if no cooked data available otherwise. Do not block unless in the midst
  59. of an IAC sequence.
  60. .. method:: Telnet.read_eager()
  61. Read readily available data.
  62. Raise :exc:`EOFError` if connection closed and no cooked data available. Return
  63. ``''`` if no cooked data available otherwise. Do not block unless in the midst
  64. of an IAC sequence.
  65. .. method:: Telnet.read_lazy()
  66. Process and return data already in the queues (lazy).
  67. Raise :exc:`EOFError` if connection closed and no data available. Return ``''``
  68. if no cooked data available otherwise. Do not block unless in the midst of an
  69. IAC sequence.
  70. .. method:: Telnet.read_very_lazy()
  71. Return any data available in the cooked queue (very lazy).
  72. Raise :exc:`EOFError` if connection closed and no data available. Return ``''``
  73. if no cooked data available otherwise. This method never blocks.
  74. .. method:: Telnet.read_sb_data()
  75. Return the data collected between a SB/SE pair (suboption begin/end). The
  76. callback should access these data when it was invoked with a ``SE`` command.
  77. This method never blocks.
  78. .. versionadded:: 2.3
  79. .. method:: Telnet.open(host[, port[, timeout]])
  80. Connect to a host. The optional second argument is the port number, which
  81. defaults to the standard Telnet port (23). The optional *timeout* parameter
  82. specifies a timeout in seconds for blocking operations like the connection
  83. attempt (if not specified, the global default timeout setting will be used).
  84. Do not try to reopen an already connected instance.
  85. .. versionchanged:: 2.6
  86. *timeout* was added.
  87. .. method:: Telnet.msg(msg[, *args])
  88. Print a debug message when the debug level is ``>`` 0. If extra arguments are
  89. present, they are substituted in the message using the standard string
  90. formatting operator.
  91. .. method:: Telnet.set_debuglevel(debuglevel)
  92. Set the debug level. The higher the value of *debuglevel*, the more debug
  93. output you get (on ``sys.stdout``).
  94. .. method:: Telnet.close()
  95. Close the connection.
  96. .. method:: Telnet.get_socket()
  97. Return the socket object used internally.
  98. .. method:: Telnet.fileno()
  99. Return the file descriptor of the socket object used internally.
  100. .. method:: Telnet.write(buffer)
  101. Write a string to the socket, doubling any IAC characters. This can block if the
  102. connection is blocked. May raise :exc:`socket.error` if the connection is
  103. closed.
  104. .. method:: Telnet.interact()
  105. Interaction function, emulates a very dumb Telnet client.
  106. .. method:: Telnet.mt_interact()
  107. Multithreaded version of :meth:`interact`.
  108. .. method:: Telnet.expect(list[, timeout])
  109. Read until one from a list of a regular expressions matches.
  110. The first argument is a list of regular expressions, either compiled
  111. (:class:`re.RegexObject` instances) or uncompiled (strings). The optional second
  112. argument is a timeout, in seconds; the default is to block indefinitely.
  113. Return a tuple of three items: the index in the list of the first regular
  114. expression that matches; the match object returned; and the text read up till
  115. and including the match.
  116. If end of file is found and no text was read, raise :exc:`EOFError`. Otherwise,
  117. when nothing matches, return ``(-1, None, text)`` where *text* is the text
  118. received so far (may be the empty string if a timeout happened).
  119. If a regular expression ends with a greedy match (such as ``.*``) or if more
  120. than one expression can match the same input, the results are indeterministic,
  121. and may depend on the I/O timing.
  122. .. method:: Telnet.set_option_negotiation_callback(callback)
  123. Each time a telnet option is read on the input flow, this *callback* (if set) is
  124. called with the following parameters : callback(telnet socket, command
  125. (DO/DONT/WILL/WONT), option). No other action is done afterwards by telnetlib.
  126. .. _telnet-example:
  127. Telnet Example
  128. --------------
  129. .. sectionauthor:: Peter Funk <pf@artcom-gmbh.de>
  130. A simple example illustrating typical use::
  131. import getpass
  132. import sys
  133. import telnetlib
  134. HOST = "localhost"
  135. user = raw_input("Enter your remote account: ")
  136. password = getpass.getpass()
  137. tn = telnetlib.Telnet(HOST)
  138. tn.read_until("login: ")
  139. tn.write(user + "\n")
  140. if password:
  141. tn.read_until("Password: ")
  142. tn.write(password + "\n")
  143. tn.write("ls\n")
  144. tn.write("exit\n")
  145. print tn.read_all()