Mercurial > cpython-lru_cache-weakref
annotate Lib/telnetlib.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | d0f90d3f6203 |
| children |
| 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 |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
10 >>> tn.write(b'guido\r\n') |
|
40785
22760d53d4da
Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
11 >>> print(tn.read_all()) |
|
6940
1dafad307006
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 |
|
86549
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
20 It is possible to pass a Telnet object to a selector in order to wait until |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
21 more data is available. Note that in this case, read_eager() may return b'' |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
22 even if there was data on the socket, because the protocol negotiation may have |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
23 eaten the data. This is why EOFError is needed in some cases to distinguish |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
24 between "no data" and "connection closed" (since the socket also appears ready |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
25 for reading when it 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
|
26 |
|
1dafad307006
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 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
|
28 - 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
|
29 - 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
|
30 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
|
31 |
|
1dafad307006
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 # Imported modules |
|
6942
06291f04ba9c
Added expect() method which takes a list of regular expressions and an
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
36 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
|
37 import socket |
|
86549
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
38 import selectors |
|
86646
ea3deb022890
Close #19339: telnetlib module is now using time.monotonic() when available to
Victor Stinner <victor.stinner@gmail.com>
parents:
78129
diff
changeset
|
39 try: |
|
ea3deb022890
Close #19339: telnetlib module is now using time.monotonic() when available to
Victor Stinner <victor.stinner@gmail.com>
parents:
78129
diff
changeset
|
40 from time import monotonic as _time |
|
ea3deb022890
Close #19339: telnetlib module is now using time.monotonic() when available to
Victor Stinner <victor.stinner@gmail.com>
parents:
78129
diff
changeset
|
41 except ImportError: |
|
ea3deb022890
Close #19339: telnetlib module is now using time.monotonic() when available to
Victor Stinner <victor.stinner@gmail.com>
parents:
78129
diff
changeset
|
42 from time import time as _time |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 |
|
16889
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
16488
diff
changeset
|
44 __all__ = ["Telnet"] |
|
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
16488
diff
changeset
|
45 |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 # Tunable parameters |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 DEBUGLEVEL = 0 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 # Telnet protocol defaults |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 TELNET_PORT = 23 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 # Telnet protocol characters (don't change) |
|
53021
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
53 IAC = bytes([255]) # "Interpret As Command" |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
54 DONT = bytes([254]) |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
55 DO = bytes([253]) |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
56 WONT = bytes([252]) |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
57 WILL = bytes([251]) |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
58 theNULL = bytes([0]) |
|
26016
bd677c8e4067
Patch #625823: Add symbolic constants for more commands.
Martin v. Löwis <martin@v.loewis.de>
parents:
26010
diff
changeset
|
59 |
|
53021
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
60 SE = bytes([240]) # Subnegotiation End |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
61 NOP = bytes([241]) # No Operation |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
62 DM = bytes([242]) # Data Mark |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
63 BRK = bytes([243]) # Break |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
64 IP = bytes([244]) # Interrupt process |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
65 AO = bytes([245]) # Abort output |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
66 AYT = bytes([246]) # Are You There |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
67 EC = bytes([247]) # Erase Character |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
68 EL = bytes([248]) # Erase Line |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
69 GA = bytes([249]) # Go Ahead |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
70 SB = bytes([250]) # Subnegotiation Begin |
|
26016
bd677c8e4067
Patch #625823: Add symbolic constants for more commands.
Martin v. Löwis <martin@v.loewis.de>
parents:
26010
diff
changeset
|
71 |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 |
|
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
|
73 # 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
|
74 # These ones all come from arpa/telnet.h |
|
53021
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
75 BINARY = bytes([0]) # 8-bit data path |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
76 ECHO = bytes([1]) # echo |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
77 RCP = bytes([2]) # prepare to reconnect |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
78 SGA = bytes([3]) # suppress go ahead |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
79 NAMS = bytes([4]) # approximate message size |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
80 STATUS = bytes([5]) # give status |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
81 TM = bytes([6]) # timing mark |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
82 RCTE = bytes([7]) # remote controlled transmission and echo |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
83 NAOL = bytes([8]) # negotiate about output line width |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
84 NAOP = bytes([9]) # negotiate about output page size |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
85 NAOCRD = bytes([10]) # negotiate about CR disposition |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
86 NAOHTS = bytes([11]) # negotiate about horizontal tabstops |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
87 NAOHTD = bytes([12]) # negotiate about horizontal tab disposition |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
88 NAOFFD = bytes([13]) # negotiate about formfeed disposition |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
89 NAOVTS = bytes([14]) # negotiate about vertical tab stops |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
90 NAOVTD = bytes([15]) # negotiate about vertical tab disposition |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
91 NAOLFD = bytes([16]) # negotiate about output LF disposition |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
92 XASCII = bytes([17]) # extended ascii character set |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
93 LOGOUT = bytes([18]) # force logout |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
94 BM = bytes([19]) # byte macro |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
95 DET = bytes([20]) # data entry terminal |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
96 SUPDUP = bytes([21]) # supdup protocol |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
97 SUPDUPOUTPUT = bytes([22]) # supdup output |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
98 SNDLOC = bytes([23]) # send location |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
99 TTYPE = bytes([24]) # terminal type |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
100 EOR = bytes([25]) # end or record |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
101 TUID = bytes([26]) # TACACS user identification |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
102 OUTMRK = bytes([27]) # output marking |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
103 TTYLOC = bytes([28]) # terminal location number |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
104 VT3270REGIME = bytes([29]) # 3270 regime |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
105 X3PAD = bytes([30]) # X.3 PAD |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
106 NAWS = bytes([31]) # window size |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
107 TSPEED = bytes([32]) # terminal speed |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
108 LFLOW = bytes([33]) # remote flow control |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
109 LINEMODE = bytes([34]) # Linemode option |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
110 XDISPLOC = bytes([35]) # X Display Location |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
111 OLD_ENVIRON = bytes([36]) # Old - Environment variables |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
112 AUTHENTICATION = bytes([37]) # Authenticate |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
113 ENCRYPT = bytes([38]) # Encryption option |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
114 NEW_ENVIRON = bytes([39]) # New - Environment variables |
|
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
|
115 # 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
|
116 # 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
|
117 # 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
|
118 # to all of them, so we are making them up |
|
53021
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
119 TN3270E = bytes([40]) # TN3270E |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
120 XAUTH = bytes([41]) # XAUTH |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
121 CHARSET = bytes([42]) # CHARSET |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
122 RSP = bytes([43]) # Telnet Remote Serial Port |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
123 COM_PORT_OPTION = bytes([44]) # Com Port Control Option |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
124 SUPPRESS_LOCAL_ECHO = bytes([45]) # Telnet Suppress Local Echo |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
125 TLS = bytes([46]) # Telnet Start TLS |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
126 KERMIT = bytes([47]) # KERMIT |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
127 SEND_URL = bytes([48]) # SEND-URL |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
128 FORWARD_X = bytes([49]) # FORWARD_X |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
129 PRAGMA_LOGON = bytes([138]) # TELOPT PRAGMA LOGON |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
130 SSPI_LOGON = bytes([139]) # TELOPT SSPI LOGON |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
131 PRAGMA_HEARTBEAT = bytes([140]) # TELOPT PRAGMA HEARTBEAT |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
132 EXOPL = bytes([255]) # Extended-Options-List |
|
cbaa1bc350ba
-fixes telnetlib constants to be one-length byte arrays instead of ints
Jack Diederich <jackdied@gmail.com>
parents:
49509
diff
changeset
|
133 NOOPT = bytes([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
|
134 |
|
86549
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
135 |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
136 # poll/select have the advantage of not requiring any extra file descriptor, |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
137 # contrarily to epoll/kqueue (also, they require a single syscall). |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
138 if hasattr(selectors, 'PollSelector'): |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
139 _TelnetSelector = selectors.PollSelector |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
140 else: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
141 _TelnetSelector = selectors.SelectSelector |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
142 |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
143 |
|
6940
1dafad307006
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 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
|
145 |
|
1dafad307006
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 """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
|
147 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 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
|
149 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
|
150 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
|
151 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
|
152 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
|
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 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
|
155 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 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
|
157 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
|
158 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
|
159 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
|
160 |
|
1dafad307006
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_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
|
162 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
|
163 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
|
164 |
|
1dafad307006
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_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
|
166 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
|
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_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
|
169 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
|
170 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 read_very_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
|
172 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
|
173 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
|
174 |
|
1dafad307006
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 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
|
176 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
|
177 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
|
178 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 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
|
180 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
|
181 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
|
182 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 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
|
184 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
|
185 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
|
186 |
|
26010
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
187 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
|
188 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
|
189 |
|
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
|
190 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
|
191 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
|
192 (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
|
193 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
|
194 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
|
195 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
|
196 |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 """ |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 |
|
47559
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47266
diff
changeset
|
199 def __init__(self, host=None, port=0, |
|
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47266
diff
changeset
|
200 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
|
201 """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
|
202 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
203 When called without arguments, create an unconnected instance. |
|
47559
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47266
diff
changeset
|
204 With a hostname argument, it connects the instance; port number |
|
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47266
diff
changeset
|
205 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
|
206 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
207 self.debuglevel = DEBUGLEVEL |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
208 self.host = host |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
209 self.port = port |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40785
diff
changeset
|
210 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
|
211 self.sock = None |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
212 self.rawq = b'' |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
213 self.irawq = 0 |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
214 self.cookedq = b'' |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
215 self.eof = 0 |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
216 self.iacseq = b'' # Buffer for IAC sequence. |
|
26010
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
217 self.sb = 0 # flag for SB and SE sequence. |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
218 self.sbdataq = b'' |
|
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
|
219 self.option_callback = None |
|
23628
ea979c5206b7
Replaced boolean tests with is None.
Raymond Hettinger <python@rcn.com>
parents:
23434
diff
changeset
|
220 if host is not None: |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40785
diff
changeset
|
221 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
|
222 |
|
47559
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47266
diff
changeset
|
223 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
|
224 """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
|
225 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
226 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
|
227 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
|
228 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
229 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
|
230 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
231 self.eof = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
232 if not port: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
233 port = TELNET_PORT |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
234 self.host = host |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
235 self.port = port |
|
47559
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47266
diff
changeset
|
236 self.timeout = timeout |
|
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47266
diff
changeset
|
237 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
|
238 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 def __del__(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
240 """Destructor -- close the connection.""" |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
241 self.close() |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 def msg(self, msg, *args): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
244 """Print a debug message, when the debug level is > 0. |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
246 If extra arguments are present, they are substituted in the |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
247 message using the standard string formatting operator. |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
248 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
249 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
250 if self.debuglevel > 0: |
|
66890
da20e7a01fa6
#10695: use %s not %d so that a string 'port' does not cause a debug traceback
R. David Murray <rdmurray@bitdance.com>
parents:
65645
diff
changeset
|
251 print('Telnet(%s,%s):' % (self.host, self.port), end=' ') |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
252 if args: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
253 print(msg % args) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
254 else: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
255 print(msg) |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 def set_debuglevel(self, debuglevel): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
258 """Set the debug level. |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
260 The higher it is, the more debug output you get (on sys.stdout). |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
262 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
263 self.debuglevel = debuglevel |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
264 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
265 def close(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
266 """Close the connection.""" |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
267 if self.sock: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
268 self.sock.close() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
269 self.sock = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
270 self.eof = 1 |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
271 self.iacseq = b'' |
|
26010
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
272 self.sb = 0 |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
273 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
274 def get_socket(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
275 """Return the socket object used internally.""" |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
276 return self.sock |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
278 def fileno(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
279 """Return the fileno() of the socket object used internally.""" |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
280 return self.sock.fileno() |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
281 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
282 def write(self, buffer): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
283 """Write a string to the socket, doubling any IAC characters. |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
284 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
285 Can block if the connection is blocked. May raise |
|
80933
50d19c2fac82
Issue #16717: get rid of socket.error, replace with OSError
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
80920
diff
changeset
|
286 OSError if the connection is closed. |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
287 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
288 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
289 if IAC in buffer: |
|
16488
92cbefdb674a
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15873
diff
changeset
|
290 buffer = buffer.replace(IAC, IAC+IAC) |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28089
diff
changeset
|
291 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
|
292 self.sock.sendall(buffer) |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 def read_until(self, match, timeout=None): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
295 """Read until a given string is encountered or until timeout. |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
296 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
297 When no match is found, return whatever is available instead, |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
298 possibly the empty string. Raise EOFError if the connection |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
299 is closed and no cooked data is available. |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
301 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
302 n = len(match) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
303 self.process_rawq() |
|
16488
92cbefdb674a
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15873
diff
changeset
|
304 i = self.cookedq.find(match) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
305 if i >= 0: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
306 i = i+n |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
307 buf = self.cookedq[:i] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
308 self.cookedq = self.cookedq[i:] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
309 return buf |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
310 if timeout is not None: |
|
86647
d0f90d3f6203
(Merge 3.3) Close #19339: telnetlib module is now using time.monotonic() when
Victor Stinner <victor.stinner@gmail.com>
diff
changeset
|
311 deadline = _time() + timeout |
|
86549
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
312 with _TelnetSelector() as selector: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
313 selector.register(self, selectors.EVENT_READ) |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
314 while not self.eof: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
315 if selector.select(timeout): |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
316 i = max(0, len(self.cookedq)-n) |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
317 self.fill_rawq() |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
318 self.process_rawq() |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
319 i = self.cookedq.find(match, i) |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
320 if i >= 0: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
321 i = i+n |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
322 buf = self.cookedq[:i] |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
323 self.cookedq = self.cookedq[i:] |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
324 return buf |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
325 if timeout is not None: |
|
86647
d0f90d3f6203
(Merge 3.3) Close #19339: telnetlib module is now using time.monotonic() when
Victor Stinner <victor.stinner@gmail.com>
diff
changeset
|
326 timeout = deadline - _time() |
|
86549
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
327 if timeout < 0: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
328 break |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
329 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
|
330 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
331 def read_all(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
332 """Read 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
|
333 self.process_rawq() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
334 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
|
335 self.fill_rawq() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
336 self.process_rawq() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
337 buf = self.cookedq |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
338 self.cookedq = b'' |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
339 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
|
340 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
341 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
|
342 """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
|
343 |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
344 Return b'' if EOF is hit. Block if no data is immediately |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
345 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
|
346 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
347 """ |
|
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() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
349 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
|
350 self.fill_rawq() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
351 self.process_rawq() |
|
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 |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
353 self.cookedq = b'' |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
354 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
|
355 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
356 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
|
357 """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
|
358 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
359 Raise EOFError if connection closed and no cooked data |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
360 available. Return b'' if no cooked data available otherwise. |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
361 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
|
362 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
363 """ |
|
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() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
365 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
|
366 self.fill_rawq() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
367 self.process_rawq() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
368 return self.read_very_lazy() |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
369 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
370 def read_eager(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
371 """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
|
372 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
373 Raise EOFError if connection closed and no cooked data |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
374 available. Return b'' if no cooked data available otherwise. |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
375 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
|
376 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
377 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
378 self.process_rawq() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
379 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
|
380 self.fill_rawq() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
381 self.process_rawq() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
382 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
|
383 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
384 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
|
385 """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
|
386 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
387 Raise EOFError if connection closed and no data available. |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
388 Return b'' if no cooked data available otherwise. Don't block |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
389 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
|
390 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
391 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
392 self.process_rawq() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
393 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
|
394 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
395 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
|
396 """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
|
397 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
398 Raise EOFError if connection closed and no data available. |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
399 Return b'' 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
|
400 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
401 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
402 buf = self.cookedq |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
403 self.cookedq = b'' |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
404 if not buf and self.eof and not self.rawq: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
41554
diff
changeset
|
405 raise EOFError('telnet connection closed') |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
406 return buf |
|
26126
98f3fd1ed42f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
26016
diff
changeset
|
407 |
|
26010
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
408 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
|
409 """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
|
410 |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
411 Return b'' if no SB ... SE available. Should only be called |
|
26126
98f3fd1ed42f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
26016
diff
changeset
|
412 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
|
413 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
|
414 |
|
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
415 """ |
|
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
416 buf = self.sbdataq |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
417 self.sbdataq = b'' |
|
26010
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
418 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
|
419 |
|
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
|
420 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
|
421 """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
|
422 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
|
423 |
|
6940
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
424 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
|
425 """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
|
426 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
427 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
|
428 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
|
429 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
430 """ |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
431 buf = [b'', b''] |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
432 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
433 while self.rawq: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
434 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
|
435 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
|
436 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
|
437 continue |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
438 if c == b"\021": |
|
26010
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
439 continue |
|
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
440 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
|
441 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
|
442 continue |
|
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
443 else: |
|
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
444 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
|
445 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
|
446 # '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
|
447 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
|
448 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
|
449 continue |
|
26126
98f3fd1ed42f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
26016
diff
changeset
|
450 |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
451 self.iacseq = b'' |
|
26010
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
452 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
|
453 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
|
454 else: |
|
26010
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
455 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
|
456 self.sb = 1 |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
457 self.sbdataq = b'' |
|
26010
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
458 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
|
459 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
|
460 self.sbdataq = self.sbdataq + buf[1] |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
461 buf[1] = b'' |
|
26010
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
462 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
|
463 # 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
|
464 # the sbdataq |
|
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
465 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
|
466 else: |
|
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
467 # 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
|
468 # 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
|
469 # 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
|
470 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
|
471 elif len(self.iacseq) == 2: |
|
55585
58c10cf04616
Merged revisions 74217,74224 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53021
diff
changeset
|
472 cmd = self.iacseq[1:2] |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
473 self.iacseq = b'' |
|
26010
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
474 opt = c |
|
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
475 if cmd in (DO, DONT): |
|
26126
98f3fd1ed42f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
26016
diff
changeset
|
476 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
|
477 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
|
478 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
|
479 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
|
480 else: |
|
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
481 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
|
482 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
|
483 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
|
484 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
|
485 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
|
486 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
|
487 else: |
|
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
488 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
|
489 except EOFError: # raised by self.rawq_getchar() |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
490 self.iacseq = b'' # Reset on EOF |
|
26010
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
491 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
|
492 pass |
|
26010
36ea31bc9076
Patch #630829: Don't block on IAC, process suboptions.
Martin v. Löwis <martin@v.loewis.de>
parents:
23628
diff
changeset
|
493 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
|
494 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
|
495 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
496 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
|
497 """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
|
498 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
499 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
|
500 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
|
501 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
502 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
503 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
|
504 self.fill_rawq() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
505 if self.eof: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
506 raise EOFError |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
507 c = self.rawq[self.irawq:self.irawq+1] |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
508 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
|
509 if self.irawq >= len(self.rawq): |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
510 self.rawq = b'' |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
511 self.irawq = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
512 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
|
513 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
514 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
|
515 """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
|
516 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
517 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
|
518 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
|
519 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
520 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
521 if self.irawq >= len(self.rawq): |
|
49509
48194d7af32f
Victor Stinner's patch to make telnetlib use bytes 3725
Benjamin Peterson <benjamin@python.org>
parents:
47559
diff
changeset
|
522 self.rawq = b'' |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
523 self.irawq = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
524 # 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
|
525 # 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
|
526 buf = self.sock.recv(50) |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28089
diff
changeset
|
527 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
|
528 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
|
529 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
|
530 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
531 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
|
532 """Test whether data is available on the socket.""" |
|
86549
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
533 with _TelnetSelector() as selector: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
534 selector.register(self, selectors.EVENT_READ) |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
535 return bool(selector.select(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
|
536 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
537 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
|
538 """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
|
539 if sys.platform == "win32": |
|
eaab8feb7fc0
Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
540 self.mt_interact() |
|
eaab8feb7fc0
Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
541 return |
|
86549
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
542 with _TelnetSelector() as selector: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
543 selector.register(self, selectors.EVENT_READ) |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
544 selector.register(sys.stdin, selectors.EVENT_READ) |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
545 |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
546 while True: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
547 for key, events in selector.select(): |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
548 if key.fileobj is self: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
549 try: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
550 text = self.read_eager() |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
551 except EOFError: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
552 print('*** Connection closed by remote host ***') |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
553 return |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
554 if text: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
555 sys.stdout.write(text.decode('ascii')) |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
556 sys.stdout.flush() |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
557 elif key.fileobj is sys.stdin: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
558 line = sys.stdin.readline().encode('ascii') |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
559 if not line: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
560 return |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
561 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
|
562 |
|
9727
eaab8feb7fc0
Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
563 def mt_interact(self): |
|
eaab8feb7fc0
Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
564 """Multithreaded version of interact().""" |
|
47266
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
43020
diff
changeset
|
565 import _thread |
|
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
43020
diff
changeset
|
566 _thread.start_new_thread(self.listener, ()) |
|
9727
eaab8feb7fc0
Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
567 while 1: |
|
eaab8feb7fc0
Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
568 line = sys.stdin.readline() |
|
eaab8feb7fc0
Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
569 if not line: |
|
eaab8feb7fc0
Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
570 break |
|
65645
e515d1241858
#7761: fix telnetlib.interact failures on Windows.
R. David Murray <rdmurray@bitdance.com>
parents:
55585
diff
changeset
|
571 self.write(line.encode('ascii')) |
|
9727
eaab8feb7fc0
Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
572 |
|
eaab8feb7fc0
Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
573 def listener(self): |
|
eaab8feb7fc0
Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
574 """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
|
575 while 1: |
|
eaab8feb7fc0
Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
576 try: |
|
eaab8feb7fc0
Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
577 data = self.read_eager() |
|
eaab8feb7fc0
Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
578 except EOFError: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
579 print('*** Connection closed by remote host ***') |
|
9727
eaab8feb7fc0
Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
580 return |
|
eaab8feb7fc0
Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
581 if data: |
|
65645
e515d1241858
#7761: fix telnetlib.interact failures on Windows.
R. David Murray <rdmurray@bitdance.com>
parents:
55585
diff
changeset
|
582 sys.stdout.write(data.decode('ascii')) |
|
9727
eaab8feb7fc0
Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
583 else: |
|
eaab8feb7fc0
Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
584 sys.stdout.flush() |
|
eaab8feb7fc0
Added mt_interact() -- multithreaded version of interact().
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
585 |
|
6942
06291f04ba9c
Added expect() method which takes a list of regular expressions and an
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
586 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
|
587 """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
|
588 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
589 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
|
590 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
|
591 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
|
592 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
|
593 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
594 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
|
595 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
|
596 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
|
597 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
598 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
|
599 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
|
600 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
|
601 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
|
602 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
603 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
|
604 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
|
605 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
|
606 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
607 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
608 re = None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
609 list = list[:] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
610 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
|
611 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
|
612 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
|
613 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
|
614 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
|
615 if timeout is not None: |
|
86647
d0f90d3f6203
(Merge 3.3) Close #19339: telnetlib module is now using time.monotonic() when
Victor Stinner <victor.stinner@gmail.com>
diff
changeset
|
616 deadline = _time() + timeout |
|
86549
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
617 with _TelnetSelector() as selector: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
618 selector.register(self, selectors.EVENT_READ) |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
619 while not self.eof: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
620 self.process_rawq() |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
621 for i in indices: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
622 m = list[i].search(self.cookedq) |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
623 if m: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
624 e = m.end() |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
625 text = self.cookedq[:e] |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
626 self.cookedq = self.cookedq[e:] |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
627 return (i, m, text) |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
628 if timeout is not None: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
629 ready = selector.select(timeout) |
|
86647
d0f90d3f6203
(Merge 3.3) Close #19339: telnetlib module is now using time.monotonic() when
Victor Stinner <victor.stinner@gmail.com>
diff
changeset
|
630 timeout = deadline - _time() |
|
86549
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
631 if not ready: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
632 if timeout < 0: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
633 break |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
634 else: |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
635 continue |
|
f713d9b6393c
Issue #19170: telnetlib: use selectors.
Charles-François Natali <cf.natali@gmail.com>
parents:
80933
diff
changeset
|
636 self.fill_rawq() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
637 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
|
638 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
|
639 raise EOFError |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
640 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
|
641 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
642 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
643 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
|
644 """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
|
645 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
646 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
|
647 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
648 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
|
649 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
650 """ |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
651 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
|
652 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
|
653 debuglevel = debuglevel+1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
654 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
|
655 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
|
656 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
|
657 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
|
658 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
|
659 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
|
660 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
|
661 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
662 port = int(portstr) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
663 except ValueError: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7414
diff
changeset
|
664 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
|
665 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
|
666 tn.set_debuglevel(debuglevel) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40785
diff
changeset
|
667 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
|
668 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
|
669 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
|
670 |
|
1dafad307006
Decided to add the telnet library that I wrote long ago (it's still in
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
671 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
|
672 test() |
