"telnetlib" — Telnet クライアント
*********************************

**ソースコード:** Lib/telnetlib.py

======================================================================

"telnetlib" モジュールでは、Telnet プロトコルを実装している "Telnet"
クラスを提供します。Telnet プロトコルについての詳細は **RFC 854** を参
照してください。加えて、このモジュールでは Telnet プロトコルにおける制
御文字 (下を参照してください) と、telnet オプションに対するシンボル定
数を提供しています。telnet オプションに対するシンボル名は
"arpa/telnet.h" の "TELOPT_" がない状態での定義に従います。伝統的に
"arpa/telnet.h" に含められていない telnet オプションのシンボル名につい
ては、このモジュールのソースコード自体を参照してください。

telnet コマンドのシンボル定数は、IAC、DONT、DO、WONT、WILL、SE (サブネ
ゴシエーション終了)、NOP (何もしない)、DM (データマーク)、BRK (ブレー
ク)、IP (プロセス中断)、AO (出力停止)、AYT (応答確認)、EC (文字削除)、
EL (行削除)、GA (進め)、SB (サブネゴシエーション開始) です。

class telnetlib.Telnet([host[, port[, timeout]]])

   "Telnet" represents a connection to a Telnet server. The instance
   is initially not connected by default; the "open()" method must be
   used to establish a connection.  Alternatively, the host name and
   optional port number can be passed to the constructor, to, in which
   case the connection to the server will be established before the
   constructor returns.  The optional *timeout* parameter specifies a
   timeout in seconds for blocking operations like the connection
   attempt (if not specified, the global default timeout setting will
   be used).

   すでに接続の開かれているンスタンスを再度開いてはいけません。

   このクラスは多くの "read_*()" メソッドを持っています。これらのメソ
   ッドのいくつかは、接続の終端を示す文字を読み込んだ場合に "EOFError"
   を送出するので注意してください。例外を送出するのは、これらの関数が
   終端に到達しなくても空の文字列を返す可能性があるからです。詳しくは
   下記の個々の説明を参照してください。

   バージョン 2.6 で変更: *timeout* was added.

参考:

  **RFC 854** - Telnet プロトコル仕様
     Telnet プロトコルの定義。


Telnet オブジェクト
===================

"Telnet" インスタンスは以下のメソッドを持っています:

Telnet.read_until(expected[, timeout])

   Read until a given string, *expected*, is encountered or until
   *timeout* seconds have passed.

   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.

Telnet.read_all()

   Read all data until EOF; block until connection closed.

Telnet.read_some()

   Read at least one byte of cooked data unless EOF is hit. Return
   "''" if EOF is hit.  Block if no data is immediately available.

Telnet.read_very_eager()

   I/O によるブロックを起こさずに読み出せる全てのデータを読み込みます
   (eager モード)。

   Raise "EOFError" if connection closed and no cooked data available.
   Return "''" if no cooked data available otherwise. Do not block
   unless in the midst of an IAC sequence.

Telnet.read_eager()

   現在すぐに読み出せるデータを読み出します。

   Raise "EOFError" if connection closed and no cooked data available.
   Return "''" if no cooked data available otherwise. Do not block
   unless in the midst of an IAC sequence.

Telnet.read_lazy()

   すでにキューに入っているデータを処理して返します (lazy モード)。

   Raise "EOFError" if connection closed and no data available. Return
   "''" if no cooked data available otherwise.  Do not block unless in
   the midst of an IAC sequence.

Telnet.read_very_lazy()

   すでに処理済みキューに入っているデータを処理して返します (very lazy
   モード)。

   Raise "EOFError" if connection closed and no data available. Return
   "''" if no cooked data available otherwise.  This method never
   blocks.

Telnet.read_sb_data()

   SB/SE ペア (サブオプション開始／終了) の間に収集されたデータを返し
   ます。 "SE" コマンドによって起動されたコールバック関数はこれらのデ
   ータにアクセスしなければなりません。このメソッドはけっしてブロック
   しません。

   バージョン 2.3 で追加.

Telnet.open(host[, port[, timeout]])

   サーバホストに接続します。第二引数はオプションで、ポート番号を指定
   します。標準の値は通常の Telnet ポート番号 (23) です。オプション引
   数の *timeout* が渡された場合、コネクション接続時などのブロックする
   操作のタイムアウト時間を秒数で指定します (指定されなかった場合は、
   グローバルのデフォルトタイムアウト設定が利用されます)。

   すでに接続しているインスタンスで再接続を試みてはいけません。

   バージョン 2.6 で変更: *timeout* was added.

Telnet.msg(msg[, *args])

   デバッグレベルが ">" 0 のとき、デバッグ用のメッセージを出力します。
   追加の引数が存在する場合、標準の文字列書式化演算子 "%" を使って
   *msg* 中の書式指定子に代入されます。

Telnet.set_debuglevel(debuglevel)

   デバッグレベルを設定します。 *debuglevel* が大きくなるほど、
   ("sys.stdout" に) デバッグメッセージがたくさん出力されます。

Telnet.close()

   コネクションをクローズします。

Telnet.get_socket()

   内部的に使われているソケットオブジェクトです。

Telnet.fileno()

   内部的に使われているソケットオブジェクトのファイル記述子です。

Telnet.write(buffer)

   Write a string to the socket, doubling any IAC characters. This can
   block if the connection is blocked.  May raise "socket.error" if
   the connection is closed.

Telnet.interact()

   非常に低機能の telnet クライアントをエミュレートする対話関数です。

Telnet.mt_interact()

   "interact()" のマルチスレッド版です。

Telnet.expect(list[, timeout])

   正規表現のリストのうちどれか一つにマッチするまでデータを読みます。

   The first argument is a list of regular expressions, either
   compiled ("regex objects") or uncompiled (strings). The optional
   second argument is a timeout, in seconds; the default is to block
   indefinitely.

   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 end of file is found 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).

   正規表現の末尾が (".*" のような) 貪欲マッチングになっている場合や、
   入力に対して 1 つ以上の正規表現がマッチする場合には、その結果は決定
   不能で、I/O のタイミングに依存するでしょう。

Telnet.set_option_negotiation_callback(callback)

   telnet オプションが入力フローから読み込まれるたびに、 *callback* が
   (設定されていれば) 以下の引数形式: callback(telnet socket, command
   (DO/DONT/WILL/WONT), option) で呼び出されます。その後 telnet オプシ
   ョンに対しては telnetlib は何も行いません。


Telnet Example
==============

典型的な使用例のサンプルコード:

   import getpass
   import sys
   import telnetlib

   HOST = "localhost"
   user = raw_input("Enter your remote account: ")
   password = getpass.getpass()

   tn = telnetlib.Telnet(HOST)

   tn.read_until("login: ")
   tn.write(user + "\n")
   if password:
       tn.read_until("Password: ")
       tn.write(password + "\n")

   tn.write("ls\n")
   tn.write("exit\n")

   print tn.read_all()
