blob: 3283eb603ae07746254112e7726e0eeb632f8950 [file] [log] [blame]
Guido van Rossum03774bb1998-04-09 13:50:55 +00001"""A POP3 client class.
Guido van Rossum484772d1998-04-06 18:27:27 +00002
Guido van Rossum03774bb1998-04-09 13:50:55 +00003Based on the J. Myers POP3 draft, Jan. 96
Guido van Rossum484772d1998-04-06 18:27:27 +00004"""
5
Guido van Rossum98d9fd32000-02-28 15:12:25 +00006# Author: David Ascher <david_ascher@brown.edu>
7# [heavily stealing from nntplib.py]
8# Updated: Piers Lauder <piers@cs.su.oz.au> [Jul '97]
Eric S. Raymond341f9292001-02-09 06:56:56 +00009# String method conversion and test jig improvements by ESR, February 2001.
Martin v. Löwis48440b72003-10-31 12:52:35 +000010# Added the POP3_SSL class. Methods loosely based on IMAP_SSL. Hector Urtubia <urtubia@mrbook.org> Aug 2003
Guido van Rossum98d9fd32000-02-28 15:12:25 +000011
Guido van Rossum484772d1998-04-06 18:27:27 +000012# Example (see the test function at the end of this file)
13
Guido van Rossum484772d1998-04-06 18:27:27 +000014# Imports
15
Eric S. Raymond341f9292001-02-09 06:56:56 +000016import re, socket
Guido van Rossum484772d1998-04-06 18:27:27 +000017
Thomas Woutersa6900e82007-08-30 21:54:39 +000018__all__ = ["POP3","error_proto"]
Skip Montanaroc62c81e2001-02-12 02:00:42 +000019
Guido van Rossum484772d1998-04-06 18:27:27 +000020# Exception raised when an error or invalid response is received:
Guido van Rossum03774bb1998-04-09 13:50:55 +000021
22class error_proto(Exception): pass
Guido van Rossum484772d1998-04-06 18:27:27 +000023
24# Standard Port
25POP3_PORT = 110
26
Martin v. Löwis48440b72003-10-31 12:52:35 +000027# POP SSL PORT
28POP3_SSL_PORT = 995
29
Guido van Rossum03774bb1998-04-09 13:50:55 +000030# Line terminators (we always output CRLF, but accept any of CRLF, LFCR, LF)
31CR = '\r'
32LF = '\n'
33CRLF = CR+LF
Guido van Rossum484772d1998-04-06 18:27:27 +000034
Barry Warsawc545a5e2013-09-30 15:56:29 -040035# maximal line length when calling readline(). This is to prevent
36# reading arbitrary lenght lines. RFC 1939 limits POP3 line length to
37# 512 characters, including CRLF. We have selected 2048 just to be on
38# the safe side.
39_MAXLINE = 2048
40
Guido van Rossum484772d1998-04-06 18:27:27 +000041
42class POP3:
Guido van Rossum03774bb1998-04-09 13:50:55 +000043
Tim Peters2344fae2001-01-15 00:50:52 +000044 """This class supports both the minimal and optional command sets.
45 Arguments can be strings or integers (where appropriate)
46 (e.g.: retr(1) and retr('1') both work equally well.
Guido van Rossum03774bb1998-04-09 13:50:55 +000047
Tim Peters2344fae2001-01-15 00:50:52 +000048 Minimal Command Set:
49 USER name user(name)
50 PASS string pass_(string)
51 STAT stat()
52 LIST [msg] list(msg = None)
53 RETR msg retr(msg)
54 DELE msg dele(msg)
55 NOOP noop()
56 RSET rset()
57 QUIT quit()
Guido van Rossum03774bb1998-04-09 13:50:55 +000058
Tim Peters2344fae2001-01-15 00:50:52 +000059 Optional Commands (some servers support these):
60 RPOP name rpop(name)
61 APOP name digest apop(name, digest)
62 TOP msg n top(msg, n)
63 UIDL [msg] uidl(msg = None)
Guido van Rossum03774bb1998-04-09 13:50:55 +000064
Tim Peters2344fae2001-01-15 00:50:52 +000065 Raises one exception: 'error_proto'.
Guido van Rossum03774bb1998-04-09 13:50:55 +000066
Tim Peters2344fae2001-01-15 00:50:52 +000067 Instantiate with:
68 POP3(hostname, port=110)
Guido van Rossum03774bb1998-04-09 13:50:55 +000069
Tim Peters2344fae2001-01-15 00:50:52 +000070 NB: the POP protocol locks the mailbox from user
71 authorization until QUIT, so be sure to get in, suck
72 the messages, and quit, each time you access the
73 mailbox.
Guido van Rossum03774bb1998-04-09 13:50:55 +000074
Tim Peters2344fae2001-01-15 00:50:52 +000075 POP is a line-based protocol, which means large mail
76 messages consume lots of python cycles reading them
77 line-by-line.
Guido van Rossum03774bb1998-04-09 13:50:55 +000078
Tim Peters2344fae2001-01-15 00:50:52 +000079 If it's available on your mail server, use IMAP4
80 instead, it doesn't suffer from the two problems
81 above.
82 """
Guido van Rossum03774bb1998-04-09 13:50:55 +000083
84
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000085 def __init__(self, host, port=POP3_PORT,
86 timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
Martin v. Löwis4eb59402001-07-26 13:37:33 +000087 self.host = host
88 self.port = port
Facundo Batista1b1c3472007-03-27 18:23:21 +000089 self.sock = socket.create_connection((host, port), timeout)
Martin v. Löwis4eb59402001-07-26 13:37:33 +000090 self.file = self.sock.makefile('rb')
91 self._debugging = 0
92 self.welcome = self._getresp()
Guido van Rossum484772d1998-04-06 18:27:27 +000093
Guido van Rossum03774bb1998-04-09 13:50:55 +000094
Tim Peters2344fae2001-01-15 00:50:52 +000095 def _putline(self, line):
Walter Dörwald70a6b492004-02-12 17:35:32 +000096 if self._debugging > 1: print '*put*', repr(line)
Martin v. Löwise12454f2002-02-16 23:06:19 +000097 self.sock.sendall('%s%s' % (line, CRLF))
Guido van Rossum03774bb1998-04-09 13:50:55 +000098
Guido van Rossum484772d1998-04-06 18:27:27 +000099
Tim Peters2344fae2001-01-15 00:50:52 +0000100 # Internal: send one command to the server (through _putline())
Guido van Rossum03774bb1998-04-09 13:50:55 +0000101
Tim Peters2344fae2001-01-15 00:50:52 +0000102 def _putcmd(self, line):
Walter Dörwald70a6b492004-02-12 17:35:32 +0000103 if self._debugging: print '*cmd*', repr(line)
Tim Peters2344fae2001-01-15 00:50:52 +0000104 self._putline(line)
Guido van Rossum484772d1998-04-06 18:27:27 +0000105
Guido van Rossum03774bb1998-04-09 13:50:55 +0000106
Tim Peters2344fae2001-01-15 00:50:52 +0000107 # Internal: return one line from the server, stripping CRLF.
108 # This is where all the CPU time of this module is consumed.
109 # Raise error_proto('-ERR EOF') if the connection is closed.
Guido van Rossum03774bb1998-04-09 13:50:55 +0000110
Tim Peters2344fae2001-01-15 00:50:52 +0000111 def _getline(self):
Barry Warsawc545a5e2013-09-30 15:56:29 -0400112 line = self.file.readline(_MAXLINE + 1)
113 if len(line) > _MAXLINE:
114 raise error_proto('line too long')
115
Walter Dörwald70a6b492004-02-12 17:35:32 +0000116 if self._debugging > 1: print '*get*', repr(line)
Tim Peters2344fae2001-01-15 00:50:52 +0000117 if not line: raise error_proto('-ERR EOF')
118 octets = len(line)
119 # server can send any combination of CR & LF
120 # however, 'readline()' returns lines ending in LF
121 # so only possibilities are ...LF, ...CRLF, CR...LF
122 if line[-2:] == CRLF:
123 return line[:-2], octets
124 if line[0] == CR:
125 return line[1:-1], octets
126 return line[:-1], octets
Guido van Rossum03774bb1998-04-09 13:50:55 +0000127
Guido van Rossum484772d1998-04-06 18:27:27 +0000128
Tim Peters2344fae2001-01-15 00:50:52 +0000129 # Internal: get a response from the server.
130 # Raise 'error_proto' if the response doesn't start with '+'.
Guido van Rossum03774bb1998-04-09 13:50:55 +0000131
Tim Peters2344fae2001-01-15 00:50:52 +0000132 def _getresp(self):
133 resp, o = self._getline()
Walter Dörwald70a6b492004-02-12 17:35:32 +0000134 if self._debugging > 1: print '*resp*', repr(resp)
Tim Peters2344fae2001-01-15 00:50:52 +0000135 c = resp[:1]
136 if c != '+':
137 raise error_proto(resp)
138 return resp
Guido van Rossum484772d1998-04-06 18:27:27 +0000139
Guido van Rossum03774bb1998-04-09 13:50:55 +0000140
Tim Peters2344fae2001-01-15 00:50:52 +0000141 # Internal: get a response plus following text from the server.
Guido van Rossum03774bb1998-04-09 13:50:55 +0000142
Tim Peters2344fae2001-01-15 00:50:52 +0000143 def _getlongresp(self):
144 resp = self._getresp()
145 list = []; octets = 0
146 line, o = self._getline()
147 while line != '.':
148 if line[:2] == '..':
149 o = o-1
150 line = line[1:]
151 octets = octets + o
152 list.append(line)
153 line, o = self._getline()
154 return resp, list, octets
Guido van Rossum03774bb1998-04-09 13:50:55 +0000155
Guido van Rossum484772d1998-04-06 18:27:27 +0000156
Tim Peters2344fae2001-01-15 00:50:52 +0000157 # Internal: send a command and get the response
Guido van Rossum03774bb1998-04-09 13:50:55 +0000158
Tim Peters2344fae2001-01-15 00:50:52 +0000159 def _shortcmd(self, line):
160 self._putcmd(line)
161 return self._getresp()
Guido van Rossum484772d1998-04-06 18:27:27 +0000162
Guido van Rossum03774bb1998-04-09 13:50:55 +0000163
Tim Peters2344fae2001-01-15 00:50:52 +0000164 # Internal: send a command and get the response plus following text
Guido van Rossum03774bb1998-04-09 13:50:55 +0000165
Tim Peters2344fae2001-01-15 00:50:52 +0000166 def _longcmd(self, line):
167 self._putcmd(line)
168 return self._getlongresp()
Guido van Rossum484772d1998-04-06 18:27:27 +0000169
Guido van Rossum03774bb1998-04-09 13:50:55 +0000170
Tim Peters2344fae2001-01-15 00:50:52 +0000171 # These can be useful:
172
173 def getwelcome(self):
174 return self.welcome
175
176
177 def set_debuglevel(self, level):
178 self._debugging = level
179
180
181 # Here are all the POP commands:
182
183 def user(self, user):
184 """Send user name, return response
Guido van Rossum484772d1998-04-06 18:27:27 +0000185
Tim Peters2344fae2001-01-15 00:50:52 +0000186 (should indicate password required).
187 """
188 return self._shortcmd('USER %s' % user)
Guido van Rossum484772d1998-04-06 18:27:27 +0000189
Guido van Rossum03774bb1998-04-09 13:50:55 +0000190
Tim Peters2344fae2001-01-15 00:50:52 +0000191 def pass_(self, pswd):
192 """Send password, return response
Guido van Rossum484772d1998-04-06 18:27:27 +0000193
Tim Peters2344fae2001-01-15 00:50:52 +0000194 (response includes message count, mailbox size).
Guido van Rossum03774bb1998-04-09 13:50:55 +0000195
Tim Peters2344fae2001-01-15 00:50:52 +0000196 NB: mailbox is locked by server from here to 'quit()'
197 """
198 return self._shortcmd('PASS %s' % pswd)
Guido van Rossum484772d1998-04-06 18:27:27 +0000199
Guido van Rossum03774bb1998-04-09 13:50:55 +0000200
Tim Peters2344fae2001-01-15 00:50:52 +0000201 def stat(self):
202 """Get mailbox status.
Guido van Rossum484772d1998-04-06 18:27:27 +0000203
Tim Peters2344fae2001-01-15 00:50:52 +0000204 Result is tuple of 2 ints (message count, mailbox size)
205 """
206 retval = self._shortcmd('STAT')
Eric S. Raymond341f9292001-02-09 06:56:56 +0000207 rets = retval.split()
Walter Dörwald70a6b492004-02-12 17:35:32 +0000208 if self._debugging: print '*stat*', repr(rets)
Eric S. Raymond341f9292001-02-09 06:56:56 +0000209 numMessages = int(rets[1])
210 sizeMessages = int(rets[2])
Tim Peters2344fae2001-01-15 00:50:52 +0000211 return (numMessages, sizeMessages)
Guido van Rossum03774bb1998-04-09 13:50:55 +0000212
Guido van Rossum03774bb1998-04-09 13:50:55 +0000213
Tim Peters2344fae2001-01-15 00:50:52 +0000214 def list(self, which=None):
215 """Request listing, return result.
Guido van Rossum484772d1998-04-06 18:27:27 +0000216
Tim Peters2344fae2001-01-15 00:50:52 +0000217 Result without a message number argument is in form
Georg Brandl2772c672005-08-05 21:01:58 +0000218 ['response', ['mesg_num octets', ...], octets].
Guido van Rossum484772d1998-04-06 18:27:27 +0000219
Tim Peters2344fae2001-01-15 00:50:52 +0000220 Result when a message number argument is given is a
221 single response: the "scan listing" for that message.
222 """
Raymond Hettinger16e3c422002-06-01 16:07:16 +0000223 if which is not None:
Tim Peters2344fae2001-01-15 00:50:52 +0000224 return self._shortcmd('LIST %s' % which)
225 return self._longcmd('LIST')
Guido van Rossum03774bb1998-04-09 13:50:55 +0000226
Guido van Rossum03774bb1998-04-09 13:50:55 +0000227
Tim Peters2344fae2001-01-15 00:50:52 +0000228 def retr(self, which):
229 """Retrieve whole message number 'which'.
Guido van Rossumf6ae7431998-09-02 14:42:02 +0000230
Tim Peters2344fae2001-01-15 00:50:52 +0000231 Result is in form ['response', ['line', ...], octets].
232 """
233 return self._longcmd('RETR %s' % which)
Guido van Rossum03774bb1998-04-09 13:50:55 +0000234
Guido van Rossum484772d1998-04-06 18:27:27 +0000235
Tim Peters2344fae2001-01-15 00:50:52 +0000236 def dele(self, which):
237 """Delete message number 'which'.
Guido van Rossum03774bb1998-04-09 13:50:55 +0000238
Tim Peters2344fae2001-01-15 00:50:52 +0000239 Result is 'response'.
240 """
241 return self._shortcmd('DELE %s' % which)
Guido van Rossum03774bb1998-04-09 13:50:55 +0000242
Guido van Rossum484772d1998-04-06 18:27:27 +0000243
Tim Peters2344fae2001-01-15 00:50:52 +0000244 def noop(self):
245 """Does nothing.
Guido van Rossum03774bb1998-04-09 13:50:55 +0000246
Tim Peters2344fae2001-01-15 00:50:52 +0000247 One supposes the response indicates the server is alive.
248 """
249 return self._shortcmd('NOOP')
Guido van Rossum03774bb1998-04-09 13:50:55 +0000250
Guido van Rossum484772d1998-04-06 18:27:27 +0000251
Tim Peters2344fae2001-01-15 00:50:52 +0000252 def rset(self):
Raymond Hettingerce956842008-05-17 04:13:36 +0000253 """Unmark all messages marked for deletion."""
Tim Peters2344fae2001-01-15 00:50:52 +0000254 return self._shortcmd('RSET')
Guido van Rossum484772d1998-04-06 18:27:27 +0000255
Guido van Rossum03774bb1998-04-09 13:50:55 +0000256
Tim Peters2344fae2001-01-15 00:50:52 +0000257 def quit(self):
258 """Signoff: commit changes on server, unlock mailbox, close connection."""
259 try:
260 resp = self._shortcmd('QUIT')
261 except error_proto, val:
262 resp = val
263 self.file.close()
264 self.sock.close()
265 del self.file, self.sock
266 return resp
Guido van Rossum484772d1998-04-06 18:27:27 +0000267
Tim Peters2344fae2001-01-15 00:50:52 +0000268 #__del__ = quit
Guido van Rossum484772d1998-04-06 18:27:27 +0000269
Guido van Rossum484772d1998-04-06 18:27:27 +0000270
Tim Peters2344fae2001-01-15 00:50:52 +0000271 # optional commands:
Guido van Rossum03774bb1998-04-09 13:50:55 +0000272
Tim Peters2344fae2001-01-15 00:50:52 +0000273 def rpop(self, user):
274 """Not sure what this does."""
275 return self._shortcmd('RPOP %s' % user)
Guido van Rossum03774bb1998-04-09 13:50:55 +0000276
Guido van Rossum03774bb1998-04-09 13:50:55 +0000277
Moshe Zadkaccc2e3d2001-01-19 19:56:27 +0000278 timestamp = re.compile(r'\+OK.*(<[^>]+>)')
Guido van Rossum03774bb1998-04-09 13:50:55 +0000279
Tim Peters2344fae2001-01-15 00:50:52 +0000280 def apop(self, user, secret):
281 """Authorisation
Guido van Rossum03774bb1998-04-09 13:50:55 +0000282
Tim Peters2344fae2001-01-15 00:50:52 +0000283 - only possible if server has supplied a timestamp in initial greeting.
Guido van Rossum03774bb1998-04-09 13:50:55 +0000284
Tim Peters2344fae2001-01-15 00:50:52 +0000285 Args:
286 user - mailbox user;
287 secret - secret shared between client and server.
Guido van Rossum03774bb1998-04-09 13:50:55 +0000288
Tim Peters2344fae2001-01-15 00:50:52 +0000289 NB: mailbox is locked by server from here to 'quit()'
290 """
Moshe Zadkaccc2e3d2001-01-19 19:56:27 +0000291 m = self.timestamp.match(self.welcome)
292 if not m:
Tim Peters2344fae2001-01-15 00:50:52 +0000293 raise error_proto('-ERR APOP not supported by server')
Georg Brandlbffb0bc2006-04-30 08:57:35 +0000294 import hashlib
295 digest = hashlib.md5(m.group(1)+secret).digest()
Eric S. Raymond341f9292001-02-09 06:56:56 +0000296 digest = ''.join(map(lambda x:'%02x'%ord(x), digest))
Tim Peters2344fae2001-01-15 00:50:52 +0000297 return self._shortcmd('APOP %s %s' % (user, digest))
Guido van Rossum03774bb1998-04-09 13:50:55 +0000298
Guido van Rossum03774bb1998-04-09 13:50:55 +0000299
Tim Peters2344fae2001-01-15 00:50:52 +0000300 def top(self, which, howmuch):
301 """Retrieve message header of message number 'which'
302 and first 'howmuch' lines of message body.
Guido van Rossum03774bb1998-04-09 13:50:55 +0000303
Tim Peters2344fae2001-01-15 00:50:52 +0000304 Result is in form ['response', ['line', ...], octets].
305 """
306 return self._longcmd('TOP %s %s' % (which, howmuch))
Guido van Rossum03774bb1998-04-09 13:50:55 +0000307
Guido van Rossum03774bb1998-04-09 13:50:55 +0000308
Tim Peters2344fae2001-01-15 00:50:52 +0000309 def uidl(self, which=None):
310 """Return message digest (unique id) list.
Guido van Rossum03774bb1998-04-09 13:50:55 +0000311
Tim Peters2344fae2001-01-15 00:50:52 +0000312 If 'which', result contains unique id for that message
313 in the form 'response mesgnum uid', otherwise result is
314 the list ['response', ['mesgnum uid', ...], octets]
315 """
Raymond Hettinger16e3c422002-06-01 16:07:16 +0000316 if which is not None:
Tim Peters2344fae2001-01-15 00:50:52 +0000317 return self._shortcmd('UIDL %s' % which)
318 return self._longcmd('UIDL')
Guido van Rossum03774bb1998-04-09 13:50:55 +0000319
Bill Janssen426ea0a2007-08-29 22:35:05 +0000320try:
321 import ssl
322except ImportError:
323 pass
324else:
Martin v. Löwis48440b72003-10-31 12:52:35 +0000325
Bill Janssen426ea0a2007-08-29 22:35:05 +0000326 class POP3_SSL(POP3):
327 """POP3 client class over SSL connection
Martin v. Löwis48440b72003-10-31 12:52:35 +0000328
Bill Janssen426ea0a2007-08-29 22:35:05 +0000329 Instantiate with: POP3_SSL(hostname, port=995, keyfile=None, certfile=None)
Martin v. Löwis48440b72003-10-31 12:52:35 +0000330
Bill Janssen426ea0a2007-08-29 22:35:05 +0000331 hostname - the hostname of the pop3 over ssl server
332 port - port number
333 keyfile - PEM formatted file that countains your private key
334 certfile - PEM formatted certificate chain file
Martin v. Löwis48440b72003-10-31 12:52:35 +0000335
Bill Janssen426ea0a2007-08-29 22:35:05 +0000336 See the methods of the parent class POP3 for more documentation.
337 """
Martin v. Löwis48440b72003-10-31 12:52:35 +0000338
Bill Janssen426ea0a2007-08-29 22:35:05 +0000339 def __init__(self, host, port = POP3_SSL_PORT, keyfile = None, certfile = None):
340 self.host = host
341 self.port = port
342 self.keyfile = keyfile
343 self.certfile = certfile
344 self.buffer = ""
345 msg = "getaddrinfo returns an empty list"
346 self.sock = None
347 for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
348 af, socktype, proto, canonname, sa = res
349 try:
350 self.sock = socket.socket(af, socktype, proto)
351 self.sock.connect(sa)
352 except socket.error, msg:
353 if self.sock:
354 self.sock.close()
355 self.sock = None
356 continue
357 break
358 if not self.sock:
359 raise socket.error, msg
360 self.file = self.sock.makefile('rb')
Bill Janssen98d19da2007-09-10 21:51:02 +0000361 self.sslobj = ssl.wrap_socket(self.sock, self.keyfile, self.certfile)
Bill Janssen426ea0a2007-08-29 22:35:05 +0000362 self._debugging = 0
363 self.welcome = self._getresp()
Martin v. Löwis48440b72003-10-31 12:52:35 +0000364
Bill Janssen426ea0a2007-08-29 22:35:05 +0000365 def _fillBuffer(self):
366 localbuf = self.sslobj.read()
367 if len(localbuf) == 0:
368 raise error_proto('-ERR EOF')
369 self.buffer += localbuf
370
371 def _getline(self):
372 line = ""
373 renewline = re.compile(r'.*?\n')
Martin v. Löwis48440b72003-10-31 12:52:35 +0000374 match = renewline.match(self.buffer)
Barry Warsawc545a5e2013-09-30 15:56:29 -0400375
Bill Janssen426ea0a2007-08-29 22:35:05 +0000376 while not match:
Barry Warsawc545a5e2013-09-30 15:56:29 -0400377 if len(self.buffer) > _MAXLINE:
378 raise error_proto('line too long')
Bill Janssen426ea0a2007-08-29 22:35:05 +0000379 self._fillBuffer()
380 match = renewline.match(self.buffer)
381 line = match.group(0)
382 self.buffer = renewline.sub('' ,self.buffer, 1)
383 if self._debugging > 1: print '*get*', repr(line)
Martin v. Löwis48440b72003-10-31 12:52:35 +0000384
Bill Janssen426ea0a2007-08-29 22:35:05 +0000385 octets = len(line)
386 if line[-2:] == CRLF:
387 return line[:-2], octets
388 if line[0] == CR:
389 return line[1:-1], octets
390 return line[:-1], octets
Martin v. Löwis48440b72003-10-31 12:52:35 +0000391
Bill Janssen426ea0a2007-08-29 22:35:05 +0000392 def _putline(self, line):
393 if self._debugging > 1: print '*put*', repr(line)
394 line += CRLF
395 bytes = len(line)
396 while bytes > 0:
397 sent = self.sslobj.write(line)
398 if sent == bytes:
399 break # avoid copy
400 line = line[sent:]
401 bytes = bytes - sent
Martin v. Löwis48440b72003-10-31 12:52:35 +0000402
Bill Janssen426ea0a2007-08-29 22:35:05 +0000403 def quit(self):
404 """Signoff: commit changes on server, unlock mailbox, close connection."""
405 try:
406 resp = self._shortcmd('QUIT')
407 except error_proto, val:
408 resp = val
409 self.sock.close()
410 del self.sslobj, self.sock
411 return resp
Martin v. Löwis48440b72003-10-31 12:52:35 +0000412
Thomas Woutersa6900e82007-08-30 21:54:39 +0000413 __all__.append("POP3_SSL")
Guido van Rossum03774bb1998-04-09 13:50:55 +0000414
Guido van Rossum484772d1998-04-06 18:27:27 +0000415if __name__ == "__main__":
Eric S. Raymond341f9292001-02-09 06:56:56 +0000416 import sys
417 a = POP3(sys.argv[1])
Tim Peters2344fae2001-01-15 00:50:52 +0000418 print a.getwelcome()
Eric S. Raymond341f9292001-02-09 06:56:56 +0000419 a.user(sys.argv[2])
420 a.pass_(sys.argv[3])
Tim Peters2344fae2001-01-15 00:50:52 +0000421 a.list()
422 (numMsgs, totalSize) = a.stat()
423 for i in range(1, numMsgs + 1):
424 (header, msg, octets) = a.retr(i)
Walter Dörwald70a6b492004-02-12 17:35:32 +0000425 print "Message %d:" % i
Tim Peters2344fae2001-01-15 00:50:52 +0000426 for line in msg:
427 print ' ' + line
428 print '-----------------------'
429 a.quit()