cpython: 26839edf3cc1 (original) (raw)

Mercurial > cpython

changeset 71617:26839edf3cc1

Fix closes Issue11281 - smtplib.STMP gets source_address parameter, which adds the ability to bind to specific source address on a machine with multiple interfaces. Patch by Paulo Scardine.

Senthil Kumaran senthil@uthcode.com
date Sat, 30 Jul 2011 10:56:50 +0800
parents 018e14a46454
children f207a41c6063
files Doc/library/smtplib.rst Lib/smtplib.py Lib/test/mock_socket.py Lib/test/test_smtplib.py Misc/NEWS
diffstat 5 files changed, 75 insertions(+), 22 deletions(-)[+] [-] Doc/library/smtplib.rst 33 Lib/smtplib.py 40 Lib/test/mock_socket.py 3 Lib/test/test_smtplib.py 17 Misc/NEWS 4

line wrap: on

line diff

--- a/Doc/library/smtplib.rst +++ b/Doc/library/smtplib.rst @@ -20,7 +20,7 @@ details of SMTP and ESMTP operation, con Protocol) and :rfc:1869 (SMTP Service Extensions). -.. class:: SMTP(host='', port=0, local_hostname=None[, timeout]) +.. class:: SMTP(host='', port=0, local_hostname=None[, timeout], source_address=None) A :class:SMTP instance encapsulates an SMTP connection. It has methods that support a full repertoire of SMTP and ESMTP operations. If the optional @@ -29,7 +29,12 @@ Protocol) and :rfc:1869 (SMTP Service raised if the specified host doesn't respond correctly. The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout

-.. class:: SMTP_SSL(host='', port=0, local_hostname=None, keyfile=None, certfile=None[, timeout], context=None) +.. class:: SMTP_SSL(host='', port=0, local_hostname=None, keyfile=None, certfile=None[, timeout], context=None, source_address=None) A :class:SMTP_SSL instance behaves exactly the same as instances of :class:SMTP. :class:SMTP_SSL should be used for situations where SSL is @@ -62,18 +69,28 @@ Protocol) and :rfc:1869 (SMTP Service keyfile and certfile must be None. The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting

-.. class:: LMTP(host='', port=LMTP_PORT, local_hostname=None) + +.. class:: LMTP(host='', port=LMTP_PORT, local_hostname=None, source_address=None) The LMTP protocol, which is very similar to ESMTP, is heavily based on the

--- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -215,7 +215,8 @@ class SMTP: default_port = SMTP_PORT def init(self, host='', port=0, local_hostname=None,

If specified, host' is the name of the remote host to which to[](#l2.12) @@ -223,11 +224,16 @@ class SMTP:[](#l2.13) By default, smtplib.SMTP_PORT is used. An SMTPConnectError is raised[](#l2.14) if the specified host' doesn't respond correctly. If specified, local_hostname is used as the FQDN of the local host. By default,

""" self.timeout = timeout self.esmtp_features = {}

+ if host: (code, msg) = self.connect(host, port) if code != 220: @@ -276,10 +282,11 @@ class SMTP: # This makes it simpler for SMTP_SSL to use the SMTP connect code # and just alter the socket connection bit. if self.debuglevel > 0:

If the hostname ends with a colon (`:') followed by a number, and @@ -290,6 +297,7 @@ class SMTP: specified during instantiation. """

@@ -829,7 +837,8 @@ if _have_ssl: """ This is a subclass derived from SMTP that connects over an SSL encrypted socket (to use this class you need a socket module that was compiled with SSL support). If host is not specified, '' (the local host) is used. If port is

@@ -840,7 +849,8 @@ if _have_ssl: def init(self, host='', port=0, local_hostname=None, keyfile=None, certfile=None,

@@ -850,12 +860,14 @@ if _have_ssl: self.keyfile = keyfile self.certfile = certfile self.context = context

def _get_socket(self, host, port, timeout): if self.debuglevel > 0: print('connect:', (host, port), file=stderr)

@@ -884,14 +896,16 @@ class LMTP(SMTP): ehlo_msg = "lhlo"

# Handle Unix-domain sockets. try:

--- a/Lib/test/mock_socket.py +++ b/Lib/test/mock_socket.py @@ -106,7 +106,8 @@ def socket(family=None, type=None, proto return MockSocket() -def create_connection(address, timeout=socket_module._GLOBAL_DEFAULT_TIMEOUT): +def create_connection(address, timeout=socket_module._GLOBAL_DEFAULT_TIMEOUT,

--- a/Lib/test/test_smtplib.py +++ b/Lib/test/test_smtplib.py @@ -72,6 +72,14 @@ class GeneralTests(unittest.TestCase): smtp = smtplib.SMTP(HOST, self.port) smtp.close()

+ def testBasic2(self): mock_socket.reply_with(b"220 Hola mundo") # connects, include port in host name @@ -206,6 +214,15 @@ class DebuggingServerTests(unittest.Test smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) smtp.quit()

+ def testNOOP(self): smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) expected = (250, b'Ok')

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -246,6 +246,10 @@ Core and Builtins Library ------- +- Issue #11281: smtplib.STMP gets source_address parameter, which adds the