Mercurial > cpython-lru_cache-weakref
annotate Lib/poplib.py @ 79691:05c8928362ee
Merge __next__ method link fixes with 3.3.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 12 Oct 2012 13:46:37 +0300 |
| parents | d21900511675 |
| children | e07f104133d5 79e33578dc05 |
| 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. |
|
30817
1510244beb5e
Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents:
23622
diff
changeset
|
10 # Added the POP3_SSL class. Methods loosely based on IMAP_SSL. Hector Urtubia <urtubia@mrbook.org> Aug 2003 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 |
|
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 # 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
|
13 |
|
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 # Imports |
|
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 |
|
16485
5f12df88a208
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16044
diff
changeset
|
16 import re, socket |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
18 __all__ = ["POP3","error_proto"] |
|
16562
623456576d6a
__all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents:
16485
diff
changeset
|
19 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 # 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
|
21 |
|
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
22 class error_proto(Exception): pass |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 |
|
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 # Standard Port |
|
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 POP3_PORT = 110 |
|
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 |
|
30817
1510244beb5e
Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents:
23622
diff
changeset
|
27 # POP SSL PORT |
|
1510244beb5e
Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents:
23622
diff
changeset
|
28 POP3_SSL_PORT = 995 |
|
1510244beb5e
Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents:
23622
diff
changeset
|
29 |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
30 # Line terminators (we always output CRLF, but accept any of CRLF, LFCR, LF) |
|
43004
aa57cee11631
Make terminator constants bytes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42343
diff
changeset
|
31 CR = b'\r' |
|
aa57cee11631
Make terminator constants bytes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42343
diff
changeset
|
32 LF = b'\n' |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
33 CRLF = CR+LF |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 |
|
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 |
|
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 class POP3: |
|
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 """This class supports both the minimal and optional command sets. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
39 Arguments can be strings or integers (where appropriate) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
40 (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
|
41 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
42 Minimal Command Set: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
43 USER name user(name) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
44 PASS string pass_(string) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
45 STAT stat() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
46 LIST [msg] list(msg = None) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
47 RETR msg retr(msg) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
48 DELE msg dele(msg) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
49 NOOP noop() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
50 RSET rset() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
51 QUIT quit() |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
52 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
53 Optional Commands (some servers support these): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
54 RPOP name rpop(name) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
55 APOP name digest apop(name, digest) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
56 TOP msg n top(msg, n) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
57 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
|
58 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
59 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
|
60 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
61 Instantiate with: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
62 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
|
63 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
64 NB: the POP protocol locks the mailbox from user |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
65 authorization until QUIT, so be sure to get in, suck |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
66 the messages, and quit, each time you access the |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
67 mailbox. |
|
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 POP is a line-based protocol, which means large mail |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
70 messages consume lots of python cycles reading them |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
71 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
|
72 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
73 If it's available on your mail server, use IMAP4 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
74 instead, it doesn't suffer from the two problems |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
75 above. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
76 """ |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
77 |
|
49648
1bc3d1b39a4b
Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents:
47559
diff
changeset
|
78 encoding = 'UTF-8' |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
79 |
|
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:
47303
diff
changeset
|
80 def __init__(self, host, port=POP3_PORT, |
|
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:
47303
diff
changeset
|
81 timeout=socket._GLOBAL_DEFAULT_TIMEOUT): |
|
18834
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
82 self.host = host |
|
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
83 self.port = port |
|
49648
1bc3d1b39a4b
Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents:
47559
diff
changeset
|
84 self.sock = self._create_socket(timeout) |
|
18834
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
85 self.file = self.sock.makefile('rb') |
|
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
86 self._debugging = 0 |
|
7e52bf52ae33
Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
18825
diff
changeset
|
87 self.welcome = self._getresp() |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 |
|
49648
1bc3d1b39a4b
Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents:
47559
diff
changeset
|
89 def _create_socket(self, timeout): |
|
1bc3d1b39a4b
Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents:
47559
diff
changeset
|
90 return socket.create_connection((self.host, self.port), timeout) |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
91 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
92 def _putline(self, line): |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
93 if self._debugging > 1: print('*put*', repr(line)) |
|
49648
1bc3d1b39a4b
Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents:
47559
diff
changeset
|
94 self.sock.sendall(line + CRLF) |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
95 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
97 # 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
|
98 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
99 def _putcmd(self, line): |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
100 if self._debugging: print('*cmd*', repr(line)) |
|
49648
1bc3d1b39a4b
Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents:
47559
diff
changeset
|
101 line = bytes(line, self.encoding) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
102 self._putline(line) |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 |
|
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 # Internal: return one line from the server, stripping CRLF. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
106 # 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
|
107 # 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
|
108 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
109 def _getline(self): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
110 line = self.file.readline() |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
111 if self._debugging > 1: print('*get*', repr(line)) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
112 if not line: raise error_proto('-ERR EOF') |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
113 octets = len(line) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
114 # server can send any combination of CR & LF |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
115 # however, 'readline()' returns lines ending in LF |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
116 # so only possibilities are ...LF, ...CRLF, CR...LF |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
117 if line[-2:] == CRLF: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
118 return line[:-2], octets |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
119 if line[0] == CR: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
120 return line[1:-1], octets |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
121 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
|
122 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
124 # Internal: get a response from the server. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
125 # 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
|
126 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
127 def _getresp(self): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
128 resp, o = self._getline() |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
129 if self._debugging > 1: print('*resp*', repr(resp)) |
|
49648
1bc3d1b39a4b
Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents:
47559
diff
changeset
|
130 if not resp.startswith(b'+'): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
131 raise error_proto(resp) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
132 return resp |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
134 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
135 # 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
|
136 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
137 def _getlongresp(self): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
138 resp = self._getresp() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
139 list = []; octets = 0 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
140 line, o = self._getline() |
|
43304
f8a5fba35eb3
Patch # 1094 by Serge Julien. Fix some bytes/str comparisons.
Guido van Rossum <guido@python.org>
parents:
43095
diff
changeset
|
141 while line != b'.': |
|
49648
1bc3d1b39a4b
Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents:
47559
diff
changeset
|
142 if line.startswith(b'..'): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
143 o = o-1 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
144 line = line[1:] |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
145 octets = octets + o |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
146 list.append(line) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
147 line, o = self._getline() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
148 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
|
149 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
151 # 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
|
152 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
153 def _shortcmd(self, line): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
154 self._putcmd(line) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
155 return self._getresp() |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
157 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
158 # 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
|
159 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
160 def _longcmd(self, line): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
161 self._putcmd(line) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
162 return self._getlongresp() |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
164 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
165 # These can be useful: |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
167 def getwelcome(self): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
168 return self.welcome |
|
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 def set_debuglevel(self, level): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
172 self._debugging = level |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
174 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
175 # Here are all the POP commands: |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
177 def user(self, user): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
178 """Send user name, return response |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
179 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
180 (should indicate password required). |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
181 """ |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
182 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
|
183 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
185 def pass_(self, pswd): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
186 """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
|
187 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
188 (response includes message count, mailbox size). |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
189 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
190 NB: mailbox is locked by server from here to 'quit()' |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
191 """ |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
192 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
|
193 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
195 def stat(self): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
196 """Get mailbox status. |
|
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 Result is tuple of 2 ints (message count, mailbox size) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
199 """ |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
200 retval = self._shortcmd('STAT') |
|
16485
5f12df88a208
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16044
diff
changeset
|
201 rets = retval.split() |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
202 if self._debugging: print('*stat*', repr(rets)) |
|
16485
5f12df88a208
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16044
diff
changeset
|
203 numMessages = int(rets[1]) |
|
5f12df88a208
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16044
diff
changeset
|
204 sizeMessages = int(rets[2]) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
205 return (numMessages, sizeMessages) |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
207 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
208 def list(self, which=None): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
209 """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
|
210 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
211 Result without a message number argument is in form |
|
35314
1d8611d8555c
backport patch [ 1252706 ] poplib list() docstring fix (and docs too)
Georg Brandl <georg@python.org>
parents:
31515
diff
changeset
|
212 ['response', ['mesg_num octets', ...], octets]. |
|
9067
a5e3c695d2fb
Fix suggested by movits@lockstar.com (plus doc string by myself)
Guido van Rossum <guido@python.org>
parents:
8837
diff
changeset
|
213 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
214 Result when a message number argument is given is a |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
215 single response: the "scan listing" for that message. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
216 """ |
|
23622
5edd80726d96
Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents:
21963
diff
changeset
|
217 if which is not None: |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
218 return self._shortcmd('LIST %s' % which) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
219 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
|
220 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
222 def retr(self, which): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
223 """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
|
224 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
225 Result is in form ['response', ['line', ...], octets]. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
226 """ |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
227 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
|
228 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
230 def dele(self, which): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
231 """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
|
232 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
233 Result is 'response'. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
234 """ |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
235 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
|
236 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
238 def noop(self): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
239 """Does nothing. |
|
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 One supposes the response indicates the server is alive. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
242 """ |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
243 return self._shortcmd('NOOP') |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
245 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
246 def rset(self): |
|
47303
8cc3ad8bf7c4
Merged revisions 63402,63405,63411,63457,63467-63468,63480,63507-63508,63516,63534,63541 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
43400
diff
changeset
|
247 """Unmark all messages marked for deletion.""" |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
248 return self._shortcmd('RSET') |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 |
|
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
251 def quit(self): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
252 """Signoff: commit changes on server, unlock mailbox, close connection.""" |
|
68157
d21900511675
Issue 11291: poplib suppresses errors on QUIT.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64114
diff
changeset
|
253 resp = self._shortcmd('QUIT') |
|
d21900511675
Issue 11291: poplib suppresses errors on QUIT.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64114
diff
changeset
|
254 self.close() |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
255 return resp |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 |
|
68157
d21900511675
Issue 11291: poplib suppresses errors on QUIT.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64114
diff
changeset
|
257 def close(self): |
|
d21900511675
Issue 11291: poplib suppresses errors on QUIT.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64114
diff
changeset
|
258 """Close the connection without assuming anything about it.""" |
|
d21900511675
Issue 11291: poplib suppresses errors on QUIT.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64114
diff
changeset
|
259 if self.file is not None: |
|
d21900511675
Issue 11291: poplib suppresses errors on QUIT.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64114
diff
changeset
|
260 self.file.close() |
|
d21900511675
Issue 11291: poplib suppresses errors on QUIT.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64114
diff
changeset
|
261 if self.sock is not None: |
|
d21900511675
Issue 11291: poplib suppresses errors on QUIT.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64114
diff
changeset
|
262 self.sock.close() |
|
d21900511675
Issue 11291: poplib suppresses errors on QUIT.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64114
diff
changeset
|
263 self.file = self.sock = None |
|
d21900511675
Issue 11291: poplib suppresses errors on QUIT.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64114
diff
changeset
|
264 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
265 #__del__ = quit |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
266 |
|
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
267 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
268 # optional commands: |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
269 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
270 def rpop(self, user): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
271 """Not sure what this does.""" |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
272 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
|
273 |
|
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
274 |
|
49648
1bc3d1b39a4b
Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents:
47559
diff
changeset
|
275 timestamp = re.compile(br'\+OK.*(<[^>]+>)') |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
276 |
|
49648
1bc3d1b39a4b
Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents:
47559
diff
changeset
|
277 def apop(self, user, password): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
278 """Authorisation |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
279 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
280 - 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
|
281 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
282 Args: |
|
49648
1bc3d1b39a4b
Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents:
47559
diff
changeset
|
283 user - mailbox user; |
|
1bc3d1b39a4b
Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents:
47559
diff
changeset
|
284 password - mailbox password. |
|
7917
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
285 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
286 NB: mailbox is locked by server from here to 'quit()' |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
287 """ |
|
55559
9fdf182f19a9
Merged revisions 74330 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
49648
diff
changeset
|
288 secret = bytes(password, self.encoding) |
|
16044
35dff4712c8e
OK, checking in patch 103329.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
15870
diff
changeset
|
289 m = self.timestamp.match(self.welcome) |
|
35dff4712c8e
OK, checking in patch 103329.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
15870
diff
changeset
|
290 if not m: |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
291 raise error_proto('-ERR APOP not supported by server') |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
35314
diff
changeset
|
292 import hashlib |
|
49648
1bc3d1b39a4b
Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents:
47559
diff
changeset
|
293 digest = m.group(1)+secret |
|
1bc3d1b39a4b
Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents:
47559
diff
changeset
|
294 digest = hashlib.md5(digest).hexdigest() |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
295 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
|
296 |
|
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
297 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
298 def top(self, which, howmuch): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
299 """Retrieve message header of message number 'which' |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
300 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
|
301 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
302 Result is in form ['response', ['line', ...], octets]. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
303 """ |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
304 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
|
305 |
|
0905f8666807
Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents:
7892
diff
changeset
|
306 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
307 def uidl(self, which=None): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
308 """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
|
309 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
310 If 'which', result contains unique id for that message |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
311 in the form 'response mesgnum uid', otherwise result is |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
312 the list ['response', ['mesgnum uid', ...], octets] |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
313 """ |
|
23622
5edd80726d96
Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents:
21963
diff
changeset
|
314 if which is not None: |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
315 return self._shortcmd('UIDL %s' % which) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
316 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
|
317 |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
318 try: |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
319 import ssl |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
320 except ImportError: |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
321 pass |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
322 else: |
|
30817
1510244beb5e
Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents:
23622
diff
changeset
|
323 |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
324 class POP3_SSL(POP3): |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
325 """POP3 client class over SSL connection |
|
30817
1510244beb5e
Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents:
23622
diff
changeset
|
326 |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
327 Instantiate with: POP3_SSL(hostname, port=995, keyfile=None, certfile=None) |
|
30817
1510244beb5e
Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents:
23622
diff
changeset
|
328 |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
329 hostname - the hostname of the pop3 over ssl server |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
330 port - port number |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
331 keyfile - PEM formatted file that countains your private key |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
332 certfile - PEM formatted certificate chain file |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
333 |
|
49648
1bc3d1b39a4b
Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents:
47559
diff
changeset
|
334 See the methods of the parent class POP3 for more documentation. |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
335 """ |
|
30817
1510244beb5e
Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents:
23622
diff
changeset
|
336 |
|
64114
caeebf7497c3
fix issue #8807: adds a context parameter to POP3_SSL class.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55559
diff
changeset
|
337 def __init__(self, host, port=POP3_SSL_PORT, keyfile=None, certfile=None, |
|
caeebf7497c3
fix issue #8807: adds a context parameter to POP3_SSL class.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55559
diff
changeset
|
338 timeout=socket._GLOBAL_DEFAULT_TIMEOUT, context=None): |
|
caeebf7497c3
fix issue #8807: adds a context parameter to POP3_SSL class.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55559
diff
changeset
|
339 if context is not None and keyfile is not None: |
|
caeebf7497c3
fix issue #8807: adds a context parameter to POP3_SSL class.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55559
diff
changeset
|
340 raise ValueError("context and keyfile arguments are mutually " |
|
caeebf7497c3
fix issue #8807: adds a context parameter to POP3_SSL class.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55559
diff
changeset
|
341 "exclusive") |
|
caeebf7497c3
fix issue #8807: adds a context parameter to POP3_SSL class.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55559
diff
changeset
|
342 if context is not None and certfile is not None: |
|
caeebf7497c3
fix issue #8807: adds a context parameter to POP3_SSL class.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55559
diff
changeset
|
343 raise ValueError("context and certfile arguments are mutually " |
|
caeebf7497c3
fix issue #8807: adds a context parameter to POP3_SSL class.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55559
diff
changeset
|
344 "exclusive") |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
345 self.keyfile = keyfile |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
346 self.certfile = certfile |
|
64114
caeebf7497c3
fix issue #8807: adds a context parameter to POP3_SSL class.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55559
diff
changeset
|
347 self.context = context |
|
49648
1bc3d1b39a4b
Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents:
47559
diff
changeset
|
348 POP3.__init__(self, host, port, timeout) |
|
30817
1510244beb5e
Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents:
23622
diff
changeset
|
349 |
|
49648
1bc3d1b39a4b
Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents:
47559
diff
changeset
|
350 def _create_socket(self, timeout): |
|
1bc3d1b39a4b
Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents:
47559
diff
changeset
|
351 sock = POP3._create_socket(self, timeout) |
|
64114
caeebf7497c3
fix issue #8807: adds a context parameter to POP3_SSL class.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55559
diff
changeset
|
352 if self.context is not None: |
|
caeebf7497c3
fix issue #8807: adds a context parameter to POP3_SSL class.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55559
diff
changeset
|
353 sock = self.context.wrap_socket(sock) |
|
caeebf7497c3
fix issue #8807: adds a context parameter to POP3_SSL class.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55559
diff
changeset
|
354 else: |
|
caeebf7497c3
fix issue #8807: adds a context parameter to POP3_SSL class.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55559
diff
changeset
|
355 sock = ssl.wrap_socket(sock, self.keyfile, self.certfile) |
|
caeebf7497c3
fix issue #8807: adds a context parameter to POP3_SSL class.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55559
diff
changeset
|
356 return sock |
|
30817
1510244beb5e
Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents:
23622
diff
changeset
|
357 |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43020
diff
changeset
|
358 __all__.append("POP3_SSL") |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
359 |
|
7892
dbd2de26c8d2
Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
360 if __name__ == "__main__": |
|
16485
5f12df88a208
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16044
diff
changeset
|
361 import sys |
|
5f12df88a208
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16044
diff
changeset
|
362 a = POP3(sys.argv[1]) |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
363 print(a.getwelcome()) |
|
16485
5f12df88a208
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16044
diff
changeset
|
364 a.user(sys.argv[2]) |
|
5f12df88a208
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16044
diff
changeset
|
365 a.pass_(sys.argv[3]) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
366 a.list() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
367 (numMsgs, totalSize) = a.stat() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
368 for i in range(1, numMsgs + 1): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
369 (header, msg, octets) = a.retr(i) |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
370 print("Message %d:" % i) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
371 for line in msg: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
372 print(' ' + line) |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
373 print('-----------------------') |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13620
diff
changeset
|
374 a.quit() |
