Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 48440b7

Browse files
committed
Patch #: Add POP3 over SSL support.
1 parent 9ad853b commit 48440b7

4 files changed

Lines changed: 110 additions & 3 deletions

File tree

Doc/lib/libpoplib.tex

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ \section{\module{poplib} ---
1313
\indexii{POP3}{protocol}
1414

1515
This module defines a class, \class{POP3}, which encapsulates a
16-
connection to an POP3 server and implements the protocol as defined in
16+
connection to a POP3 server and implements the protocol as defined in
1717
\rfc{1725}. The \class{POP3} class supports both the minimal and
18-
optional command sets.
18+
optional command sets. Additionally, this module provides a class
19+
\class{POP3_SSL}, which provides support for connecting to POP3
20+
servers that use SSL as an underlying protocol layer.
21+
1922

2023
Note that POP3, though widely supported, is obsolescent. The
2124
implementation quality of POP3 servers varies widely, and too many are
@@ -31,6 +34,16 @@ \section{\module{poplib} ---
3134
If \var{port} is omitted, the standard POP3 port (110) is used.
3235
\end{classdesc}
3336

37+
\begin{classdesc}{POP3_SSL}{host\optional{, port\optional{, keyfile\optional{, certfile}}}}
38+
This is a subclass of \class{POP3} that connects to the server over an
39+
SSL encrypted socket. If \var{port} is not specified, 995, the
40+
standard POP3-over-SSL port is used. \var{keyfile} and \var{certfile}
41+
are also optional - they can contain a PEM formatted private key and
42+
certificate chain file for the SSL connection.
43+
44+
\versionadded{2.4}
45+
\end{classdesc}
46+
3447
One exception is defined as an attribute of the \module{poplib} module:
3548

3649
\begin{excdesc}{error_proto}
@@ -143,6 +156,9 @@ \subsection{POP3 Objects \label{pop3-objects}}
143156
\var{octets})}.
144157
\end{methoddesc}
145158

159+
Instances of \class{POP3_SSL} have no additional methods. The
160+
interface of this subclass is identical to its parent.
161+
146162

147163
\subsection{POP3 Example \label{pop3-example}}
148164

Lib/poplib.py

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
# [heavily stealing from nntplib.py]
88
# Updated: Piers Lauder <[email protected]> [Jul '97]
99
# String method conversion and test jig improvements by ESR, February 2001.
10+
# Added the POP3_SSL class. Methods loosely based on IMAP_SSL. Hector Urtubia <[email protected]> Aug 2003
1011

1112
# Example (see the test function at the end of this file)
1213

1314
# Imports
1415

1516
import re, socket
1617

17-
__all__ = ["POP3","error_proto"]
18+
__all__ = ["POP3","error_proto","POP3_SSL"]
1819

1920
# Exception raised when an error or invalid response is received:
2021

@@ -23,6 +24,9 @@ class error_proto(Exception): pass
2324
# Standard Port
2425
POP3_PORT = 110
2526

27+
# POP SSL PORT
28+
POP3_SSL_PORT = 995
29+
2630
# Line terminators (we always output CRLF, but accept any of CRLF, LFCR, LF)
2731
CR = '\r'
2832
LF = '\n'
@@ -317,6 +321,90 @@ def uidl(self, which=None):
317321
return self._shortcmd('UIDL %s' % which)
318322
return self._longcmd('UIDL')
319323

324+
class POP3_SSL(POP3):
325+
"""POP3 client class over SSL connection
326+
327+
Instantiate with: POP3_SSL(hostname, port=995, keyfile=None, certfile=None)
328+
329+
hostname - the hostname of the pop3 over ssl server
330+
port - port number
331+
keyfile - PEM formatted file that countains your private key
332+
certfile - PEM formatted certificate chain file
333+
334+
See the methods of the parent class POP3 for more documentation.
335+
"""
336+
337+
def __init__(self, host, port = POP3_SSL_PORT, keyfile = None, certfile = None):
338+
self.host = host
339+
self.port = port
340+
self.keyfile = keyfile
341+
self.certfile = certfile
342+
self.buffer = ""
343+
msg = "getaddrinfo returns an empty list"
344+
self.sock = None
345+
for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
346+
af, socktype, proto, canonname, sa = res
347+
try:
348+
self.sock = socket.socket(af, socktype, proto)
349+
self.sock.connect(sa)
350+
except socket.error, msg:
351+
if self.sock:
352+
self.sock.close()
353+
self.sock = None
354+
continue
355+
break
356+
if not self.sock:
357+
raise socket.error, msg
358+
self.file = self.sock.makefile('rb')
359+
self.sslobj = socket.ssl(self.sock, self.keyfile, self.certfile)
360+
self._debugging = 0
361+
self.welcome = self._getresp()
362+
363+
def _fillBuffer(self):
364+
localbuf = self.sslobj.read()
365+
if len(localbuf) == 0:
366+
raise error_proto('-ERR EOF')
367+
self.buffer += localbuf
368+
369+
def _getline(self):
370+
line = ""
371+
renewline = re.compile(r'.*?\n')
372+
match = renewline.match(self.buffer)
373+
while not match:
374+
self._fillBuffer()
375+
match = renewline.match(self.buffer)
376+
line = match.group(0)
377+
self.buffer = renewline.sub('' ,self.buffer, 1)
378+
if self._debugging > 1: print '*get*', `line`
379+
380+
octets = len(line)
381+
if line[-2:] == CRLF:
382+
return line[:-2], octets
383+
if line[0] == CR:
384+
return line[1:-1], octets
385+
return line[:-1], octets
386+
387+
def _putline(self, line):
388+
if self._debugging > 1: print '*put*', `line`
389+
line += CRLF
390+
bytes = len(line)
391+
while bytes > 0:
392+
sent = self.sslobj.write(line)
393+
if sent == bytes:
394+
break # avoid copy
395+
line = line[sent:]
396+
bytes = bytes - sent
397+
398+
def quit(self):
399+
"""Signoff: commit changes on server, unlock mailbox, close connection."""
400+
try:
401+
resp = self._shortcmd('QUIT')
402+
except error_proto, val:
403+
resp = val
404+
self.sock.close()
405+
del self.sslobj, self.sock
406+
return resp
407+
320408

321409
if __name__ == "__main__":
322410
import sys

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ Stephen Turner
562562
Bill Tutt
563563
Doobee R. Tzeck
564564
Lionel Ulmer
565+
Hector Urtubia
565566
Frank Vercruesse
566567
Jaap Vermeulen
567568
Al Vezza

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ Extension modules
101101
Library
102102
-------
103103

104+
- poplib.POP3_SSL has been added.
105+
104106
- tmpfile.mkstemp now returns an absolute path even if dir is relative.
105107

106108
- urlparse is RFC 2396 compliant.

0 commit comments

Comments
 (0)