annotate Lib/telnetlib.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents c53e3aacb816
children
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
78127
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
37 import errno
6942
06291f04ba9c Added expect() method which takes a list of regular expressions and an
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
38 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
39 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
40 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
41
16889
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 16488
diff changeset
42 __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
43
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
44 # 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
45 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
46
1dafad307006 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 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
48 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
49
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 # Telnet 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
51 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
52 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
53 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
54 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
55 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
56 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
57
bd677c8e4067 Patch #625823: Add symbolic constants for more commands.
Martin v. Löwis <martin@v.loewis.de>
parents: 26010
diff changeset
58 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
59 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
60 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
61 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
62 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
63 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
64 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
65 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
66 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
67 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
68 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
69
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
70
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
71 # 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
72 # 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
73 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
74 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
75 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
76 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
77 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
78 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
79 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
80 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
81 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
82 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
83 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
84 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
85 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
86 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
87 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
88 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
89 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
90 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
91 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
92 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
93 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
94 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
95 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
96 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
97 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
98 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
99 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
100 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
101 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
102 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
103 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
104 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
105 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
106 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
107 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
108 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
109 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
110 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
111 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
112 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
113 # 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
114 # 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
115 # 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
116 # 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
117 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
118 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
119 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
120 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
121 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
122 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
123 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
124 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
125 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
126 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
127 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
128 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
129 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
130 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
131 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
132
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 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
134
1dafad307006 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 """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
136
1dafad307006 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 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
138 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
139 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
140 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
141 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
142
1dafad307006 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 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
144
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 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
146 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
147 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
148 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
149
1dafad307006 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(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
151 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
152 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
153
1dafad307006 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()
1dafad307006 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 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
156
1dafad307006 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_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
158 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
159
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 read_very_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
161 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
162 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
163
1dafad307006 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_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
165 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
166 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
167
1dafad307006 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_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
169 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
170 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
171
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 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
173 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
174 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
175
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
176 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
177 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
178
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
179 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
180 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
181 (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
182 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
183 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
184 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
185
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
186 """
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
187
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 41323
diff changeset
188 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
189 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
190 """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
191
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
192 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
193 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
194 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
195 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
196 self.debuglevel = DEBUGLEVEL
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
197 self.host = host
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
198 self.port = port
41323
3fd8f06577ff Added timout parameter to telnetlib.Telnet. Also created
Facundo Batista <facundobatista@gmail.com>
parents: 38992
diff changeset
199 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
200 self.sock = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
201 self.rawq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
202 self.irawq = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
203 self.cookedq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
204 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
205 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
206 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
207 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
208 self.option_callback = None
78127
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
209 self._has_poll = hasattr(select, 'poll')
23628
ea979c5206b7 Replaced boolean tests with is None.
Raymond Hettinger <python@rcn.com>
parents: 23434
diff changeset
210 if host is not None:
41323
3fd8f06577ff Added timout parameter to telnetlib.Telnet. Also created
Facundo Batista <facundobatista@gmail.com>
parents: 38992
diff changeset
211 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
212
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 41323
diff changeset
213 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
214 """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
215
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
216 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
217 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
218
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
219 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
220 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
221 self.eof = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
222 if not port:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
223 port = TELNET_PORT
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
224 self.host = host
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
225 self.port = port
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 41323
diff changeset
226 self.timeout = timeout
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 41323
diff changeset
227 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
228
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 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
230 """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
231 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
232
1dafad307006 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 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
234 """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
235
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
236 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
237 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
238
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
239 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
240 if self.debuglevel > 0:
66892
8ccf58b0a5da Merged revisions 87230 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 47372
diff changeset
241 print 'Telnet(%s,%s):' % (self.host, self.port),
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
242 if args:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
243 print msg % args
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
244 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
245 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
246
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 def 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
248 """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
249
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
250 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
251
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
252 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
253 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
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
255 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
256 """Close the connection."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
257 if self.sock:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
258 self.sock.close()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
259 self.sock = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
260 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
261 self.iacseq = ''
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
262 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
263
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 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
265 """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
266 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
267
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 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
269 """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
270 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
271
1dafad307006 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 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
273 """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
274
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
275 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
276 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
277
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
278 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
279 if IAC in buffer:
16488
92cbefdb674a String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15873
diff changeset
280 buffer = buffer.replace(IAC, IAC+IAC)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 28089
diff changeset
281 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
282 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
283
1dafad307006 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 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
285 """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
286
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
287 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
288 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
289 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
290
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
291 """
78127
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
292 if self._has_poll:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
293 return self._read_until_with_poll(match, timeout)
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
294 else:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
295 return self._read_until_with_select(match, timeout)
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
296
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
297 def _read_until_with_poll(self, match, timeout):
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
298 """Read until a given string is encountered or until timeout.
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
299
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
300 This method uses select.poll() to implement the timeout.
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
301 """
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
302 n = len(match)
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
303 call_timeout = timeout
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
304 if timeout is not None:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
305 from time import time
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
306 time_start = time()
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
307 self.process_rawq()
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
308 i = self.cookedq.find(match)
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
309 if i < 0:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
310 poller = select.poll()
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
311 poll_in_or_priority_flags = select.POLLIN | select.POLLPRI
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
312 poller.register(self, poll_in_or_priority_flags)
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
313 while i < 0 and not self.eof:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
314 try:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
315 ready = poller.poll(call_timeout)
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
316 except select.error as e:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
317 if e.errno == errno.EINTR:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
318 if timeout is not None:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
319 elapsed = time() - time_start
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
320 call_timeout = timeout-elapsed
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
321 continue
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
322 raise
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
323 for fd, mode in ready:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
324 if mode & poll_in_or_priority_flags:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
325 i = max(0, len(self.cookedq)-n)
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
326 self.fill_rawq()
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
327 self.process_rawq()
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
328 i = self.cookedq.find(match, i)
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
329 if timeout is not None:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
330 elapsed = time() - time_start
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
331 if elapsed >= timeout:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
332 break
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
333 call_timeout = timeout-elapsed
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
334 poller.unregister(self)
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
335 if i >= 0:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
336 i = i + n
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
337 buf = self.cookedq[:i]
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
338 self.cookedq = self.cookedq[i:]
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
339 return buf
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
340 return self.read_very_lazy()
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
341
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
342 def _read_until_with_select(self, match, timeout=None):
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
343 """Read until a given string is encountered or until timeout.
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
344
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
345 The timeout is implemented using select.select().
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
346 """
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
347 n = len(match)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
348 self.process_rawq()
16488
92cbefdb674a String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15873
diff changeset
349 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
350 if i >= 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
351 i = i+n
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
352 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
353 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
354 return buf
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
355 s_reply = ([self], [], [])
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
356 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
357 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
358 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
359 from time import time
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
360 time_start = time()
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 26126
diff changeset
361 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
362 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
363 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
364 self.process_rawq()
16488
92cbefdb674a String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15873
diff changeset
365 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
366 if i >= 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
367 i = i+n
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
368 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
369 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
370 return buf
38992
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
371 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
372 elapsed = time() - time_start
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
373 if elapsed >= timeout:
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
374 break
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
375 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
376 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
377
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 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
379 """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
380 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
381 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
382 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
383 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
384 buf = self.cookedq
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
385 self.cookedq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
386 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
387
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 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
389 """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
390
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
391 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
392 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
393
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
394 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
395 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
396 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
397 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
398 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
399 buf = self.cookedq
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
400 self.cookedq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
401 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
402
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 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
404 """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
405
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
406 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
407 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
408 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
409
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
410 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
411 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
412 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
413 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
414 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
415 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
416
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 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
418 """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
419
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
420 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
421 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
422 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
423
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
424 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
425 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
426 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
427 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
428 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
429 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
430
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 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
432 """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
433
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
434 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
435 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
436 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
437
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
438 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
439 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
440 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
441
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 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
443 """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
444
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
445 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
446 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
447
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
448 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
449 buf = self.cookedq
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
450 self.cookedq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
451 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
452 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
453 return buf
26126
98f3fd1ed42f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26016
diff changeset
454
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
455 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
456 """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
457
26126
98f3fd1ed42f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26016
diff changeset
458 Return '' if no SB ... SE available. Should only be called
98f3fd1ed42f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26016
diff changeset
459 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
460 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
461
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
462 """
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
463 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
464 self.sbdataq = ''
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
465 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
466
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
467 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
468 """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
469 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
470
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
471 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
472 """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
473
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
474 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
475 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
476
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
477 """
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
478 buf = ['', '']
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
479 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
480 while self.rawq:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
481 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
482 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
483 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
484 continue
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
485 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
486 continue
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
487 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
488 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
489 continue
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
490 else:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
491 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
492 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
493 # '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
494 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
495 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
496 continue
26126
98f3fd1ed42f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26016
diff changeset
497
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
498 self.iacseq = ''
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
499 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
500 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
501 else:
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
502 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
503 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
504 self.sbdataq = ''
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
505 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
506 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
507 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
508 buf[1] = ''
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
509 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
510 # 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
511 # the sbdataq
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
512 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
513 else:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
514 # 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
515 # 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
516 # 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
517 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
518 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
519 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
520 self.iacseq = ''
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
521 opt = c
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
522 if cmd in (DO, DONT):
26126
98f3fd1ed42f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26016
diff changeset
523 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
524 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
525 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
526 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
527 else:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
528 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
529 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
530 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
531 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
532 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
533 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
534 else:
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
535 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
536 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
537 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
538 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
539 pass
26010
36ea31bc9076 Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents: 23628
diff changeset
540 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
541 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
542
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
543 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
544 """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
545
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
546 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
547 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
548
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
549 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
550 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
551 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
552 if self.eof:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
553 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
554 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
555 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
556 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
557 self.rawq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
558 self.irawq = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
559 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
560
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
561 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
562 """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
563
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
564 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
565 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
566
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
567 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
568 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
569 self.rawq = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
570 self.irawq = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
571 # 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
572 # 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
573 buf = self.sock.recv(50)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 28089
diff changeset
574 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
575 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
576 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
577
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 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
579 """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
580 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
581
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
582 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
583 """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
584 if sys.platform == "win32":
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
585 self.mt_interact()
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
586 return
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
587 while 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
588 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
589 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
590 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
591 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
592 except EOFError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
593 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
594 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
595 if text:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
596 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
597 sys.stdout.flush()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
598 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
599 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
600 if not line:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
601 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
602 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
603
9727
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
604 def mt_interact(self):
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
605 """Multithreaded version of interact()."""
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
606 import thread
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
607 thread.start_new_thread(self.listener, ())
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
608 while 1:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
609 line = sys.stdin.readline()
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
610 if not line:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
611 break
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
612 self.write(line)
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
613
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
614 def listener(self):
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
615 """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
616 while 1:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
617 try:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
618 data = self.read_eager()
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
619 except EOFError:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
620 print '*** Connection closed by remote host ***'
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
621 return
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
622 if data:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
623 sys.stdout.write(data)
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
624 else:
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
625 sys.stdout.flush()
eaab8feb7fc0 Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
626
6942
06291f04ba9c Added expect() method which takes a list of regular expressions and an
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
627 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
628 """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
629
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
630 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
631 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
632 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
633 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
634
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
635 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
636 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
637 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
638
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
639 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
640 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
641 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
642 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
643
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
644 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
645 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
646 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
647
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
648 """
78127
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
649 if self._has_poll:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
650 return self._expect_with_poll(list, timeout)
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
651 else:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
652 return self._expect_with_select(list, timeout)
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
653
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
654 def _expect_with_poll(self, expect_list, timeout=None):
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
655 """Read until one from a list of a regular expressions matches.
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
656
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
657 This method uses select.poll() to implement the timeout.
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
658 """
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
659 re = None
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
660 expect_list = expect_list[:]
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
661 indices = range(len(expect_list))
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
662 for i in indices:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
663 if not hasattr(expect_list[i], "search"):
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
664 if not re: import re
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
665 expect_list[i] = re.compile(expect_list[i])
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
666 call_timeout = timeout
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
667 if timeout is not None:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
668 from time import time
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
669 time_start = time()
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
670 self.process_rawq()
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
671 m = None
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
672 for i in indices:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
673 m = expect_list[i].search(self.cookedq)
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
674 if m:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
675 e = m.end()
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
676 text = self.cookedq[:e]
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
677 self.cookedq = self.cookedq[e:]
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
678 break
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
679 if not m:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
680 poller = select.poll()
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
681 poll_in_or_priority_flags = select.POLLIN | select.POLLPRI
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
682 poller.register(self, poll_in_or_priority_flags)
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
683 while not m and not self.eof:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
684 try:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
685 ready = poller.poll(call_timeout)
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
686 except select.error as e:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
687 if e.errno == errno.EINTR:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
688 if timeout is not None:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
689 elapsed = time() - time_start
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
690 call_timeout = timeout-elapsed
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
691 continue
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
692 raise
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
693 for fd, mode in ready:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
694 if mode & poll_in_or_priority_flags:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
695 self.fill_rawq()
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
696 self.process_rawq()
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
697 for i in indices:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
698 m = expect_list[i].search(self.cookedq)
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
699 if m:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
700 e = m.end()
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
701 text = self.cookedq[:e]
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
702 self.cookedq = self.cookedq[e:]
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
703 break
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
704 if timeout is not None:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
705 elapsed = time() - time_start
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
706 if elapsed >= timeout:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
707 break
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
708 call_timeout = timeout-elapsed
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
709 poller.unregister(self)
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
710 if m:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
711 return (i, m, text)
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
712 text = self.read_very_lazy()
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
713 if not text and self.eof:
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
714 raise EOFError
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
715 return (-1, None, text)
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
716
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
717 def _expect_with_select(self, list, timeout=None):
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
718 """Read until one from a list of a regular expressions matches.
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
719
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
720 The timeout is implemented using select.select().
c53e3aacb816 Fixes Issue #14635: telnetlib will use poll() rather than select() when possible
Gregory P. Smith <greg@krypto.org>
parents: 66892
diff changeset
721 """
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
722 re = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
723 list = list[:]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
724 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
725 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
726 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
727 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
728 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
729 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
730 from time import time
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
731 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
732 while 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
733 self.process_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
734 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
735 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
736 if m:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
737 e = m.end()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
738 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
739 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
740 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
741 if self.eof:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
742 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
743 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
744 elapsed = time() - time_start
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
745 if elapsed >= timeout:
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
746 break
093156899f75 Patch #825417: Fix timeout processing in expect,
Martin v. Löwis <martin@v.loewis.de>
parents: 37546
diff changeset
747 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
748 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
749 if not r:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
750 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
751 self.fill_rawq()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
752 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
753 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
754 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
755 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
756
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
757
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
758 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
759 """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
760
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
761 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
762
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
763 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
764
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
765 """
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
766 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
767 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
768 debuglevel = debuglevel+1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
769 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
770 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
771 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
772 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
773 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
774 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
775 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
776 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
777 port = int(portstr)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
778 except ValueError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7414
diff changeset
779 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
780 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
781 tn.set_debuglevel(debuglevel)
41323
3fd8f06577ff Added timout parameter to telnetlib.Telnet. Also created
Facundo Batista <facundobatista@gmail.com>
parents: 38992
diff changeset
782 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
783 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
784 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
785
1dafad307006 Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff changeset
786 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
787 test()