Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/smtplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ def __init__(self, host='', port=0, local_hostname=None,
will be used.

"""
self._host = host
self.timeout = timeout
self.esmtp_features = {}
self.command_encoding = 'ascii'
Expand Down Expand Up @@ -335,6 +334,7 @@ def connect(self, host='localhost', port=0, source_address=None):
port = int(port)
except ValueError:
raise OSError("nonnumeric port")
self._host = host
if not port:
port = self.default_port
sys.audit("smtplib.connect", self, host, port)
Expand Down
28 changes: 28 additions & 0 deletions Lib/test/test_smtplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,34 @@ def testBasic(self):
# connect
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
timeout=support.LOOPBACK_TIMEOUT)
# implicit .connect must set ._host since it is used by .starttls
self.assertEqual(smtp._host, HOST)
smtp.quit()

def test_host_port_host(self):
# create instance with host:port notation
smtp = smtplib.SMTP(f"{HOST}:{self.port}", local_hostname='localhost',
timeout=support.LOOPBACK_TIMEOUT)
# implicit .connect must set ._host since it is used by .starttls
self.assertEqual(smtp._host, HOST)
smtp.quit()

def test_explicit_connect(self):
# create instance without arguments
smtp = smtplib.SMTP(local_hostname='localhost',
timeout=support.LOOPBACK_TIMEOUT)
# explicit .connect must set ._host since it is used by .starttls
smtp.connect(HOST, self.port)
self.assertEqual(smtp._host, HOST)
smtp.quit()

def test_explicit_connect_host_port(self):
# create instance without arguments
smtp = smtplib.SMTP(local_hostname='localhost',
timeout=support.LOOPBACK_TIMEOUT)
# explicit .connect with host:port notation must set ._host, too
smtp.connect(f"{HOST}:{self.port}")
self.assertEqual(smtp._host, HOST)
smtp.quit()

def testSourceAddress(self):
Expand Down
53 changes: 53 additions & 0 deletions Lib/test/test_smtpnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,59 @@ def test_connect_starttls(self):
server.ehlo()
server.quit()

def test_connect_host_port_starttls(self):
support.get_attribute(smtplib, 'SMTP_SSL')
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
with socket_helper.transient_internet(self.testServer):
server = smtplib.SMTP(f'{self.testServer}:{self.remotePort}')
try:
server.starttls(context=context)
except smtplib.SMTPException as e:
if e.args[0] == 'STARTTLS extension not supported by server.':
unittest.skip(e.args[0])
else:
raise
server.ehlo()
server.quit()

def test_explicit_connect_starttls(self):
support.get_attribute(smtplib, 'SMTP_SSL')
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
with socket_helper.transient_internet(self.testServer):
server = smtplib.SMTP()
server.connect(self.testServer, self.remotePort)
try:
server.starttls(context=context)
except smtplib.SMTPException as e:
if e.args[0] == 'STARTTLS extension not supported by server.':
unittest.skip(e.args[0])
else:
raise
server.ehlo()
server.quit()

def test_explicit_connect_host_port_starttls(self):
support.get_attribute(smtplib, 'SMTP_SSL')
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
with socket_helper.transient_internet(self.testServer):
server = smtplib.SMTP()
server.connect(f'{self.testServer}:{self.remotePort}')
try:
server.starttls(context=context)
except smtplib.SMTPException as e:
if e.args[0] == 'STARTTLS extension not supported by server.':
unittest.skip(e.args[0])
else:
raise
server.ehlo()
server.quit()


class SmtpSSLTest(unittest.TestCase):
testServer = 'smtp.gmail.com'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
In smtplib.SMTP, set ._host in .connect() and not immediately during instantiation. This allows .starttls() to work if the instance was created without arguments and host and port were only provided via an explicit .connect() call. If will also set ._host to the correct host value if the host and port were specified using the 'host:port' notation, which according to the documentation is supported by .connect().
Loading