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

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents e2c5ae30cffe
children 7214e3324a45 d234dd21374a
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
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
43093
1d57fb7efb15 Don't lie in __all__ attributes when SSL is not available: only add the SSL
Thomas Wouters <thomas@python.org>
parents: 43008
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)
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
31 CR = '\r'
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
32 LF = '\n'
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
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
78
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47116
diff changeset
79 def __init__(self, host, port=POP3_PORT,
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47116
diff changeset
80 timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
18834
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
81 self.host = host
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
82 self.port = port
41308
65326a3815f7 Added an optional timeout to poplib.POP3. Also created a
Facundo Batista <facundobatista@gmail.com>
parents: 38052
diff changeset
83 self.sock = socket.create_connection((host, port), timeout)
18834
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
84 self.file = self.sock.makefile('rb')
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
85 self._debugging = 0
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
86 self.welcome = self._getresp()
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
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 def _putline(self, line):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30817
diff changeset
90 if self._debugging > 1: print '*put*', repr(line)
21963
2faa23facef7 The Grande 'sendall()' patch, copied from release21-maint. Fixes #516715.
Martin v. Löwis <martin@v.loewis.de>
parents: 21240
diff changeset
91 self.sock.sendall('%s%s' % (line, CRLF))
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
92
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
94 # 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
95
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
96 def _putcmd(self, line):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30817
diff changeset
97 if self._debugging: print '*cmd*', repr(line)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
98 self._putline(line)
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
100
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
101 # Internal: return one line from the server, stripping CRLF.
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
102 # 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
103 # 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
104
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
105 def _getline(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
106 line = self.file.readline()
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30817
diff changeset
107 if self._debugging > 1: print '*get*', repr(line)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
108 if not line: raise error_proto('-ERR EOF')
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
109 octets = len(line)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
110 # server can send any combination of CR & LF
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
111 # however, 'readline()' returns lines ending in LF
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
112 # so only possibilities are ...LF, ...CRLF, CR...LF
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
113 if line[-2:] == CRLF:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
114 return line[:-2], octets
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
115 if line[0] == CR:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
116 return line[1:-1], octets
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
117 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
118
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
120 # Internal: get a response from the server.
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
121 # 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
122
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
123 def _getresp(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
124 resp, o = self._getline()
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30817
diff changeset
125 if self._debugging > 1: print '*resp*', repr(resp)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
126 c = resp[:1]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
127 if c != '+':
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
128 raise error_proto(resp)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
129 return resp
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
131
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
132 # 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
133
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
134 def _getlongresp(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
135 resp = self._getresp()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
136 list = []; octets = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
137 line, o = self._getline()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
138 while line != '.':
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
139 if line[:2] == '..':
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
140 o = o-1
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
141 line = line[1:]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
142 octets = octets + o
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
143 list.append(line)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
144 line, o = self._getline()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
145 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
146
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
148 # 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
149
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
150 def _shortcmd(self, line):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
151 self._putcmd(line)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
152 return self._getresp()
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: 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
156
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
157 def _longcmd(self, line):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
158 self._putcmd(line)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
159 return self._getlongresp()
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
161
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
162 # These can be useful:
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
163
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
164 def getwelcome(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
165 return self.welcome
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
167
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
168 def set_debuglevel(self, level):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
169 self._debugging = level
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
171
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
172 # Here are all the POP commands:
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
173
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
174 def user(self, user):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
175 """Send user name, return response
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
176
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
177 (should indicate password required).
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
178 """
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
179 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
180
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
182 def pass_(self, pswd):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
183 """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
184
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
185 (response includes message count, mailbox size).
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
186
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
187 NB: mailbox is locked by server from here to 'quit()'
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
188 """
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
189 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
190
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
192 def stat(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
193 """Get mailbox status.
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
194
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
195 Result is tuple of 2 ints (message count, mailbox size)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
196 """
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
197 retval = self._shortcmd('STAT')
16485
5f12df88a208 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16044
diff changeset
198 rets = retval.split()
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30817
diff changeset
199 if self._debugging: print '*stat*', repr(rets)
16485
5f12df88a208 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16044
diff changeset
200 numMessages = int(rets[1])
5f12df88a208 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16044
diff changeset
201 sizeMessages = int(rets[2])
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
202 return (numMessages, sizeMessages)
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
204
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
205 def list(self, which=None):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
206 """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
207
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
208 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
209 ['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
210
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
211 Result when a message number argument is given is a
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
212 single response: the "scan listing" for that message.
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
213 """
23622
5edd80726d96 Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents: 21963
diff changeset
214 if which is not None:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
215 return self._shortcmd('LIST %s' % which)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
216 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
217
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
219 def retr(self, which):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
220 """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
221
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
222 Result is in form ['response', ['line', ...], octets].
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
223 """
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
224 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
225
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
227 def dele(self, which):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
228 """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
229
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
230 Result is 'response'.
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
231 """
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
232 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
233
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
234
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
235 def noop(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
236 """Does nothing.
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
237
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
238 One supposes the response indicates the server is alive.
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
239 """
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
240 return self._shortcmd('NOOP')
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
241
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
242
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
243 def rset(self):
47116
2144c9f365c8 Fix-up docstring
Raymond Hettinger <python@rcn.com>
parents: 43325
diff changeset
244 """Unmark all messages marked for deletion."""
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
245 return self._shortcmd('RSET')
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
246
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
248 def quit(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
249 """Signoff: commit changes on server, unlock mailbox, close connection."""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
250 try:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
251 resp = self._shortcmd('QUIT')
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
252 except error_proto, val:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
253 resp = val
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
254 self.file.close()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
255 self.sock.close()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
256 del self.file, self.sock
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
257 return resp
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
258
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
259 #__del__ = quit
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
260
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
261
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
262 # optional commands:
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
263
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
264 def rpop(self, user):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
265 """Not sure what this does."""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
266 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
267
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
268
16044
35dff4712c8e OK, checking in patch 103329.
Moshe Zadka <moshez@math.huji.ac.il>
parents: 15870
diff changeset
269 timestamp = re.compile(r'\+OK.*(<[^>]+>)')
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
270
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
271 def apop(self, user, secret):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
272 """Authorisation
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
273
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
274 - 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
275
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
276 Args:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
277 user - mailbox user;
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
278 secret - secret shared between client and server.
7917
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
279
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
280 NB: mailbox is locked by server from here to 'quit()'
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
281 """
16044
35dff4712c8e OK, checking in patch 103329.
Moshe Zadka <moshez@math.huji.ac.il>
parents: 15870
diff changeset
282 m = self.timestamp.match(self.welcome)
35dff4712c8e OK, checking in patch 103329.
Moshe Zadka <moshez@math.huji.ac.il>
parents: 15870
diff changeset
283 if not m:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
284 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
285 import hashlib
32a187f3d5ba In stdlib, use hashlib instead of deprecated md5 and sha modules.
Georg Brandl <georg@python.org>
parents: 35314
diff changeset
286 digest = hashlib.md5(m.group(1)+secret).digest()
16485
5f12df88a208 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16044
diff changeset
287 digest = ''.join(map(lambda x:'%02x'%ord(x), digest))
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
288 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
289
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
290
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
291 def top(self, which, howmuch):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
292 """Retrieve message header of message number 'which'
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
293 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
294
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
295 Result is in form ['response', ['line', ...], octets].
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
296 """
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
297 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
298
0905f8666807 Version with docstrings and some other changes, by Piers Lauder.
Guido van Rossum <guido@python.org>
parents: 7892
diff changeset
299
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
300 def uidl(self, which=None):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
301 """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
302
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
303 If 'which', result contains unique id for that message
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
304 in the form 'response mesgnum uid', otherwise result is
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
305 the list ['response', ['mesgnum uid', ...], octets]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
306 """
23622
5edd80726d96 Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents: 21963
diff changeset
307 if which is not None:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
308 return self._shortcmd('UIDL %s' % which)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
309 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
310
43008
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
311 try:
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
312 import ssl
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
313 except ImportError:
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
314 pass
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
315 else:
30817
1510244beb5e Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents: 23622
diff changeset
316
43008
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
317 class POP3_SSL(POP3):
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
318 """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
319
43008
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
320 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
321
43008
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
322 hostname - the hostname of the pop3 over ssl server
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
323 port - port number
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
324 keyfile - PEM formatted file that countains your private key
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
325 certfile - PEM formatted certificate chain file
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
326
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
327 See the methods of the parent class POP3 for more documentation.
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
328 """
30817
1510244beb5e Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents: 23622
diff changeset
329
43008
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
330 def __init__(self, host, port = POP3_SSL_PORT, keyfile = None, certfile = None):
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
331 self.host = host
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
332 self.port = port
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
333 self.keyfile = keyfile
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
334 self.certfile = certfile
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
335 self.buffer = ""
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
336 msg = "getaddrinfo returns an empty list"
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
337 self.sock = None
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
338 for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
339 af, socktype, proto, canonname, sa = res
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
340 try:
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
341 self.sock = socket.socket(af, socktype, proto)
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
342 self.sock.connect(sa)
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
343 except socket.error, msg:
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
344 if self.sock:
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
345 self.sock.close()
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
346 self.sock = None
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
347 continue
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
348 break
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
349 if not self.sock:
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
350 raise socket.error, msg
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
351 self.file = self.sock.makefile('rb')
43325
193020bf52dc More work on SSL support.
Bill Janssen <janssen@parc.com>
parents: 43093
diff changeset
352 self.sslobj = ssl.wrap_socket(self.sock, self.keyfile, self.certfile)
43008
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
353 self._debugging = 0
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
354 self.welcome = self._getresp()
30817
1510244beb5e Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents: 23622
diff changeset
355
43008
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
356 def _fillBuffer(self):
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
357 localbuf = self.sslobj.read()
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
358 if len(localbuf) == 0:
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
359 raise error_proto('-ERR EOF')
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
360 self.buffer += localbuf
30817
1510244beb5e Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents: 23622
diff changeset
361
43008
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
362 def _getline(self):
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
363 line = ""
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
364 renewline = re.compile(r'.*?\n')
30817
1510244beb5e Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents: 23622
diff changeset
365 match = renewline.match(self.buffer)
43008
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
366 while not match:
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
367 self._fillBuffer()
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
368 match = renewline.match(self.buffer)
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
369 line = match.group(0)
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
370 self.buffer = renewline.sub('' ,self.buffer, 1)
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
371 if self._debugging > 1: print '*get*', repr(line)
30817
1510244beb5e Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents: 23622
diff changeset
372
43008
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
373 octets = len(line)
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
374 if line[-2:] == CRLF:
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
375 return line[:-2], octets
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
376 if line[0] == CR:
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
377 return line[1:-1], octets
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
378 return line[:-1], octets
30817
1510244beb5e Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents: 23622
diff changeset
379
43008
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
380 def _putline(self, line):
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
381 if self._debugging > 1: print '*put*', repr(line)
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
382 line += CRLF
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
383 bytes = len(line)
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
384 while bytes > 0:
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
385 sent = self.sslobj.write(line)
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
386 if sent == bytes:
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
387 break # avoid copy
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
388 line = line[sent:]
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
389 bytes = bytes - sent
30817
1510244beb5e Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents: 23622
diff changeset
390
43008
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
391 def quit(self):
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
392 """Signoff: commit changes on server, unlock mailbox, close connection."""
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
393 try:
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
394 resp = self._shortcmd('QUIT')
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
395 except error_proto, val:
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
396 resp = val
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
397 self.sock.close()
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
398 del self.sslobj, self.sock
4771639fc049 This contains a number of things:
Bill Janssen <janssen@parc.com>
parents: 41308
diff changeset
399 return resp
30817
1510244beb5e Patch #: Add POP3 over SSL support.
Martin v. Löwis <martin@v.loewis.de>
parents: 23622
diff changeset
400
43093
1d57fb7efb15 Don't lie in __all__ attributes when SSL is not available: only add the SSL
Thomas Wouters <thomas@python.org>
parents: 43008
diff changeset
401 __all__.append("POP3_SSL")
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
402
7892
dbd2de26c8d2 Dave Ascher's pop client code.
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 if __name__ == "__main__":
16485
5f12df88a208 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16044
diff changeset
404 import sys
5f12df88a208 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16044
diff changeset
405 a = POP3(sys.argv[1])
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
406 print a.getwelcome()
16485
5f12df88a208 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16044
diff changeset
407 a.user(sys.argv[2])
5f12df88a208 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16044
diff changeset
408 a.pass_(sys.argv[3])
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
409 a.list()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
410 (numMsgs, totalSize) = a.stat()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
411 for i in range(1, numMsgs + 1):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
412 (header, msg, octets) = a.retr(i)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30817
diff changeset
413 print "Message %d:" % i
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
414 for line in msg:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
415 print ' ' + line
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
416 print '-----------------------'
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
417 a.quit()