annotate Lib/poplib.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 03cc1b55faae
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
80583
75a146a657d9 Fix missing import (followup to #4473).
Antoine Pitrou <solipsis@pitrou.net>
parents: 80575
diff changeset
16 import errno
75a146a657d9 Fix missing import (followup to #4473).
Antoine Pitrou <solipsis@pitrou.net>
parents: 80575
diff changeset
17 import re
75a146a657d9 Fix missing import (followup to #4473).
Antoine Pitrou <solipsis@pitrou.net>
parents: 80575
diff changeset
18 import socket
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
80575
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
20 try:
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
21 import ssl
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
22 HAVE_SSL = True
84442
7769c4d72806 Issue #18200: Back out usage of ModuleNotFoundError (8d28d44f3a9a)
Brett Cannon <brett@python.org>
parents: 84114
diff changeset
23 except ImportError:
80575
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
24 HAVE_SSL = False
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
43095
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43020
diff changeset
26 __all__ = ["POP3","error_proto"]
16562
623456576d6a __all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents: 16485
diff changeset
27
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # 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
29
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
30 class error_proto(Exception): pass
7892
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 # Standard Port
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 POP3_PORT = 110
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
30817
1510244beb5e Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents: 23622
diff changeset
35 # POP SSL PORT
1510244beb5e Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents: 23622
diff changeset
36 POP3_SSL_PORT = 995
1510244beb5e Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents: 23622
diff changeset
37
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
38 # 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
39 CR = b'\r'
aa57cee11631 Make terminator constants bytes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42343
diff changeset
40 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
41 CRLF = CR+LF
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
86677
68029048c9c6 Issue #16041: CVE-2013-1752: poplib: Limit maximum line lengths to 2048 to
Georg Brandl <georg@python.org>
parents: 85221
diff changeset
43 # maximal line length when calling readline(). This is to prevent
68029048c9c6 Issue #16041: CVE-2013-1752: poplib: Limit maximum line lengths to 2048 to
Georg Brandl <georg@python.org>
parents: 85221
diff changeset
44 # reading arbitrary lenght lines. RFC 1939 limits POP3 line length to
68029048c9c6 Issue #16041: CVE-2013-1752: poplib: Limit maximum line lengths to 2048 to
Georg Brandl <georg@python.org>
parents: 85221
diff changeset
45 # 512 characters, including CRLF. We have selected 2048 just to be on
68029048c9c6 Issue #16041: CVE-2013-1752: poplib: Limit maximum line lengths to 2048 to
Georg Brandl <georg@python.org>
parents: 85221
diff changeset
46 # the safe side.
68029048c9c6 Issue #16041: CVE-2013-1752: poplib: Limit maximum line lengths to 2048 to
Georg Brandl <georg@python.org>
parents: 85221
diff changeset
47 _MAXLINE = 2048
68029048c9c6 Issue #16041: CVE-2013-1752: poplib: Limit maximum line lengths to 2048 to
Georg Brandl <georg@python.org>
parents: 85221
diff changeset
48
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 class POP3:
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
51
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
52 """This class supports both the minimal and optional command sets.
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
53 Arguments can be strings or integers (where appropriate)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
54 (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
55
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
56 Minimal Command Set:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
57 USER name user(name)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
58 PASS string pass_(string)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
59 STAT stat()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
60 LIST [msg] list(msg = None)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
61 RETR msg retr(msg)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
62 DELE msg dele(msg)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
63 NOOP noop()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
64 RSET rset()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
65 QUIT quit()
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
66
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
67 Optional Commands (some servers support these):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
68 RPOP name rpop(name)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
69 APOP name digest apop(name, digest)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
70 TOP msg n top(msg, n)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
71 UIDL [msg] uidl(msg = None)
80574
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
72 CAPA capa()
80575
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
73 STLS stls()
7917
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 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
76
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
77 Instantiate with:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
78 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
79
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
80 NB: the POP protocol locks the mailbox from user
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
81 authorization until QUIT, so be sure to get in, suck
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
82 the messages, and quit, each time you access the
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
83 mailbox.
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
84
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
85 POP is a line-based protocol, which means large mail
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
86 messages consume lots of python cycles reading them
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
87 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
88
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
89 If it's available on your mail server, use IMAP4
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
90 instead, it doesn't suffer from the two problems
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
91 above.
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
92 """
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
93
49648
1bc3d1b39a4b Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents: 47559
diff changeset
94 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
95
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
96 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
97 timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
18834
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
98 self.host = host
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
99 self.port = port
80575
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
100 self._tls_established = False
49648
1bc3d1b39a4b Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents: 47559
diff changeset
101 self.sock = self._create_socket(timeout)
18834
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
102 self.file = self.sock.makefile('rb')
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
103 self._debugging = 0
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
104 self.welcome = self._getresp()
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105
49648
1bc3d1b39a4b Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents: 47559
diff changeset
106 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
107 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
108
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
109 def _putline(self, line):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
110 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
111 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
112
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
114 # 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
115
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
116 def _putcmd(self, line):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
117 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
118 line = bytes(line, self.encoding)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
119 self._putline(line)
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
121
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
122 # Internal: return one line from the server, stripping CRLF.
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
123 # 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
124 # 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
125
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
126 def _getline(self):
86677
68029048c9c6 Issue #16041: CVE-2013-1752: poplib: Limit maximum line lengths to 2048 to
Georg Brandl <georg@python.org>
parents: 85221
diff changeset
127 line = self.file.readline(_MAXLINE + 1)
68029048c9c6 Issue #16041: CVE-2013-1752: poplib: Limit maximum line lengths to 2048 to
Georg Brandl <georg@python.org>
parents: 85221
diff changeset
128 if len(line) > _MAXLINE:
68029048c9c6 Issue #16041: CVE-2013-1752: poplib: Limit maximum line lengths to 2048 to
Georg Brandl <georg@python.org>
parents: 85221
diff changeset
129 raise error_proto('line too long')
68029048c9c6 Issue #16041: CVE-2013-1752: poplib: Limit maximum line lengths to 2048 to
Georg Brandl <georg@python.org>
parents: 85221
diff changeset
130
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
131 if self._debugging > 1: print('*get*', repr(line))
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
132 if not line: raise error_proto('-ERR EOF')
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
133 octets = len(line)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
134 # server can send any combination of CR & LF
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
135 # however, 'readline()' returns lines ending in LF
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
136 # so only possibilities are ...LF, ...CRLF, CR...LF
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
137 if line[-2:] == CRLF:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
138 return line[:-2], octets
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
139 if line[0] == CR:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
140 return line[1:-1], octets
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
141 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
142
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
144 # Internal: get a response from the server.
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
145 # 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
146
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
147 def _getresp(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
148 resp, o = self._getline()
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
149 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
150 if not resp.startswith(b'+'):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
151 raise error_proto(resp)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
152 return resp
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
154
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
155 # 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
156
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
157 def _getlongresp(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
158 resp = self._getresp()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
159 list = []; octets = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
160 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
161 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
162 if line.startswith(b'..'):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
163 o = o-1
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
164 line = line[1:]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
165 octets = octets + o
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
166 list.append(line)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
167 line, o = self._getline()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
168 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
169
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
171 # 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
172
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
173 def _shortcmd(self, line):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
174 self._putcmd(line)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
175 return self._getresp()
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
177
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
178 # 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
179
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
180 def _longcmd(self, line):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
181 self._putcmd(line)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
182 return self._getlongresp()
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
184
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
185 # These can be useful:
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
187 def getwelcome(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
188 return self.welcome
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
189
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
190
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
191 def set_debuglevel(self, level):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
192 self._debugging = level
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
194
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
195 # Here are all the POP commands:
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
197 def user(self, user):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
198 """Send user name, return response
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 (should indicate password required).
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
201 """
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
202 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
203
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
205 def pass_(self, pswd):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
206 """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
207
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
208 (response includes message count, mailbox size).
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
209
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
210 NB: mailbox is locked by server from here to 'quit()'
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
211 """
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
212 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
213
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
215 def stat(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
216 """Get mailbox status.
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
217
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
218 Result is tuple of 2 ints (message count, mailbox size)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
219 """
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
220 retval = self._shortcmd('STAT')
16485
5f12df88a208 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16044
diff changeset
221 rets = retval.split()
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
222 if self._debugging: print('*stat*', repr(rets))
16485
5f12df88a208 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16044
diff changeset
223 numMessages = int(rets[1])
5f12df88a208 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16044
diff changeset
224 sizeMessages = int(rets[2])
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
225 return (numMessages, sizeMessages)
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
227
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
228 def list(self, which=None):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
229 """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
230
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
231 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
232 ['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
233
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
234 Result when a message number argument is given is a
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
235 single response: the "scan listing" for that message.
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
236 """
23622
5edd80726d96 Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents: 21963
diff changeset
237 if which is not None:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
238 return self._shortcmd('LIST %s' % which)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
239 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
240
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
241
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
242 def retr(self, which):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
243 """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
244
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
245 Result is in form ['response', ['line', ...], octets].
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 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
248
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
249
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
250 def dele(self, which):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
251 """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
252
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
253 Result is 'response'.
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
254 """
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
255 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
256
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
257
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
258 def noop(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
259 """Does nothing.
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
260
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
261 One supposes the response indicates the server is alive.
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
262 """
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
263 return self._shortcmd('NOOP')
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
264
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
265
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
266 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
267 """Unmark all messages marked for deletion."""
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
268 return self._shortcmd('RSET')
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
269
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
270
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
271 def quit(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
272 """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
273 resp = self._shortcmd('QUIT')
d21900511675 Issue 11291: poplib suppresses errors on QUIT.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64114
diff changeset
274 self.close()
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
275 return resp
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
276
68157
d21900511675 Issue 11291: poplib suppresses errors on QUIT.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64114
diff changeset
277 def close(self):
d21900511675 Issue 11291: poplib suppresses errors on QUIT.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64114
diff changeset
278 """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
279 if self.file is not None:
d21900511675 Issue 11291: poplib suppresses errors on QUIT.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64114
diff changeset
280 self.file.close()
d21900511675 Issue 11291: poplib suppresses errors on QUIT.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64114
diff changeset
281 if self.sock is not None:
80573
79e33578dc05 Issue #4473: Ensure the socket is shutdown cleanly in POP3.close().
Antoine Pitrou <solipsis@pitrou.net>
parents: 68157
diff changeset
282 try:
79e33578dc05 Issue #4473: Ensure the socket is shutdown cleanly in POP3.close().
Antoine Pitrou <solipsis@pitrou.net>
parents: 68157
diff changeset
283 self.sock.shutdown(socket.SHUT_RDWR)
80933
50d19c2fac82 Issue #16717: get rid of socket.error, replace with OSError
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80583
diff changeset
284 except OSError as e:
80573
79e33578dc05 Issue #4473: Ensure the socket is shutdown cleanly in POP3.close().
Antoine Pitrou <solipsis@pitrou.net>
parents: 68157
diff changeset
285 # The server might already have closed the connection
79e33578dc05 Issue #4473: Ensure the socket is shutdown cleanly in POP3.close().
Antoine Pitrou <solipsis@pitrou.net>
parents: 68157
diff changeset
286 if e.errno != errno.ENOTCONN:
79e33578dc05 Issue #4473: Ensure the socket is shutdown cleanly in POP3.close().
Antoine Pitrou <solipsis@pitrou.net>
parents: 68157
diff changeset
287 raise
79e33578dc05 Issue #4473: Ensure the socket is shutdown cleanly in POP3.close().
Antoine Pitrou <solipsis@pitrou.net>
parents: 68157
diff changeset
288 finally:
79e33578dc05 Issue #4473: Ensure the socket is shutdown cleanly in POP3.close().
Antoine Pitrou <solipsis@pitrou.net>
parents: 68157
diff changeset
289 self.sock.close()
68157
d21900511675 Issue 11291: poplib suppresses errors on QUIT.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64114
diff changeset
290 self.file = self.sock = None
d21900511675 Issue 11291: poplib suppresses errors on QUIT.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64114
diff changeset
291
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
292 #__del__ = quit
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
293
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
294
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
295 # optional commands:
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
296
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
297 def rpop(self, user):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
298 """Not sure what this does."""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
299 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
300
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
301
49648
1bc3d1b39a4b Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents: 47559
diff changeset
302 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
303
49648
1bc3d1b39a4b Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents: 47559
diff changeset
304 def apop(self, user, password):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
305 """Authorisation
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
306
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
307 - 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
308
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
309 Args:
49648
1bc3d1b39a4b Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents: 47559
diff changeset
310 user - mailbox user;
1bc3d1b39a4b Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents: 47559
diff changeset
311 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
312
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
313 NB: mailbox is locked by server from here to 'quit()'
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
314 """
55559
9fdf182f19a9 Merged revisions 74330 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49648
diff changeset
315 secret = bytes(password, self.encoding)
16044
35dff4712c8e OK, checking in patch 103329.
Moshe Zadka <moshez@math.huji.ac.il>
parents: 15870
diff changeset
316 m = self.timestamp.match(self.welcome)
35dff4712c8e OK, checking in patch 103329.
Moshe Zadka <moshez@math.huji.ac.il>
parents: 15870
diff changeset
317 if not m:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
318 raise error_proto('-ERR APOP not supported by server')
38052
32a187f3d5ba In stdlib, use hashlib instead of deprecated md5 and sha modules.
Georg Brandl <georg@python.org>
parents: 35314
diff changeset
319 import hashlib
49648
1bc3d1b39a4b Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents: 47559
diff changeset
320 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
321 digest = hashlib.md5(digest).hexdigest()
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
322 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
323
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
324
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
325 def top(self, which, howmuch):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
326 """Retrieve message header of message number 'which'
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
327 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
328
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
329 Result is in form ['response', ['line', ...], octets].
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
330 """
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
331 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
332
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
333
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
334 def uidl(self, which=None):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
335 """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
336
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
337 If 'which', result contains unique id for that message
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
338 in the form 'response mesgnum uid', otherwise result is
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
339 the list ['response', ['mesgnum uid', ...], octets]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
340 """
23622
5edd80726d96 Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents: 21963
diff changeset
341 if which is not None:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
342 return self._shortcmd('UIDL %s' % which)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
343 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
344
80574
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
345
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
346 def capa(self):
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
347 """Return server capabilities (RFC 2449) as a dictionary
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
348 >>> c=poplib.POP3('localhost')
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
349 >>> c.capa()
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
350 {'IMPLEMENTATION': ['Cyrus', 'POP3', 'server', 'v2.2.12'],
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
351 'TOP': [], 'LOGIN-DELAY': ['0'], 'AUTH-RESP-CODE': [],
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
352 'EXPIRE': ['NEVER'], 'USER': [], 'STLS': [], 'PIPELINING': [],
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
353 'UIDL': [], 'RESP-CODES': []}
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
354 >>>
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
355
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
356 Really, according to RFC 2449, the cyrus folks should avoid
85222
a9ee869cae40 #18705: merge with 3.3.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 84442 85221
diff changeset
357 having the implementation split into multiple arguments...
80574
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
358 """
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
359 def _parsecap(line):
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
360 lst = line.decode('ascii').split()
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
361 return lst[0], lst[1:]
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
362
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
363 caps = {}
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
364 try:
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
365 resp = self._longcmd('CAPA')
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
366 rawcaps = resp[1]
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
367 for capline in rawcaps:
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
368 capnm, capargs = _parsecap(capline)
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
369 caps[capnm] = capargs
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
370 except error_proto as _err:
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
371 raise error_proto('-ERR CAPA not supported by server')
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
372 return caps
d30fd9834cec Issue #4473: Add a POP3.capa() method to query the capabilities advertised by the POP3 server.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80573
diff changeset
373
80575
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
374
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
375 def stls(self, context=None):
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
376 """Start a TLS session on the active connection as specified in RFC 2595.
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
377
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
378 context - a ssl.SSLContext
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
379 """
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
380 if not HAVE_SSL:
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
381 raise error_proto('-ERR TLS support missing')
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
382 if self._tls_established:
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
383 raise error_proto('-ERR TLS session already established')
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
384 caps = self.capa()
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
385 if not 'STLS' in caps:
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
386 raise error_proto('-ERR STLS not supported by server')
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
387 if context is None:
87463
03cc1b55faae Issue #19735: Implement private function ssl._create_stdlib_context() to
Christian Heimes <christian@cheimes.de>
parents: 86681
diff changeset
388 context = ssl._create_stdlib_context()
80575
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
389 resp = self._shortcmd('STLS')
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
390 self.sock = context.wrap_socket(self.sock)
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
391 self.file = self.sock.makefile('rb')
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
392 self._tls_established = True
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
393 return resp
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
394
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
395
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
396 if HAVE_SSL:
30817
1510244beb5e Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents: 23622
diff changeset
397
43095
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43020
diff changeset
398 class POP3_SSL(POP3):
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43020
diff changeset
399 """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
400
80575
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
401 Instantiate with: POP3_SSL(hostname, port=995, keyfile=None, certfile=None,
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
402 context=None)
30817
1510244beb5e Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents: 23622
diff changeset
403
43095
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43020
diff changeset
404 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
405 port - port number
85221
e07f104133d5 #18705: fix a number of typos. Patch by Févry Thibault.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68157
diff changeset
406 keyfile - PEM formatted file that contains your private key
43095
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43020
diff changeset
407 certfile - PEM formatted certificate chain file
80575
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
408 context - a ssl.SSLContext
43095
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43020
diff changeset
409
49648
1bc3d1b39a4b Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents: 47559
diff changeset
410 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
411 """
30817
1510244beb5e Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents: 23622
diff changeset
412
64114
caeebf7497c3 fix issue #8807: adds a context parameter to POP3_SSL class.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 55559
diff changeset
413 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
414 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
415 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
416 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
417 "exclusive")
caeebf7497c3 fix issue #8807: adds a context parameter to POP3_SSL class.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 55559
diff changeset
418 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
419 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
420 "exclusive")
43095
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43020
diff changeset
421 self.keyfile = keyfile
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43020
diff changeset
422 self.certfile = certfile
87463
03cc1b55faae Issue #19735: Implement private function ssl._create_stdlib_context() to
Christian Heimes <christian@cheimes.de>
parents: 86681
diff changeset
423 if context is None:
03cc1b55faae Issue #19735: Implement private function ssl._create_stdlib_context() to
Christian Heimes <christian@cheimes.de>
parents: 86681
diff changeset
424 context = ssl._create_stdlib_context(certfile=certfile,
03cc1b55faae Issue #19735: Implement private function ssl._create_stdlib_context() to
Christian Heimes <christian@cheimes.de>
parents: 86681
diff changeset
425 keyfile=keyfile)
64114
caeebf7497c3 fix issue #8807: adds a context parameter to POP3_SSL class.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 55559
diff changeset
426 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
427 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
428
49648
1bc3d1b39a4b Fixed issue #3727: poplib module broken by str to unicode conversion
Christian Heimes <christian@cheimes.de>
parents: 47559
diff changeset
429 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
430 sock = POP3._create_socket(self, timeout)
87463
03cc1b55faae Issue #19735: Implement private function ssl._create_stdlib_context() to
Christian Heimes <christian@cheimes.de>
parents: 86681
diff changeset
431 sock = self.context.wrap_socket(sock)
64114
caeebf7497c3 fix issue #8807: adds a context parameter to POP3_SSL class.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 55559
diff changeset
432 return sock
30817
1510244beb5e Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents: 23622
diff changeset
433
80575
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
434 def stls(self, keyfile=None, certfile=None, context=None):
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
435 """The method unconditionally raises an exception since the
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
436 STLS command doesn't make any sense on an already established
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
437 SSL/TLS session.
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
438 """
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
439 raise error_proto('-ERR TLS session already established')
2329f9198d7f Issue #4473: Add a POP3.stls() to switch a clear-text POP3 session into an encrypted POP3 session, on supported servers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80574
diff changeset
440
43095
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43020
diff changeset
441 __all__.append("POP3_SSL")
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
442
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
443 if __name__ == "__main__":
16485
5f12df88a208 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16044
diff changeset
444 import sys
5f12df88a208 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16044
diff changeset
445 a = POP3(sys.argv[1])
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
446 print(a.getwelcome())
16485
5f12df88a208 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16044
diff changeset
447 a.user(sys.argv[2])
5f12df88a208 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16044
diff changeset
448 a.pass_(sys.argv[3])
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
449 a.list()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
450 (numMsgs, totalSize) = a.stat()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
451 for i in range(1, numMsgs + 1):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
452 (header, msg, octets) = a.retr(i)
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
453 print("Message %d:" % i)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
454 for line in msg:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
455 print(' ' + line)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
456 print('-----------------------')
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
457 a.quit()