annotate Lib/telnetlib.py @ 27998:e27f58a50737 legacy-trunk

SF bug #690377: Dud footnote reference in distutils docs
author Raymond Hettinger <python@rcn.com>
date Fri, 21 Feb 2003 02:47:09 +0000
parents 98f3fd1ed42f
children 14f65d661084
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 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
29 - 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
30 - 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
31 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
32
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
33 """
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
34
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 # 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
37 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
38 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
39 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
40
16889
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 16488
diff changeset
41 __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
42
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
43 # 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
44 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
45
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 # 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
47 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
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 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
50 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
51 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
52 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
53 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
54 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
55 theNULL = chr(0)
26016
bd677c8e4067 Patch #625823: Add symbolic constants for more commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 26010
diff changeset
56
bd677c8e4067 Patch #625823: Add symbolic constants for more commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 26010
diff changeset
57 SE = chr(240) # Subnegotiation End
bd677c8e4067 Patch #625823: Add symbolic constants for more commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 26010
diff changeset
58 NOP = chr(241) # No Operation
bd677c8e4067 Patch #625823: Add symbolic constants for more commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 26010
diff changeset
59 DM = chr(242) # Data Mark
bd677c8e4067 Patch #625823: Add symbolic constants for more commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 26010
diff changeset
60 BRK = chr(243) # Break
bd677c8e4067 Patch #625823: Add symbolic constants for more commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 26010
diff changeset
61 IP = chr(244) # Interrupt process
bd677c8e4067 Patch #625823: Add symbolic constants for more commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 26010
diff changeset
62 AO = chr(245) # Abort output
bd677c8e4067 Patch #625823: Add symbolic constants for more commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 26010
diff changeset
63 AYT = chr(246) # Are You There
bd677c8e4067 Patch #625823: Add symbolic constants for more commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 26010
diff changeset
64 EC = chr(247) # Erase Character
bd677c8e4067 Patch #625823: Add symbolic constants for more commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 26010
diff changeset
65 EL = chr(248) # Erase Line
bd677c8e4067 Patch #625823: Add symbolic constants for more commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 26010
diff changeset
66 GA = chr(249) # Go Ahead
bd677c8e4067 Patch #625823: Add symbolic constants for more commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 26010
diff changeset
67 SB = chr(250) # Subnegotiation Begin
bd677c8e4067 Patch #625823: Add symbolic constants for more commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 26010
diff changeset
68
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
69
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
70 # 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
71 # 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
72 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
73 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
74 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
75 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
76 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
77 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
78 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
79 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
80 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
81 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
82 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
83 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
84 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
85 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
86 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
87 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
88 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
89 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
90 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
91 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
92 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
93 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
94 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
95 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
96 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
97 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
98 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
99 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
100 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
101 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
102 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
103 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
104 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
105 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
106 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
107 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
108 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
109 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
110 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
111 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
112 # 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
113 # 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
114 # 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
115 # 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
116 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
117 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
118 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
119 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
120 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
121 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
122 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
123 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
124 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
125 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
126 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
127 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
128 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
129 EXOPL = chr(255) # Extended-Options-List
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
130 NOOPT = chr(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
131
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 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
133
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 """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
135
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 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
137 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
138 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
139 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
140 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
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 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
143
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 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
145 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
146 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
147 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
148
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_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
150 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
151 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
152
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_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
154 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
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_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
157 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
158
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 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
160 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
161 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
162
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 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
164 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
165 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
166
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
167 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
168 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
169 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
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 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
172 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
173 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
174
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
175 read_sb_data()
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
176 Reads available data between SB ... SE sequence. Don't block.
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
177
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
178 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
179 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
180 (if set) is called with the following parameters :
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
181 callback(telnet socket, command, option)
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
182 option will be chr(0) when there is no option.
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
183 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
184
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
185 """
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
186
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
187 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
188 """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
189
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
190 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
191 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
192 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
193
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
194 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
195 self.debuglevel = DEBUGLEVEL
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
196 self.host = host
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
197 self.port = port
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
198 self.sock = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
199 self.rawq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
200 self.irawq = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
201 self.cookedq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
202 self.eof = 0
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
203 self.iacseq = '' # Buffer for IAC sequence.
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
204 self.sb = 0 # flag for SB and SE sequence.
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
205 self.sbdataq = ''
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
206 self.option_callback = None
23628
ea979c5206b7 Replaced boolean tests with is None.
Raymond Hettinger <python@rcn.com>
parents: 23434
diff changeset
207 if host is not None:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
208 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
209
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
210 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
211 """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
212
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
213 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
214 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
215
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
216 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
217
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
218 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
219 self.eof = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
220 if not port:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
221 port = TELNET_PORT
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
222 self.host = host
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
223 self.port = port
18902
edf058b16ddf Initialize msg to avoid unbound locals.
Martin v. Löwis <martin@v.loewis.de>
parents: 18834
diff changeset
224 msg = "getaddrinfo returns an empty list"
18834
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
225 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
226 af, socktype, proto, canonname, sa = res
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
227 try:
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
228 self.sock = socket.socket(af, socktype, proto)
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
229 self.sock.connect(sa)
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
230 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
231 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
232 self.sock.close()
18834
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
233 self.sock = None
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
234 continue
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
235 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
236 if not self.sock:
18834
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
237 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
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 __del__(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
240 """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
241 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
242
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 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
244 """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
245
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
246 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
247 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
248
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
249 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
250 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
251 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
252 if args:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
253 print msg % args
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
254 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
255 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
256
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 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
258 """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
259
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
260 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
261
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
262 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
263 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
264
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
265 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
266 """Close the connection."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
267 if self.sock:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
268 self.sock.close()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
269 self.sock = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
270 self.eof = 1
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
271 self.iacseq = ''
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
272 self.sb = 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
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 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
275 """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
276 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
277
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
278 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
279 """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
280 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
281
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
282 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
283 """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
284
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
285 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
286 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
287
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
288 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
289 if IAC in buffer:
16488
92cbefdb674a String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15873
diff changeset
290 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
291 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
292 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
293
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
294 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
295 """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
296
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
297 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
298 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
299 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
300
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
301 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
302 n = len(match)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
303 self.process_rawq()
16488
92cbefdb674a String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15873
diff changeset
304 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
305 if i >= 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
306 i = i+n
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
307 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
308 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
309 return buf
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
310 s_reply = ([self], [], [])
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
311 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
312 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
313 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
314 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
315 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
316 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
317 self.process_rawq()
16488
92cbefdb674a String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15873
diff changeset
318 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
319 if i >= 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
320 i = i+n
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
321 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
322 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
323 return buf
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
324 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
325
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
326 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
327 """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
328 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
329 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
330 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
331 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
332 buf = self.cookedq
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
333 self.cookedq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
334 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
335
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
336 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
337 """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
338
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
339 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
340 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
341
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
342 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
343 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
344 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
345 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
346 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
347 buf = self.cookedq
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
348 self.cookedq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
349 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
350
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 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
352 """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
353
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
354 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
355 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
356 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
357
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
358 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
359 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
360 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
361 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
362 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
363 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
364
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 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
366 """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
367
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
368 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
369 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
370 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
371
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
372 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
373 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
374 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
375 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
376 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
377 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
378
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
379 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
380 """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
381
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
382 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
383 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
384 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
385
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
386 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
387 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
388 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
389
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
390 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
391 """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
392
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
393 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
394 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
395
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
396 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
397 buf = self.cookedq
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
398 self.cookedq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
399 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
400 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
401 return buf
26126
98f3fd1ed42f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26016
diff changeset
402
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
403 def read_sb_data(self):
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
404 """Return any data available in the SB ... SE queue.
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
405
26126
98f3fd1ed42f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26016
diff changeset
406 Return '' if no SB ... SE available. Should only be called
98f3fd1ed42f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26016
diff changeset
407 after seeing a SB or SE command. When a new SB command is
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
408 found, old unread SB data will be discarded. Don't block.
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
409
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
410 """
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
411 buf = self.sbdataq
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
412 self.sbdataq = ''
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
413 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
414
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
415 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
416 """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
417 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
418
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
419 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
420 """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
421
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
422 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
423 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
424
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
425 """
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
426 buf = ['', '']
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
427 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
428 while self.rawq:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
429 c = self.rawq_getchar()
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
430 if not self.iacseq:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
431 if c == theNULL:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
432 continue
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
433 if c == "\021":
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
434 continue
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
435 if c != IAC:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
436 buf[self.sb] = buf[self.sb] + c
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
437 continue
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
438 else:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
439 self.iacseq += c
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
440 elif len(self.iacseq) == 1:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
441 'IAC: IAC CMD [OPTION only for WILL/WONT/DO/DONT]'
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
442 if c in (DO, DONT, WILL, WONT):
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
443 self.iacseq += c
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
444 continue
26126
98f3fd1ed42f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26016
diff changeset
445
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
446 self.iacseq = ''
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
447 if c == IAC:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
448 buf[self.sb] = buf[self.sb] + c
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
449 else:
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
450 if c == SB: # SB ... SE start.
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
451 self.sb = 1
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
452 self.sbdataq = ''
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
453 elif c == SE:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
454 self.sb = 0
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
455 self.sbdataq = self.sbdataq + buf[1]
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
456 buf[1] = ''
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
457 if self.option_callback:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
458 # Callback is supposed to look into
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
459 # the sbdataq
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
460 self.option_callback(self.sock, c, NOOPT)
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
461 else:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
462 # We can't offer automatic processing of
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
463 # suboptions. Alas, we should not get any
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
464 # unless we did a WILL/DO before.
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
465 self.msg('IAC %d not recognized' % ord(c))
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
466 elif len(self.iacseq) == 2:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
467 cmd = self.iacseq[1]
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
468 self.iacseq = ''
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
469 opt = c
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
470 if cmd in (DO, DONT):
26126
98f3fd1ed42f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26016
diff changeset
471 self.msg('IAC %s %d',
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
472 cmd == DO and 'DO' or 'DONT', ord(opt))
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
473 if self.option_callback:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
474 self.option_callback(self.sock, cmd, opt)
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
475 else:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
476 self.sock.sendall(IAC + WONT + opt)
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
477 elif cmd in (WILL, WONT):
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
478 self.msg('IAC %s %d',
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
479 cmd == WILL and 'WILL' or 'WONT', ord(opt))
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
480 if self.option_callback:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
481 self.option_callback(self.sock, cmd, opt)
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
482 else:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
483 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
484 except EOFError: # raised by self.rawq_getchar()
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
485 self.iacseq = '' # Reset on EOF
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
486 self.sb = 0
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
487 pass
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
488 self.cookedq = self.cookedq + buf[0]
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
489 self.sbdataq = self.sbdataq + buf[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
490
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
491 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
492 """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
493
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
494 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
495 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
496
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
497 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
498 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
499 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
500 if self.eof:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
501 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
502 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
503 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
504 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
505 self.rawq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
506 self.irawq = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
507 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
508
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
509 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
510 """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
511
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
512 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
513 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
514
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
515 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
516 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
517 self.rawq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
518 self.irawq = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
519 # 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
520 # 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
521 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
522 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
523 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
524 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
525
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
526 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
527 """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
528 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
529
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
530 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
531 """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
532 if sys.platform == "win32":
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
533 self.mt_interact()
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
534 return
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
535 while 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
536 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
537 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
538 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
539 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
540 except EOFError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
541 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
542 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
543 if text:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
544 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
545 sys.stdout.flush()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
546 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
547 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
548 if not line:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
549 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
550 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
551
9727
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
552 def mt_interact(self):
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
553 """Multithreaded version of interact()."""
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
554 import thread
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
555 thread.start_new_thread(self.listener, ())
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
556 while 1:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
557 line = sys.stdin.readline()
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
558 if not line:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
559 break
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
560 self.write(line)
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
561
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
562 def listener(self):
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
563 """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
564 while 1:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
565 try:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
566 data = self.read_eager()
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
567 except EOFError:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
568 print '*** Connection closed by remote host ***'
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
569 return
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
570 if data:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
571 sys.stdout.write(data)
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
572 else:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
573 sys.stdout.flush()
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
574
6942
06291f04ba9c Added expect() method which takes a list of regular expressions and an
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
575 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
576 """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
577
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
578 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
579 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
580 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
581 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
582
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
583 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
584 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
585 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
586
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
587 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
588 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
589 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
590 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
591
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
592 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
593 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
594 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
595
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
596 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
597 re = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
598 list = list[:]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
599 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
600 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
601 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
602 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
603 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
604 while 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
605 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
606 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
607 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
608 if m:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
609 e = m.end()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
610 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
611 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
612 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
613 if self.eof:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
614 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
615 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
616 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
617 if not r:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
618 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
619 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
620 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
621 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
622 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
623 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
624
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
625
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
626 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
627 """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
628
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
629 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
630
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
631 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
632
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
633 """
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
634 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
635 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
636 debuglevel = debuglevel+1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
637 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
638 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
639 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
640 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
641 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
642 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
643 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
644 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
645 port = int(portstr)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
646 except ValueError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
647 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
648 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
649 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
650 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
651 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
652 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
653
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
654 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
655 test()