annotate Lib/telnetlib.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents e2c5ae30cffe
children 8ccf58b0a5da
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35525
e73accf3eddb Quote docstring with r"" to keep '\r\n' in example code.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31515
diff changeset
1 r"""TELNET client class.
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
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
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 41323
diff changeset
187 def __init__(self, host=None, port=0,
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 41323
diff changeset
188 timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
189 """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
190
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
191 When called without arguments, create an unconnected instance.
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 41323
diff changeset
192 With a hostname argument, it connects the instance; port number
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 41323
diff changeset
193 and timeout are optional.
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
41323
3fd8f06577ff Added timout parameter to telnetlib.Telnet. Also created
Facundo Batista <facundobatista@gmail.com>
parents: 38992
diff changeset
198 self.timeout = timeout
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
199 self.sock = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
200 self.rawq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
201 self.irawq = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
202 self.cookedq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
203 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
204 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
205 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
206 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
207 self.option_callback = None
23628
ea979c5206b7 Replaced boolean tests with is None.
Raymond Hettinger <python@rcn.com>
parents: 23434
diff changeset
208 if host is not None:
41323
3fd8f06577ff Added timout parameter to telnetlib.Telnet. Also created
Facundo Batista <facundobatista@gmail.com>
parents: 38992
diff changeset
209 self.open(host, port, 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
210
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 41323
diff changeset
211 def open(self, host, port=0, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
212 """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
213
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
214 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
215 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
216
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
217 Don't try to reopen an already connected instance.
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
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 41323
diff changeset
224 self.timeout = timeout
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 41323
diff changeset
225 self.sock = socket.create_connection((host, port), 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
226
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 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
228 """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
229 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
230
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 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
232 """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
233
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
234 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
235 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
236
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
237 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
238 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
239 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
240 if args:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
241 print msg % args
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
242 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
243 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
244
1dafad307006 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 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
246 """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
247
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
248 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
249
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
250 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
251 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
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
253 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
254 """Close the connection."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
255 if self.sock:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
256 self.sock.close()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
257 self.sock = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
258 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
259 self.iacseq = ''
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
260 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
261
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 def 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
263 """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
264 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
265
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 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
267 """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
268 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
269
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 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
271 """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
272
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
273 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
274 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
275
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
276 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
277 if IAC in buffer:
16488
92cbefdb674a String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15873
diff changeset
278 buffer = buffer.replace(IAC, IAC+IAC)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 28089
diff changeset
279 self.msg("send %r", 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
280 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
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 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
283 """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
284
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
285 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
286 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
287 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
288
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
289 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
290 n = len(match)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
291 self.process_rawq()
16488
92cbefdb674a String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15873
diff changeset
292 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
293 if i >= 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
294 i = i+n
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
295 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
296 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
297 return buf
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
298 s_reply = ([self], [], [])
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
299 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
300 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
301 s_args = s_args + (timeout,)
38992
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
302 from time import time
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
303 time_start = time()
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 26126
diff changeset
304 while not self.eof and select.select(*s_args) == s_reply:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
305 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
306 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
307 self.process_rawq()
16488
92cbefdb674a String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15873
diff changeset
308 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
309 if i >= 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
310 i = i+n
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
311 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
312 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
313 return buf
38992
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
314 if timeout is not None:
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
315 elapsed = time() - time_start
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
316 if elapsed >= timeout:
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
317 break
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
318 s_args = s_reply + (timeout-elapsed,)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
319 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
320
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 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
322 """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
323 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
324 while not self.eof:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
325 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
326 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
327 buf = self.cookedq
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
328 self.cookedq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
329 return buf
6940
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
330
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 def read_some(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
332 """Read 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
333
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
334 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
335 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
336
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
337 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
338 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
339 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
340 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
341 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
342 buf = self.cookedq
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
343 self.cookedq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
344 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
345
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 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
347 """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
348
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
349 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
350 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
351 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
352
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
353 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
354 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
355 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
356 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
357 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
358 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
359
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 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
361 """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
362
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
363 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
364 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
365 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
366
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
367 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
368 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
369 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
370 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
371 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
372 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
373
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 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
375 """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
376
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
377 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
378 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
379 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
380
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
381 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
382 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
383 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
384
1dafad307006 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 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
386 """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
387
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
388 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
389 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
390
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
391 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
392 buf = self.cookedq
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
393 self.cookedq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
394 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
395 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
396 return buf
26126
98f3fd1ed42f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26016
diff changeset
397
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
398 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
399 """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
400
26126
98f3fd1ed42f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26016
diff changeset
401 Return '' if no SB ... SE available. Should only be called
98f3fd1ed42f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26016
diff changeset
402 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
403 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
404
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
405 """
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
406 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
407 self.sbdataq = ''
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
408 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
409
19760
96da985cde98 Patch #416079: fix the debug string output when receiving telnet commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 18902
diff changeset
410 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
411 """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
412 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
413
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 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
415 """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
416
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
417 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
418 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
419
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
420 """
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
421 buf = ['', '']
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
422 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
423 while self.rawq:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
424 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
425 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
426 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
427 continue
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
428 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
429 continue
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
430 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
431 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
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 else:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
434 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
435 elif len(self.iacseq) == 1:
37546
edc6a0cc8466 Fix a couple of strings that were no-ops. urllib.open_file was a docstring
Neal Norwitz <nnorwitz@gmail.com>
parents: 35525
diff changeset
436 # 'IAC: IAC CMD [OPTION only for WILL/WONT/DO/DONT]'
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
437 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
438 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
439 continue
26126
98f3fd1ed42f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26016
diff changeset
440
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
441 self.iacseq = ''
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 == IAC:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
443 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
444 else:
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
445 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
446 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
447 self.sbdataq = ''
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
448 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
449 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
450 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
451 buf[1] = ''
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
452 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
453 # 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
454 # the sbdataq
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
455 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
456 else:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
457 # 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
458 # 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
459 # 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
460 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
461 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
462 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
463 self.iacseq = ''
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
464 opt = c
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
465 if cmd in (DO, DONT):
26126
98f3fd1ed42f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26016
diff changeset
466 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
467 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
468 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
469 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
470 else:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
471 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
472 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
473 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
474 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
475 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
476 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
477 else:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
478 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
479 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
480 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
481 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
482 pass
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
483 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
484 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
485
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
486 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
487 """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
488
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
489 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
490 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
491
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
492 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
493 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
494 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
495 if self.eof:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
496 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
497 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
498 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
499 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
500 self.rawq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
501 self.irawq = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
502 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
503
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 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
505 """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
506
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
507 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
508 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
509
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
510 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
511 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
512 self.rawq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
513 self.irawq = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
514 # 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
515 # 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
516 buf = self.sock.recv(50)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 28089
diff changeset
517 self.msg("recv %r", buf)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
518 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
519 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
520
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 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
522 """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
523 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
524
1dafad307006 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 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
526 """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
527 if sys.platform == "win32":
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
528 self.mt_interact()
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
529 return
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
530 while 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
531 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
532 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
533 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
534 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
535 except EOFError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
536 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
537 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
538 if text:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
539 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
540 sys.stdout.flush()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
541 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
542 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
543 if not line:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
544 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
545 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
546
9727
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
547 def mt_interact(self):
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
548 """Multithreaded version of interact()."""
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
549 import thread
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
550 thread.start_new_thread(self.listener, ())
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
551 while 1:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
552 line = sys.stdin.readline()
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
553 if not line:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
554 break
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
555 self.write(line)
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
556
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
557 def listener(self):
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
558 """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
559 while 1:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
560 try:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
561 data = self.read_eager()
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
562 except EOFError:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
563 print '*** Connection closed by remote host ***'
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
564 return
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
565 if data:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
566 sys.stdout.write(data)
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
567 else:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
568 sys.stdout.flush()
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
569
6942
06291f04ba9c Added expect() method which takes a list of regular expressions and an
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
570 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
571 """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
572
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
573 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
574 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
575 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
576 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
577
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
578 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
579 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
580 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
581
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
582 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
583 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
584 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
585 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
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 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
588 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
589 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
590
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
591 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
592 re = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
593 list = list[:]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
594 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
595 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
596 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
597 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
598 list[i] = re.compile(list[i])
38992
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
599 if timeout is not None:
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
600 from time import time
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
601 time_start = time()
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
602 while 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
603 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
604 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
605 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
606 if m:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
607 e = m.end()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
608 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
609 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
610 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
611 if self.eof:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
612 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
613 if timeout is not None:
38992
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
614 elapsed = time() - time_start
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
615 if elapsed >= timeout:
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
616 break
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
617 s_args = ([self.fileno()], [], [], timeout-elapsed)
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
618 r, w, x = select.select(*s_args)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
619 if not r:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
620 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
621 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
622 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
623 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
624 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
625 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
626
1dafad307006 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
1dafad307006 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 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
629 """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
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 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
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 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
634
1dafad307006 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 """
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 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
637 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
638 debuglevel = debuglevel+1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
639 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
640 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
641 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
642 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
643 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
644 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
645 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
646 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
647 port = int(portstr)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
648 except ValueError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
649 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
650 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
651 tn.set_debuglevel(debuglevel)
41323
3fd8f06577ff Added timout parameter to telnetlib.Telnet. Also created
Facundo Batista <facundobatista@gmail.com>
parents: 38992
diff changeset
652 tn.open(host, port, timeout=0.5)
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
653 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
654 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
655
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
656 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
657 test()