Mercurial > cpython-lru_cache-weakref
annotate Lib/poplib.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 2faa23facef7 |
| children | 5edd80726d96 |
| rev | line source |
|---|---|
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
1 """A POP3 client class. |
|
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
2 |
|
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
3 Based on the J. Myers POP3 draft, Jan. 96 |
|
11870
d7eca5b0960a
Simple changes by Gerrit Holl - move author acknowledgements out of
Guido van Rossum <guido@python.org>
parents:
10852
diff
changeset
|
4 """ |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 |
|
11870
d7eca5b0960a
Simple changes by Gerrit Holl - move author acknowledgements out of
Guido van Rossum <guido@python.org>
parents:
10852
diff
changeset
|
6 # Author: David Ascher <david_ascher@brown.edu> |
|
d7eca5b0960a
Simple changes by Gerrit Holl - move author acknowledgements out of
Guido van Rossum <guido@python.org>
parents:
10852
diff
changeset
|
7 # [heavily stealing from nntplib.py] |
|
d7eca5b0960a
Simple changes by Gerrit Holl - move author acknowledgements out of
Guido van Rossum <guido@python.org>
parents:
10852
diff
changeset
|
8 # Updated: Piers Lauder <piers@cs.su.oz.au> [Jul '97] |
|
16485
5f12df88a208
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16044
diff
changeset
|
9 # String method conversion and test jig improvements by ESR, February 2001. |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 |
|
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 # Example (see the test function at the end of this file) |
|
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 |
|
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 # Imports |
|
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 |
|
16485
5f12df88a208
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16044
diff
changeset
|
15 import re, socket |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 |
|
16562
623456576d6a
__all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents:
16485
diff
changeset
|
17 __all__ = ["POP3","error_proto"] |
|
623456576d6a
__all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents:
16485
diff
changeset
|
18 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 # Exception raised when an error or invalid response is received: |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
20 |
|
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
21 class error_proto(Exception): pass |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 |
|
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 # Standard Port |
|
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 POP3_PORT = 110 |
|
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
26 # Line terminators (we always output CRLF, but accept any of CRLF, LFCR, LF) |
|
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
27 CR = '\r' |
|
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
28 LF = '\n' |
|
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
29 CRLF = CR+LF |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 |
|
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 |
|
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 class POP3: |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
33 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
34 """This class supports both the minimal and optional command sets. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
35 Arguments can be strings or integers (where appropriate) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
36 (e.g.: retr(1) and retr('1') both work equally well. |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
37 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
38 Minimal Command Set: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
39 USER name user(name) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
40 PASS string pass_(string) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
41 STAT stat() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
42 LIST [msg] list(msg = None) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
43 RETR msg retr(msg) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
44 DELE msg dele(msg) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
45 NOOP noop() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
46 RSET rset() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
47 QUIT quit() |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
48 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
49 Optional Commands (some servers support these): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
50 RPOP name rpop(name) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
51 APOP name digest apop(name, digest) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
52 TOP msg n top(msg, n) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
53 UIDL [msg] uidl(msg = None) |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
54 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
55 Raises one exception: 'error_proto'. |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
56 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
57 Instantiate with: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
58 POP3(hostname, port=110) |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
59 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
60 NB: the POP protocol locks the mailbox from user |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
61 authorization until QUIT, so be sure to get in, suck |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
62 the messages, and quit, each time you access the |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
63 mailbox. |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
64 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
65 POP is a line-based protocol, which means large mail |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
66 messages consume lots of python cycles reading them |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
67 line-by-line. |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
68 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
69 If it's available on your mail server, use IMAP4 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
70 instead, it doesn't suffer from the two problems |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
71 above. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
72 """ |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
73 |
|
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
74 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
75 def __init__(self, host, port = POP3_PORT): |
|
18834
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
76 self.host = host |
|
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
77 self.port = port |
|
18902
edf058b16ddf
Initialize msg to avoid unbound locals.
Martin v. Löwis <martin@v.loewis.de>
parents:
18834
diff
changeset
|
78 msg = "getaddrinfo returns an empty list" |
|
20303
faec69c11640
Only close sockets if they have been created. Reported by Blake Winton.
Martin v. Löwis <martin@v.loewis.de>
parents:
18902
diff
changeset
|
79 self.sock = None |
|
18834
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
80 for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM): |
|
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
81 af, socktype, proto, canonname, sa = res |
|
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
82 try: |
|
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
83 self.sock = socket.socket(af, socktype, proto) |
|
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
84 self.sock.connect(sa) |
|
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
85 except socket.error, msg: |
|
20303
faec69c11640
Only close sockets if they have been created. Reported by Blake Winton.
Martin v. Löwis <martin@v.loewis.de>
parents:
18902
diff
changeset
|
86 if self.sock: |
|
faec69c11640
Only close sockets if they have been created. Reported by Blake Winton.
Martin v. Löwis <martin@v.loewis.de>
parents:
18902
diff
changeset
|
87 self.sock.close() |
|
18834
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
88 self.sock = None |
|
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
89 continue |
|
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
90 break |
|
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
91 if not self.sock: |
|
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
92 raise socket.error, msg |
|
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
93 self.file = self.sock.makefile('rb') |
|
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
94 self._debugging = 0 |
|
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
95 self.welcome = self._getresp() |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
97 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
98 def _putline(self, line): |
|
21240
54ddc379d4fc
Re-enabled debugging prints in poplib & documented the set_debuglevel()
Fred Drake <fdrake@acm.org>
parents:
20303
diff
changeset
|
99 if self._debugging > 1: print '*put*', `line` |
|
21963
2faa23facef7
The Grande 'sendall()' patch, copied from release21-maint. Fixes #516715.
Martin v. Löwis <martin@v.loewis.de>
parents:
21240
diff
changeset
|
100 self.sock.sendall('%s%s' % (line, CRLF)) |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
101 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
103 # Internal: send one command to the server (through _putline()) |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
104 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
105 def _putcmd(self, line): |
|
21240
54ddc379d4fc
Re-enabled debugging prints in poplib & documented the set_debuglevel()
Fred Drake <fdrake@acm.org>
parents:
20303
diff
changeset
|
106 if self._debugging: print '*cmd*', `line` |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
107 self._putline(line) |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
109 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
110 # Internal: return one line from the server, stripping CRLF. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
111 # This is where all the CPU time of this module is consumed. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
112 # Raise error_proto('-ERR EOF') if the connection is closed. |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
113 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
114 def _getline(self): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
115 line = self.file.readline() |
|
21240
54ddc379d4fc
Re-enabled debugging prints in poplib & documented the set_debuglevel()
Fred Drake <fdrake@acm.org>
parents:
20303
diff
changeset
|
116 if self._debugging > 1: print '*get*', `line` |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
117 if not line: raise error_proto('-ERR EOF') |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
118 octets = len(line) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
119 # server can send any combination of CR & LF |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
120 # however, 'readline()' returns lines ending in LF |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
121 # so only possibilities are ...LF, ...CRLF, CR...LF |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
122 if line[-2:] == CRLF: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
123 return line[:-2], octets |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
124 if line[0] == CR: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
125 return line[1:-1], octets |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
126 return line[:-1], octets |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
127 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
129 # Internal: get a response from the server. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
130 # Raise 'error_proto' if the response doesn't start with '+'. |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
131 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
132 def _getresp(self): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
133 resp, o = self._getline() |
|
21240
54ddc379d4fc
Re-enabled debugging prints in poplib & documented the set_debuglevel()
Fred Drake <fdrake@acm.org>
parents:
20303
diff
changeset
|
134 if self._debugging > 1: print '*resp*', `resp` |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
135 c = resp[:1] |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
136 if c != '+': |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
137 raise error_proto(resp) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
138 return resp |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
140 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
141 # Internal: get a response plus following text from the server. |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
142 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
143 def _getlongresp(self): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
144 resp = self._getresp() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
145 list = []; octets = 0 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
146 line, o = self._getline() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
147 while line != '.': |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
148 if line[:2] == '..': |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
149 o = o-1 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
150 line = line[1:] |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
151 octets = octets + o |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
152 list.append(line) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
153 line, o = self._getline() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
154 return resp, list, octets |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
155 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
157 # Internal: send a command and get the response |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
158 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
159 def _shortcmd(self, line): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
160 self._putcmd(line) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
161 return self._getresp() |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
163 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
164 # Internal: send a command and get the response plus following text |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
165 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
166 def _longcmd(self, line): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
167 self._putcmd(line) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
168 return self._getlongresp() |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
170 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
171 # These can be useful: |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
173 def getwelcome(self): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
174 return self.welcome |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
176 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
177 def set_debuglevel(self, level): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
178 self._debugging = level |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
180 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
181 # Here are all the POP commands: |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
183 def user(self, user): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
184 """Send user name, return response |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
185 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
186 (should indicate password required). |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
187 """ |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
188 return self._shortcmd('USER %s' % user) |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
189 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
191 def pass_(self, pswd): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
192 """Send password, return response |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
193 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
194 (response includes message count, mailbox size). |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
195 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
196 NB: mailbox is locked by server from here to 'quit()' |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
197 """ |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
198 return self._shortcmd('PASS %s' % pswd) |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
199 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
201 def stat(self): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
202 """Get mailbox status. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
203 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
204 Result is tuple of 2 ints (message count, mailbox size) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
205 """ |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
206 retval = self._shortcmd('STAT') |
|
16485
5f12df88a208
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16044
diff
changeset
|
207 rets = retval.split() |
|
21240
54ddc379d4fc
Re-enabled debugging prints in poplib & documented the set_debuglevel()
Fred Drake <fdrake@acm.org>
parents:
20303
diff
changeset
|
208 if self._debugging: print '*stat*', `rets` |
|
16485
5f12df88a208
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16044
diff
changeset
|
209 numMessages = int(rets[1]) |
|
5f12df88a208
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16044
diff
changeset
|
210 sizeMessages = int(rets[2]) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
211 return (numMessages, sizeMessages) |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
213 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
214 def list(self, which=None): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
215 """Request listing, return result. |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
216 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
217 Result without a message number argument is in form |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
218 ['response', ['mesg_num octets', ...]]. |
|
9067
a5e3c695d2fb
Fix suggested by movits@lockstar.com (plus doc string by myself)
Guido van Rossum <guido@python.org>
parents:
8837
diff
changeset
|
219 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
220 Result when a message number argument is given is a |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
221 single response: the "scan listing" for that message. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
222 """ |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
223 if which: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
224 return self._shortcmd('LIST %s' % which) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
225 return self._longcmd('LIST') |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
226 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
228 def retr(self, which): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
229 """Retrieve whole message number 'which'. |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
230 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
231 Result is in form ['response', ['line', ...], octets]. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
232 """ |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
233 return self._longcmd('RETR %s' % which) |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
234 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
236 def dele(self, which): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
237 """Delete message number 'which'. |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
238 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
239 Result is 'response'. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
240 """ |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
241 return self._shortcmd('DELE %s' % which) |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
242 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
244 def noop(self): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
245 """Does nothing. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
246 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
247 One supposes the response indicates the server is alive. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
248 """ |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
249 return self._shortcmd('NOOP') |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
251 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
252 def rset(self): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
253 """Not sure what this does.""" |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
254 return self._shortcmd('RSET') |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 |
|
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
257 def quit(self): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
258 """Signoff: commit changes on server, unlock mailbox, close connection.""" |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
259 try: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
260 resp = self._shortcmd('QUIT') |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
261 except error_proto, val: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
262 resp = val |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
263 self.file.close() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
264 self.sock.close() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
265 del self.file, self.sock |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
266 return resp |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
268 #__del__ = quit |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
269 |
|
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
270 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
271 # optional commands: |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
272 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
273 def rpop(self, user): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
274 """Not sure what this does.""" |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
275 return self._shortcmd('RPOP %s' % user) |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
276 |
|
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
277 |
|
16044
35dff4712c8e
OK, checking in patch 103329.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
15870
diff
changeset
|
278 timestamp = re.compile(r'\+OK.*(<[^>]+>)') |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
279 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
280 def apop(self, user, secret): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
281 """Authorisation |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
282 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
283 - only possible if server has supplied a timestamp in initial greeting. |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
284 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
285 Args: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
286 user - mailbox user; |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
287 secret - secret shared between client and server. |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
288 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
289 NB: mailbox is locked by server from here to 'quit()' |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
290 """ |
|
16044
35dff4712c8e
OK, checking in patch 103329.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
15870
diff
changeset
|
291 m = self.timestamp.match(self.welcome) |
|
35dff4712c8e
OK, checking in patch 103329.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
15870
diff
changeset
|
292 if not m: |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
293 raise error_proto('-ERR APOP not supported by server') |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
294 import md5 |
|
16044
35dff4712c8e
OK, checking in patch 103329.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
15870
diff
changeset
|
295 digest = md5.new(m.group(1)+secret).digest() |
|
16485
5f12df88a208
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16044
diff
changeset
|
296 digest = ''.join(map(lambda x:'%02x'%ord(x), digest)) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
297 return self._shortcmd('APOP %s %s' % (user, digest)) |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
298 |
|
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
299 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
300 def top(self, which, howmuch): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
301 """Retrieve message header of message number 'which' |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
302 and first 'howmuch' lines of message body. |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
303 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
304 Result is in form ['response', ['line', ...], octets]. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
305 """ |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
306 return self._longcmd('TOP %s %s' % (which, howmuch)) |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
307 |
|
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
308 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
309 def uidl(self, which=None): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
310 """Return message digest (unique id) list. |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
311 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
312 If 'which', result contains unique id for that message |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
313 in the form 'response mesgnum uid', otherwise result is |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
314 the list ['response', ['mesgnum uid', ...], octets] |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
315 """ |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
316 if which: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
317 return self._shortcmd('UIDL %s' % which) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
318 return self._longcmd('UIDL') |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
319 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
320 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 if __name__ == "__main__": |
|
16485
5f12df88a208
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16044
diff
changeset
|
322 import sys |
|
5f12df88a208
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16044
diff
changeset
|
323 a = POP3(sys.argv[1]) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
324 print a.getwelcome() |
|
16485
5f12df88a208
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16044
diff
changeset
|
325 a.user(sys.argv[2]) |
|
5f12df88a208
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16044
diff
changeset
|
326 a.pass_(sys.argv[3]) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
327 a.list() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
328 (numMsgs, totalSize) = a.stat() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
329 for i in range(1, numMsgs + 1): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
330 (header, msg, octets) = a.retr(i) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
331 print "Message ", `i`, ':' |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
332 for line in msg: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
333 print ' ' + line |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
334 print '-----------------------' |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
335 a.quit() |
