blob: a238510b38fc6a2ddc120d390ce41d6d1caeeb8c [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
Benjamin Petersonfaad6bb2014-12-05 20:02:38 -050035# maximal line length when calling readline(). This is to prevent
36# reading arbitrary length 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):
Benjamin Petersonfaad6bb2014-12-05 20:02:38 -0500112 line = self.file.readline(_MAXLINE + 1)
113 if len(line) > _MAXLINE:
114 raise error_proto('line too long')
Walter Dörwald70a6b492004-02-12 17:35:32 +0000115 if self._debugging > 1: print '*get*', repr(line)
Tim Peters2344fae2001-01-15 00:50:52 +0000116 if not line: raise error_proto('-ERR EOF')
117 octets = len(line)
118 # server can send any combination of CR & LF
119 # however, 'readline()' returns lines ending in LF
120 # so only possibilities are ...LF, ...CRLF, CR...LF
121 if line[-2:] == CRLF:
122 return line[:-2], octets
123 if line[0] == CR:
124 return line[1:-1], octets
125 return line[:-1], octets
Guido van Rossum03774bb1998-04-09 13:50:55 +0000126
Guido van Rossum484772d1998-04-06 18:27:27 +0000127
Tim Peters2344fae2001-01-15 00:50:52 +0000128 # Internal: get a response from the server.
129 # Raise 'error_proto' if the response doesn't start with '+'.
Guido van Rossum03774bb1998-04-09 13:50:55 +0000130
Tim Peters2344fae2001-01-15 00:50:52 +0000131 def _getresp(self):
132 resp, o = self._getline()
Walter Dörwald70a6b492004-02-12 17:35:32 +0000133 if self._debugging > 1: print '*resp*', repr(resp)
Tim Peters2344fae2001-01-15 00:50:52 +0000134 c = resp[:1]
135 if c != '+':
136 raise error_proto(resp)
137 return resp
Guido van Rossum484772d1998-04-06 18:27:27 +0000138
Guido van Rossum03774bb1998-04-09 13:50:55 +0000139
Tim Peters2344fae2001-01-15 00:50:52 +0000140 # Internal: get a response plus following text from the server.
Guido van Rossum03774bb1998-04-09 13:50:55 +0000141
Tim Peters2344fae2001-01-15 00:50:52 +0000142 def _getlongresp(self):
143 resp = self._getresp()
144 list = []; octets = 0
145 line, o = self._getline()
146 while line != '.':
147 if line[:2] == '..':
148 o = o-1
149 line = line[1:]
150 octets = octets + o
151 list.append(line)
152 line, o = self._getline()
153 return resp, list, octets
Guido van Rossum03774bb1998-04-09 13:50:55 +0000154
Guido van Rossum484772d1998-04-06 18:27:27 +0000155
Tim Peters2344fae2001-01-15 00:50:52 +0000156 # Internal: send a command and get the response
Guido van Rossum03774bb1998-04-09 13:50:55 +0000157
Tim Peters2344fae2001-01-15 00:50:52 +0000158 def _shortcmd(self, line):
159 self._putcmd(line)
160 return self._getresp()
Guido van Rossum484772d1998-04-06 18:27:27 +0000161
Guido van Rossum03774bb1998-04-09 13:50:55 +0000162
Tim Peters2344fae2001-01-15 00:50:52 +0000163 # Internal: send a command and get the response plus following text
Guido van Rossum03774bb1998-04-09 13:50:55 +0000164
Tim Peters2344fae2001-01-15 00:50:52 +0000165 def _longcmd(self, line):
166 self._putcmd(line)
167 return self._getlongresp()
Guido van Rossum484772d1998-04-06 18:27:27 +0000168
Guido van Rossum03774bb1998-04-09 13:50:55 +0000169
Tim Peters2344fae2001-01-15 00:50:52 +0000170 # These can be useful:
171
172 def getwelcome(self):
173 return self.welcome
174
175
176 def set_debuglevel(self, level):
177 self._debugging = level
178
179
180 # Here are all the POP commands:
181
182 def user(self, user):
183 """Send user name, return response
Guido van Rossum484772d1998-04-06 18:27:27 +0000184
Tim Peters2344fae2001-01-15 00:50:52 +0000185 (should indicate password required).
186 """
187 return self._shortcmd('USER %s' % user)
Guido van Rossum484772d1998-04-06 18:27:27 +0000188
Guido van Rossum03774bb1998-04-09 13:50:55 +0000189
Tim Peters2344fae2001-01-15 00:50:52 +0000190 def pass_(self, pswd):
191 """Send password, return response
Guido van Rossum484772d1998-04-06 18:27:27 +0000192
Tim Peters2344fae2001-01-15 00:50:52 +0000193 (response includes message count, mailbox size).
Guido van Rossum03774bb1998-04-09 13:50:55 +0000194
Tim Peters2344fae2001-01-15 00:50:52 +0000195 NB: mailbox is locked by server from here to 'quit()'
196 """
197 return self._shortcmd('PASS %s' % pswd)
Guido van Rossum484772d1998-04-06 18:27:27 +0000198
Guido van Rossum03774bb1998-04-09 13:50:55 +0000199
Tim Peters2344fae2001-01-15 00:50:52 +0000200 def stat(self):
201 """Get mailbox status.
Guido van Rossum484772d1998-04-06 18:27:27 +0000202
Tim Peters2344fae2001-01-15 00:50:52 +0000203 Result is tuple of 2 ints (message count, mailbox size)
204 """
205 retval = self._shortcmd('STAT')
Eric S. Raymond341f9292001-02-09 06:56:56 +0000206 rets = retval.split()
Walter Dörwald70a6b492004-02-12 17:35:32 +0000207 if self._debugging: print '*stat*', repr(rets)
Eric S. Raymond341f9292001-02-09 06:56:56 +0000208 numMessages = int(rets[1])
209 sizeMessages = int(rets[2])
Tim Peters2344fae2001-01-15 00:50:52 +0000210 return (numMessages, sizeMessages)
Guido van Rossum03774bb1998-04-09 13:50:55 +0000211
Guido van Rossum03774bb1998-04-09 13:50:55 +0000212
Tim Peters2344fae2001-01-15 00:50:52 +0000213 def list(self, which=None):
214 """Request listing, return result.
Guido van Rossum484772d1998-04-06 18:27:27 +0000215
Tim Peters2344fae2001-01-15 00:50:52 +0000216 Result without a message number argument is in form
Georg Brandl2772c672005-08-05 21:01:58 +0000217 ['response', ['mesg_num octets', ...], octets].
Guido van Rossum484772d1998-04-06 18:27:27 +0000218
Tim Peters2344fae2001-01-15 00:50:52 +0000219 Result when a message number argument is given is a
220 single response: the "scan listing" for that message.
221 """
Raymond Hettinger16e3c422002-06-01 16:07:16 +0000222 if which is not None:
Tim Peters2344fae2001-01-15 00:50:52 +0000223 return self._shortcmd('LIST %s' % which)
224 return self._longcmd('LIST')
Guido van Rossum03774bb1998-04-09 13:50:55 +0000225
Guido van Rossum03774bb1998-04-09 13:50:55 +0000226
Tim Peters2344fae2001-01-15 00:50:52 +0000227 def retr(self, which):
228 """Retrieve whole message number 'which'.
Guido van Rossumf6ae7431998-09-02 14:42:02 +0000229
Tim Peters2344fae2001-01-15 00:50:52 +0000230 Result is in form ['response', ['line', ...], octets].
231 """
232 return self._longcmd('RETR %s' % which)
Guido van Rossum03774bb1998-04-09 13:50:55 +0000233
Guido van Rossum484772d1998-04-06 18:27:27 +0000234
Tim Peters2344fae2001-01-15 00:50:52 +0000235 def dele(self, which):
236 """Delete message number 'which'.
Guido van Rossum03774bb1998-04-09 13:50:55 +0000237
Tim Peters2344fae2001-01-15 00:50:52 +0000238 Result is 'response'.
239 """
240 return self._shortcmd('DELE %s' % which)
Guido van Rossum03774bb1998-04-09 13:50:55 +0000241
Guido van Rossum484772d1998-04-06 18:27:27 +0000242
Tim Peters2344fae2001-01-15 00:50:52 +0000243 def noop(self):
244 """Does nothing.
Guido van Rossum03774bb1998-04-09 13:50:55 +0000245
Tim Peters2344fae2001-01-15 00:50:52 +0000246 One supposes the response indicates the server is alive.
247 """
248 return self._shortcmd('NOOP')
Guido van Rossum03774bb1998-04-09 13:50:55 +0000249
Guido van Rossum484772d1998-04-06 18:27:27 +0000250
Tim Peters2344fae2001-01-15 00:50:52 +0000251 def rset(self):
Raymond Hettingerce956842008-05-17 04:13:36 +0000252 """Unmark all messages marked for deletion."""
Tim Peters2344fae2001-01-15 00:50:52 +0000253 return self._shortcmd('RSET')
Guido van Rossum484772d1998-04-06 18:27:27 +0000254
Guido van Rossum03774bb1998-04-09 13:50:55 +0000255
Tim Peters2344fae2001-01-15 00:50:52 +0000256 def quit(self):
257 """Signoff: commit changes on server, unlock mailbox, close connection."""
258 try:
259 resp = self._shortcmd('QUIT')
260 except error_proto, val:
261 resp = val
262 self.file.close()
263 self.sock.close()
264 del self.file, self.sock
265 return resp
Guido van Rossum484772d1998-04-06 18:27:27 +0000266
Tim Peters2344fae2001-01-15 00:50:52 +0000267 #__del__ = quit
Guido van Rossum484772d1998-04-06 18:27:27 +0000268
Guido van Rossum484772d1998-04-06 18:27:27 +0000269
Tim Peters2344fae2001-01-15 00:50:52 +0000270 # optional commands:
Guido van Rossum03774bb1998-04-09 13:50:55 +0000271
Tim Peters2344fae2001-01-15 00:50:52 +0000272 def rpop(self, user):
273 """Not sure what this does."""
274 return self._shortcmd('RPOP %s' % user)
Guido van Rossum03774bb1998-04-09 13:50:55 +0000275
Guido van Rossum03774bb1998-04-09 13:50:55 +0000276
Benjamin Petersone052d402018-03-03 22:18:17 -0800277 timestamp = re.compile(br'\+OK.[^<]*(<.*>)')
Guido van Rossum03774bb1998-04-09 13:50:55 +0000278
Tim Peters2344fae2001-01-15 00:50:52 +0000279 def apop(self, user, secret):
280 """Authorisation
Guido van Rossum03774bb1998-04-09 13:50:55 +0000281
Tim Peters2344fae2001-01-15 00:50:52 +0000282 - only possible if server has supplied a timestamp in initial greeting.
Guido van Rossum03774bb1998-04-09 13:50:55 +0000283
Tim Peters2344fae2001-01-15 00:50:52 +0000284 Args:
285 user - mailbox user;
286 secret - secret shared between client and server.
Guido van Rossum03774bb1998-04-09 13:50:55 +0000287
Tim Peters2344fae2001-01-15 00:50:52 +0000288 NB: mailbox is locked by server from here to 'quit()'
289 """
Moshe Zadkaccc2e3d2001-01-19 19:56:27 +0000290 m = self.timestamp.match(self.welcome)
291 if not m:
Tim Peters2344fae2001-01-15 00:50:52 +0000292 raise error_proto('-ERR APOP not supported by server')
Georg Brandlbffb0bc2006-04-30 08:57:35 +0000293 import hashlib
294 digest = hashlib.md5(m.group(1)+secret).digest()
Eric S. Raymond341f9292001-02-09 06:56:56 +0000295 digest = ''.join(map(lambda x:'%02x'%ord(x), digest))
Tim Peters2344fae2001-01-15 00:50:52 +0000296 return self._shortcmd('APOP %s %s' % (user, digest))
Guido van Rossum03774bb1998-04-09 13:50:55 +0000297
Guido van Rossum03774bb1998-04-09 13:50:55 +0000298
Tim Peters2344fae2001-01-15 00:50:52 +0000299 def top(self, which, howmuch):
300 """Retrieve message header of message number 'which'
301 and first 'howmuch' lines of message body.
Guido van Rossum03774bb1998-04-09 13:50:55 +0000302
Tim Peters2344fae2001-01-15 00:50:52 +0000303 Result is in form ['response', ['line', ...], octets].
304 """
305 return self._longcmd('TOP %s %s' % (which, howmuch))
Guido van Rossum03774bb1998-04-09 13:50:55 +0000306
Guido van Rossum03774bb1998-04-09 13:50:55 +0000307
Tim Peters2344fae2001-01-15 00:50:52 +0000308 def uidl(self, which=None):
309 """Return message digest (unique id) list.
Guido van Rossum03774bb1998-04-09 13:50:55 +0000310
Tim Peters2344fae2001-01-15 00:50:52 +0000311 If 'which', result contains unique id for that message
312 in the form 'response mesgnum uid', otherwise result is
313 the list ['response', ['mesgnum uid', ...], octets]
314 """
Raymond Hettinger16e3c422002-06-01 16:07:16 +0000315 if which is not None:
Tim Peters2344fae2001-01-15 00:50:52 +0000316 return self._shortcmd('UIDL %s' % which)
317 return self._longcmd('UIDL')
Guido van Rossum03774bb1998-04-09 13:50:55 +0000318
Bill Janssen426ea0a2007-08-29 22:35:05 +0000319try:
320 import ssl
321except ImportError:
322 pass
323else:
Martin v. Löwis48440b72003-10-31 12:52:35 +0000324
Bill Janssen426ea0a2007-08-29 22:35:05 +0000325 class POP3_SSL(POP3):
326 """POP3 client class over SSL connection
Martin v. Löwis48440b72003-10-31 12:52:35 +0000327
Bill Janssen426ea0a2007-08-29 22:35:05 +0000328 Instantiate with: POP3_SSL(hostname, port=995, keyfile=None, certfile=None)
Martin v. Löwis48440b72003-10-31 12:52:35 +0000329
Bill Janssen426ea0a2007-08-29 22:35:05 +0000330 hostname - the hostname of the pop3 over ssl server
331 port - port number
Ezio Melottif5469cf2013-08-17 15:43:51 +0300332 keyfile - PEM formatted file that contains your private key
Bill Janssen426ea0a2007-08-29 22:35:05 +0000333 certfile - PEM formatted certificate chain file
Martin v. Löwis48440b72003-10-31 12:52:35 +0000334
Bill Janssen426ea0a2007-08-29 22:35:05 +0000335 See the methods of the parent class POP3 for more documentation.
336 """
Martin v. Löwis48440b72003-10-31 12:52:35 +0000337
Bill Janssen426ea0a2007-08-29 22:35:05 +0000338 def __init__(self, host, port = POP3_SSL_PORT, keyfile = None, certfile = None):
339 self.host = host
340 self.port = port
341 self.keyfile = keyfile
342 self.certfile = certfile
343 self.buffer = ""
344 msg = "getaddrinfo returns an empty list"
345 self.sock = None
346 for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
347 af, socktype, proto, canonname, sa = res
348 try:
349 self.sock = socket.socket(af, socktype, proto)
350 self.sock.connect(sa)
351 except socket.error, msg:
352 if self.sock:
353 self.sock.close()
354 self.sock = None
355 continue
356 break
357 if not self.sock:
358 raise socket.error, msg
359 self.file = self.sock.makefile('rb')
Bill Janssen98d19da2007-09-10 21:51:02 +0000360 self.sslobj = ssl.wrap_socket(self.sock, self.keyfile, self.certfile)
Bill Janssen426ea0a2007-08-29 22:35:05 +0000361 self._debugging = 0
362 self.welcome = self._getresp()
Martin v. Löwis48440b72003-10-31 12:52:35 +0000363
Bill Janssen426ea0a2007-08-29 22:35:05 +0000364 def _fillBuffer(self):
365 localbuf = self.sslobj.read()
366 if len(localbuf) == 0:
367 raise error_proto('-ERR EOF')
368 self.buffer += localbuf
369
370 def _getline(self):
371 line = ""
372 renewline = re.compile(r'.*?\n')
Martin v. Löwis48440b72003-10-31 12:52:35 +0000373 match = renewline.match(self.buffer)
Bill Janssen426ea0a2007-08-29 22:35:05 +0000374 while not match:
375 self._fillBuffer()
Benjamin Petersonfaad6bb2014-12-05 20:02:38 -0500376 if len(self.buffer) > _MAXLINE:
377 raise error_proto('line too long')
Bill Janssen426ea0a2007-08-29 22:35:05 +0000378 match = renewline.match(self.buffer)
379 line = match.group(0)
380 self.buffer = renewline.sub('' ,self.buffer, 1)
381 if self._debugging > 1: print '*get*', repr(line)
Martin v. Löwis48440b72003-10-31 12:52:35 +0000382
Bill Janssen426ea0a2007-08-29 22:35:05 +0000383 octets = len(line)
384 if line[-2:] == CRLF:
385 return line[:-2], octets
386 if line[0] == CR:
387 return line[1:-1], octets
388 return line[:-1], octets
Martin v. Löwis48440b72003-10-31 12:52:35 +0000389
Bill Janssen426ea0a2007-08-29 22:35:05 +0000390 def _putline(self, line):
391 if self._debugging > 1: print '*put*', repr(line)
392 line += CRLF
393 bytes = len(line)
394 while bytes > 0:
395 sent = self.sslobj.write(line)
396 if sent == bytes:
397 break # avoid copy
398 line = line[sent:]
399 bytes = bytes - sent
Martin v. Löwis48440b72003-10-31 12:52:35 +0000400
Bill Janssen426ea0a2007-08-29 22:35:05 +0000401 def quit(self):
402 """Signoff: commit changes on server, unlock mailbox, close connection."""
403 try:
404 resp = self._shortcmd('QUIT')
405 except error_proto, val:
406 resp = val
407 self.sock.close()
408 del self.sslobj, self.sock
409 return resp
Martin v. Löwis48440b72003-10-31 12:52:35 +0000410
Thomas Woutersa6900e82007-08-30 21:54:39 +0000411 __all__.append("POP3_SSL")
Guido van Rossum03774bb1998-04-09 13:50:55 +0000412
Guido van Rossum484772d1998-04-06 18:27:27 +0000413if __name__ == "__main__":
Eric S. Raymond341f9292001-02-09 06:56:56 +0000414 import sys
415 a = POP3(sys.argv[1])
Tim Peters2344fae2001-01-15 00:50:52 +0000416 print a.getwelcome()
Eric S. Raymond341f9292001-02-09 06:56:56 +0000417 a.user(sys.argv[2])
418 a.pass_(sys.argv[3])
Tim Peters2344fae2001-01-15 00:50:52 +0000419 a.list()
420 (numMsgs, totalSize) = a.stat()
421 for i in range(1, numMsgs + 1):
422 (header, msg, octets) = a.retr(i)
Walter Dörwald70a6b492004-02-12 17:35:32 +0000423 print "Message %d:" % i
Tim Peters2344fae2001-01-15 00:50:52 +0000424 for line in msg:
425 print ' ' + line
426 print '-----------------------'
427 a.quit()