annotate Lib/telnetlib.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 1a3270e26c16
children ea979c5206b7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """TELNET client class.
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 Based on RFC 854: TELNET Protocol Specification, by J. Postel and
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 J. Reynolds
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 Example:
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 >>> from telnetlib import Telnet
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 >>> tn = Telnet('www.python.org', 79) # connect to finger port
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 >>> tn.write('guido\r\n')
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 >>> print tn.read_all()
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 Login Name TTY Idle When Where
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 guido Guido van Rossum pts/2 <Dec 2 11:10> snag.cnri.reston..
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12547
diff changeset
14
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 >>>
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 Note that read_all() won't read until eof -- it just reads some data
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 -- but it guarantees to read at least one byte unless EOF is hit.
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 It is possible to pass a Telnet object to select.select() in order to
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 wait until more data is available. Note that in this case,
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 read_eager() may return '' even if there was data on the socket,
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 because the protocol negotiation may have eaten the data. This is why
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 EOFError is needed in some cases to distinguish between "no data" and
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 "connection closed" (since the socket also appears ready for reading
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 when it is closed).
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 Bugs:
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 - may hang when connection is slow in the middle of an IAC sequence
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 To do:
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 - option negotiation
6942
06291f04ba9c Added expect() method which takes a list of regular expressions and an
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
33 - timeout should be intrinsic to the connection object instead of an
06291f04ba9c Added expect() method which takes a list of regular expressions and an
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
34 option on one of the read calls only
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 """
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 # Imported modules
6942
06291f04ba9c Added expect() method which takes a list of regular expressions and an
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
40 import sys
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 import socket
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 import select
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
16889
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 16488
diff changeset
44 __all__ = ["Telnet"]
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 16488
diff changeset
45
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 # Tunable parameters
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 DEBUGLEVEL = 0
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 # Telnet protocol defaults
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 TELNET_PORT = 23
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
51
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 # Telnet protocol characters (don't change)
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 IAC = chr(255) # "Interpret As Command"
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 DONT = chr(254)
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 DO = chr(253)
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 WONT = chr(252)
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 WILL = chr(251)
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 theNULL = chr(0)
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
19760
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
60 # Telnet protocol options code (don't change)
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
61 # These ones all come from arpa/telnet.h
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
62 BINARY = chr(0) # 8-bit data path
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
63 ECHO = chr(1) # echo
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
64 RCP = chr(2) # prepare to reconnect
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
65 SGA = chr(3) # suppress go ahead
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
66 NAMS = chr(4) # approximate message size
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
67 STATUS = chr(5) # give status
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
68 TM = chr(6) # timing mark
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
69 RCTE = chr(7) # remote controlled transmission and echo
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
70 NAOL = chr(8) # negotiate about output line width
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
71 NAOP = chr(9) # negotiate about output page size
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
72 NAOCRD = chr(10) # negotiate about CR disposition
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
73 NAOHTS = chr(11) # negotiate about horizontal tabstops
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
74 NAOHTD = chr(12) # negotiate about horizontal tab disposition
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
75 NAOFFD = chr(13) # negotiate about formfeed disposition
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
76 NAOVTS = chr(14) # negotiate about vertical tab stops
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
77 NAOVTD = chr(15) # negotiate about vertical tab disposition
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
78 NAOLFD = chr(16) # negotiate about output LF disposition
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
79 XASCII = chr(17) # extended ascii character set
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
80 LOGOUT = chr(18) # force logout
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
81 BM = chr(19) # byte macro
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
82 DET = chr(20) # data entry terminal
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
83 SUPDUP = chr(21) # supdup protocol
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
84 SUPDUPOUTPUT = chr(22) # supdup output
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
85 SNDLOC = chr(23) # send location
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
86 TTYPE = chr(24) # terminal type
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
87 EOR = chr(25) # end or record
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
88 TUID = chr(26) # TACACS user identification
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
89 OUTMRK = chr(27) # output marking
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
90 TTYLOC = chr(28) # terminal location number
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
91 VT3270REGIME = chr(29) # 3270 regime
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
92 X3PAD = chr(30) # X.3 PAD
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
93 NAWS = chr(31) # window size
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
94 TSPEED = chr(32) # terminal speed
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
95 LFLOW = chr(33) # remote flow control
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
96 LINEMODE = chr(34) # Linemode option
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
97 XDISPLOC = chr(35) # X Display Location
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
98 OLD_ENVIRON = chr(36) # Old - Environment variables
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
99 AUTHENTICATION = chr(37) # Authenticate
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
100 ENCRYPT = chr(38) # Encryption option
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
101 NEW_ENVIRON = chr(39) # New - Environment variables
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
102 # the following ones come from
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
103 # http://www.iana.org/assignments/telnet-options
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
104 # Unfortunately, that document does not assign identifiers
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
105 # to all of them, so we are making them up
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
106 TN3270E = chr(40) # TN3270E
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
107 XAUTH = chr(41) # XAUTH
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
108 CHARSET = chr(42) # CHARSET
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
109 RSP = chr(43) # Telnet Remote Serial Port
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
110 COM_PORT_OPTION = chr(44) # Com Port Control Option
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
111 SUPPRESS_LOCAL_ECHO = chr(45) # Telnet Suppress Local Echo
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
112 TLS = chr(46) # Telnet Start TLS
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
113 KERMIT = chr(47) # KERMIT
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
114 SEND_URL = chr(48) # SEND-URL
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
115 FORWARD_X = chr(49) # FORWARD_X
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
116 PRAGMA_LOGON = chr(138) # TELOPT PRAGMA LOGON
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
117 SSPI_LOGON = chr(139) # TELOPT SSPI LOGON
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
118 PRAGMA_HEARTBEAT = chr(140) # TELOPT PRAGMA HEARTBEAT
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
119 EXOPL = chr(255) # Extended-Options-List
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 class Telnet:
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 """Telnet interface class.
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 An instance of this class represents a connection to a telnet
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 server. The instance is initially not connected; the open()
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 method must be used to establish a connection. Alternatively, the
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 host name and optional port number can be passed to the
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 constructor, too.
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
130
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 Don't try to reopen an already connected instance.
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
132
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 This class has many read_*() methods. Note that some of them
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 raise EOFError when the end of the connection is read, because
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 they can return an empty string for other reasons. See the
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 individual doc strings.
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
137
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 read_until(expected, [timeout])
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 Read until the expected string has been seen, or a timeout is
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
140 hit (default is no timeout); may block.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 read_all()
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 Read all data until EOF; may block.
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
144
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 read_some()
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 Read at least one byte or EOF; may block.
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 read_very_eager()
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 Read all data available already queued or on the socket,
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
150 without blocking.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 read_eager()
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 Read either data already queued or some data available on the
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
154 socket, without blocking.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
155
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 read_lazy()
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 Read all data in the raw queue (processing it first), without
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
158 doing any socket I/O.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
159
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 read_very_lazy()
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 Reads all data in the cooked queue, without doing any socket
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
162 I/O.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
163
19760
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
164 set_option_negotiation_callback(callback)
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
165 Each time a telnet option is read on the input flow, this callback
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
166 (if set) is called with the following parameters :
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
167 callback(telnet socket, command (DO/DONT/WILL/WONT), option)
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
168 No other action is done afterwards by telnetlib.
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
169
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 """
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
171
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 def __init__(self, host=None, port=0):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
173 """Constructor.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
174
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
175 When called without arguments, create an unconnected instance.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
176 With a hostname argument, it connects the instance; a port
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
177 number is optional.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
178
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
179 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
180 self.debuglevel = DEBUGLEVEL
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
181 self.host = host
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
182 self.port = port
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
183 self.sock = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
184 self.rawq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
185 self.irawq = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
186 self.cookedq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
187 self.eof = 0
19760
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
188 self.option_callback = None
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
189 if host:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
190 self.open(host, port)
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
191
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 def open(self, host, port=0):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
193 """Connect to a host.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
194
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
195 The optional second argument is the port number, which
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
196 defaults to the standard telnet port (23).
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
197
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
198 Don't try to reopen an already connected instance.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
199
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
200 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
201 self.eof = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
202 if not port:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
203 port = TELNET_PORT
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
204 self.host = host
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
205 self.port = port
18902
edf058b16ddf Initialize msg to avoid unbound locals.
Martin v. Löwis <martin@v.loewis.de>
parents: 18834
diff changeset
206 msg = "getaddrinfo returns an empty list"
18834
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
207 for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
208 af, socktype, proto, canonname, sa = res
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
209 try:
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
210 self.sock = socket.socket(af, socktype, proto)
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
211 self.sock.connect(sa)
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
212 except socket.error, msg:
20303
faec69c11640 Only close sockets if they have been created. Reported by Blake Winton.
Martin v. Löwis <martin@v.loewis.de>
parents: 19760
diff changeset
213 if self.sock:
faec69c11640 Only close sockets if they have been created. Reported by Blake Winton.
Martin v. Löwis <martin@v.loewis.de>
parents: 19760
diff changeset
214 self.sock.close()
18834
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
215 self.sock = None
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
216 continue
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
217 break
18825
40900620293e Patch #401196: Use getaddrinfo and AF_INET6 in TCP servers and clients.
Martin v. Löwis <martin@v.loewis.de>
parents: 16889
diff changeset
218 if not self.sock:
18834
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
219 raise socket.error, msg
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
220
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 def __del__(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
222 """Destructor -- close the connection."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
223 self.close()
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
224
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 def msg(self, msg, *args):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
226 """Print a debug message, when the debug level is > 0.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
227
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
228 If extra arguments are present, they are substituted in the
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
229 message using the standard string formatting operator.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
230
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
231 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
232 if self.debuglevel > 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
233 print 'Telnet(%s,%d):' % (self.host, self.port),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
234 if args:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
235 print msg % args
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
236 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
237 print msg
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
238
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 def set_debuglevel(self, debuglevel):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
240 """Set the debug level.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
241
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
242 The higher it is, the more debug output you get (on sys.stdout).
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
243
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
244 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
245 self.debuglevel = debuglevel
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
246
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 def close(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
248 """Close the connection."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
249 if self.sock:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
250 self.sock.close()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
251 self.sock = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
252 self.eof = 1
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
253
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 def get_socket(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
255 """Return the socket object used internally."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
256 return self.sock
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
257
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 def fileno(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
259 """Return the fileno() of the socket object used internally."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
260 return self.sock.fileno()
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
261
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 def write(self, buffer):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
263 """Write a string to the socket, doubling any IAC characters.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
264
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
265 Can block if the connection is blocked. May raise
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
266 socket.error if the connection is closed.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
267
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
268 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
269 if IAC in buffer:
16488
92cbefdb674a String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15873
diff changeset
270 buffer = buffer.replace(IAC, IAC+IAC)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
271 self.msg("send %s", `buffer`)
21963
2faa23facef7 The Grande 'sendall()' patch, copied from release21-maint. Fixes #516715.
Martin v. Löwis <martin@v.loewis.de>
parents: 20303
diff changeset
272 self.sock.sendall(buffer)
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
273
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 def read_until(self, match, timeout=None):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
275 """Read until a given string is encountered or until timeout.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
276
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
277 When no match is found, return whatever is available instead,
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
278 possibly the empty string. Raise EOFError if the connection
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
279 is closed and no cooked data is available.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
280
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
281 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
282 n = len(match)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
283 self.process_rawq()
16488
92cbefdb674a String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15873
diff changeset
284 i = self.cookedq.find(match)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
285 if i >= 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
286 i = i+n
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
287 buf = self.cookedq[:i]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
288 self.cookedq = self.cookedq[i:]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
289 return buf
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
290 s_reply = ([self], [], [])
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
291 s_args = s_reply
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
292 if timeout is not None:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
293 s_args = s_args + (timeout,)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
294 while not self.eof and apply(select.select, s_args) == s_reply:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
295 i = max(0, len(self.cookedq)-n)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
296 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
297 self.process_rawq()
16488
92cbefdb674a String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15873
diff changeset
298 i = self.cookedq.find(match, i)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
299 if i >= 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
300 i = i+n
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
301 buf = self.cookedq[:i]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
302 self.cookedq = self.cookedq[i:]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
303 return buf
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
304 return self.read_very_lazy()
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
305
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 def read_all(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
307 """Read all data until EOF; block until connection closed."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
308 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
309 while not self.eof:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
310 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
311 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
312 buf = self.cookedq
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
313 self.cookedq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
314 return buf
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
315
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 def read_some(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
317 """Read at least one byte of cooked data unless EOF is hit.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
318
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
319 Return '' if EOF is hit. Block if no data is immediately
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
320 available.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
321
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
322 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
323 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
324 while not self.cookedq and not self.eof:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
325 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
326 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
327 buf = self.cookedq
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
328 self.cookedq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
329 return buf
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
330
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 def read_very_eager(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
332 """Read everything that's possible without blocking in I/O (eager).
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12547
diff changeset
333
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
334 Raise EOFError if connection closed and no cooked data
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
335 available. Return '' if no cooked data available otherwise.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
336 Don't block unless in the midst of an IAC sequence.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
337
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
338 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
339 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
340 while not self.eof and self.sock_avail():
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
341 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
342 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
343 return self.read_very_lazy()
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
344
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 def read_eager(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
346 """Read readily available data.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
347
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
348 Raise EOFError if connection closed and no cooked data
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
349 available. Return '' if no cooked data available otherwise.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
350 Don't block unless in the midst of an IAC sequence.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
351
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
352 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
353 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
354 while not self.cookedq and not self.eof and self.sock_avail():
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
355 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
356 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
357 return self.read_very_lazy()
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
358
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 def read_lazy(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
360 """Process and return data that's already in the queues (lazy).
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12547
diff changeset
361
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
362 Raise EOFError if connection closed and no data available.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
363 Return '' if no cooked data available otherwise. Don't block
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
364 unless in the midst of an IAC sequence.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
365
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
366 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
367 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
368 return self.read_very_lazy()
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
369
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 def read_very_lazy(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
371 """Return any data available in the cooked queue (very lazy).
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
372
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
373 Raise EOFError if connection closed and no data available.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
374 Return '' if no cooked data available otherwise. Don't block.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
375
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
376 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
377 buf = self.cookedq
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
378 self.cookedq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
379 if not buf and self.eof and not self.rawq:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
380 raise EOFError, 'telnet connection closed'
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
381 return buf
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
382
19760
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
383 def set_option_negotiation_callback(self, callback):
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
384 """Provide a callback function called after each receipt of a telnet option."""
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
385 self.option_callback = callback
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
386
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 def process_rawq(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
388 """Transfer from raw queue to cooked queue.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
389
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
390 Set self.eof when connection is closed. Don't block unless in
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
391 the midst of an IAC sequence.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
392
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
393 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
394 buf = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
395 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
396 while self.rawq:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
397 c = self.rawq_getchar()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
398 if c == theNULL:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
399 continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
400 if c == "\021":
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
401 continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
402 if c != IAC:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
403 buf = buf + c
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
404 continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
405 c = self.rawq_getchar()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
406 if c == IAC:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
407 buf = buf + c
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
408 elif c in (DO, DONT):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
409 opt = self.rawq_getchar()
19760
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
410 self.msg('IAC %s %d', c == DO and 'DO' or 'DONT', ord(opt))
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
411 if self.option_callback:
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
412 self.option_callback(self.sock, c, opt)
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
413 else:
21963
2faa23facef7 The Grande 'sendall()' patch, copied from release21-maint. Fixes #516715.
Martin v. Löwis <martin@v.loewis.de>
parents: 20303
diff changeset
414 self.sock.sendall(IAC + WONT + opt)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
415 elif c in (WILL, WONT):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
416 opt = self.rawq_getchar()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
417 self.msg('IAC %s %d',
19760
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
418 c == WILL and 'WILL' or 'WONT', ord(opt))
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
419 if self.option_callback:
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
420 self.option_callback(self.sock, c, opt)
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
421 else:
21963
2faa23facef7 The Grande 'sendall()' patch, copied from release21-maint. Fixes #516715.
Martin v. Löwis <martin@v.loewis.de>
parents: 20303
diff changeset
422 self.sock.sendall(IAC + DONT + opt)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
423 else:
24744
1a3270e26c16 Fix SF bug #593696 telnetlib raises UnboundLocalError
Neal Norwitz <nnorwitz@gmail.com>
parents: 21963
diff changeset
424 self.msg('IAC %d not recognized' % ord(c))
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
425 except EOFError: # raised by self.rawq_getchar()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
426 pass
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
427 self.cookedq = self.cookedq + buf
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
428
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 def rawq_getchar(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
430 """Get next char from raw queue.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
431
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
432 Block if no data is immediately available. Raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
433 when connection is closed.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
434
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
435 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
436 if not self.rawq:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
437 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
438 if self.eof:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
439 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
440 c = self.rawq[self.irawq]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
441 self.irawq = self.irawq + 1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
442 if self.irawq >= len(self.rawq):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
443 self.rawq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
444 self.irawq = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
445 return c
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
446
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 def fill_rawq(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
448 """Fill raw queue from exactly one recv() system call.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
449
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
450 Block if no data is immediately available. Set self.eof when
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
451 connection is closed.
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
452
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
453 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
454 if self.irawq >= len(self.rawq):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
455 self.rawq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
456 self.irawq = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
457 # The buffer size should be fairly small so as to avoid quadratic
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
458 # behavior in process_rawq() above
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
459 buf = self.sock.recv(50)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
460 self.msg("recv %s", `buf`)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
461 self.eof = (not buf)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
462 self.rawq = self.rawq + buf
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
463
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 def sock_avail(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
465 """Test whether data is available on the socket."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
466 return select.select([self], [], [], 0) == ([self], [], [])
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
467
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 def interact(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
469 """Interaction function, emulates a very dumb telnet client."""
9727
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
470 if sys.platform == "win32":
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
471 self.mt_interact()
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
472 return
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
473 while 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
474 rfd, wfd, xfd = select.select([self, sys.stdin], [], [])
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
475 if self in rfd:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
476 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
477 text = self.read_eager()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
478 except EOFError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
479 print '*** Connection closed by remote host ***'
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
480 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
481 if text:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
482 sys.stdout.write(text)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
483 sys.stdout.flush()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
484 if sys.stdin in rfd:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
485 line = sys.stdin.readline()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
486 if not line:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
487 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
488 self.write(line)
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
489
9727
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
490 def mt_interact(self):
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
491 """Multithreaded version of interact()."""
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
492 import thread
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
493 thread.start_new_thread(self.listener, ())
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
494 while 1:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
495 line = sys.stdin.readline()
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
496 if not line:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
497 break
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
498 self.write(line)
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
499
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
500 def listener(self):
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
501 """Helper for mt_interact() -- this executes in the other thread."""
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
502 while 1:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
503 try:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
504 data = self.read_eager()
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
505 except EOFError:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
506 print '*** Connection closed by remote host ***'
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
507 return
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
508 if data:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
509 sys.stdout.write(data)
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
510 else:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
511 sys.stdout.flush()
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
512
6942
06291f04ba9c Added expect() method which takes a list of regular expressions and an
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
513 def expect(self, list, timeout=None):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
514 """Read until one from a list of a regular expressions matches.
6942
06291f04ba9c Added expect() method which takes a list of regular expressions and an
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
515
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
516 The first argument is a list of regular expressions, either
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
517 compiled (re.RegexObject instances) or uncompiled (strings).
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
518 The optional second argument is a timeout, in seconds; default
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
519 is no timeout.
6942
06291f04ba9c Added expect() method which takes a list of regular expressions and an
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
520
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
521 Return a tuple of three items: the index in the list of the
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
522 first regular expression that matches; the match object
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
523 returned; and the text read up till and including the match.
6942
06291f04ba9c Added expect() method which takes a list of regular expressions and an
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
524
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
525 If EOF is read and no text was read, raise EOFError.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
526 Otherwise, when nothing matches, return (-1, None, text) where
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
527 text is the text received so far (may be the empty string if a
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
528 timeout happened).
6942
06291f04ba9c Added expect() method which takes a list of regular expressions and an
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
529
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
530 If a regular expression ends with a greedy match (e.g. '.*')
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
531 or if more than one expression can match the same input, the
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
532 results are undeterministic, and may depend on the I/O timing.
6942
06291f04ba9c Added expect() method which takes a list of regular expressions and an
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
533
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
534 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
535 re = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
536 list = list[:]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
537 indices = range(len(list))
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
538 for i in indices:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
539 if not hasattr(list[i], "search"):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
540 if not re: import re
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
541 list[i] = re.compile(list[i])
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
542 while 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
543 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
544 for i in indices:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
545 m = list[i].search(self.cookedq)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
546 if m:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
547 e = m.end()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
548 text = self.cookedq[:e]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
549 self.cookedq = self.cookedq[e:]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
550 return (i, m, text)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
551 if self.eof:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
552 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
553 if timeout is not None:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
554 r, w, x = select.select([self.fileno()], [], [], timeout)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
555 if not r:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
556 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
557 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
558 text = self.read_very_lazy()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
559 if not text and self.eof:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
560 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
561 return (-1, None, text)
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
562
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
563
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
564 def test():
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
565 """Test program for telnetlib.
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
566
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
567 Usage: python telnetlib.py [-d] ... [host [port]]
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
568
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
569 Default host is localhost; default port is 23.
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
570
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 """
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
572 debuglevel = 0
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
573 while sys.argv[1:] and sys.argv[1] == '-d':
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
574 debuglevel = debuglevel+1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
575 del sys.argv[1]
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
576 host = 'localhost'
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 if sys.argv[1:]:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
578 host = sys.argv[1]
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
579 port = 0
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 if sys.argv[2:]:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
581 portstr = sys.argv[2]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
582 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
583 port = int(portstr)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
584 except ValueError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
585 port = socket.getservbyname(portstr, 'tcp')
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
586 tn = Telnet()
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 tn.set_debuglevel(debuglevel)
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
588 tn.open(host, port)
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
589 tn.interact()
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
590 tn.close()
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
591
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 if __name__ == '__main__':
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 test()