Skip to content

Commit f46a17b

Browse files
[3.14] gh-70039: smtplib: store the server name in ._host in .connect() (GH-115259) (#148273)
Original patch by gigaplastik, extended with a few more tests. Addresses gh-70039 and bpo-25852: failure of starttls if connect is called explicitly. (cherry picked from commit 442f83a) Co-authored-by: nmartensen <nis.martensen@web.de>
1 parent a84e2db commit f46a17b

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

Lib/smtplib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ def __init__(self, host='', port=0, local_hostname=None,
251251
will be used.
252252
253253
"""
254-
self._host = host
255254
self.timeout = timeout
256255
self.esmtp_features = {}
257256
self.command_encoding = 'ascii'
@@ -342,6 +341,7 @@ def connect(self, host='localhost', port=0, source_address=None):
342341
port = int(port)
343342
except ValueError:
344343
raise OSError("nonnumeric port")
344+
self._host = host
345345
if not port:
346346
port = self.default_port
347347
sys.audit("smtplib.connect", self, host, port)

Lib/test/test_smtpnet.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,59 @@ def test_connect_starttls(self):
4545
server.ehlo()
4646
server.quit()
4747

48+
def test_connect_host_port_starttls(self):
49+
support.get_attribute(smtplib, 'SMTP_SSL')
50+
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
51+
context.check_hostname = False
52+
context.verify_mode = ssl.CERT_NONE
53+
with socket_helper.transient_internet(self.testServer):
54+
server = smtplib.SMTP(f'{self.testServer}:{self.remotePort}')
55+
try:
56+
server.starttls(context=context)
57+
except smtplib.SMTPException as e:
58+
if e.args[0] == 'STARTTLS extension not supported by server.':
59+
unittest.skip(e.args[0])
60+
else:
61+
raise
62+
server.ehlo()
63+
server.quit()
64+
65+
def test_explicit_connect_starttls(self):
66+
support.get_attribute(smtplib, 'SMTP_SSL')
67+
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
68+
context.check_hostname = False
69+
context.verify_mode = ssl.CERT_NONE
70+
with socket_helper.transient_internet(self.testServer):
71+
server = smtplib.SMTP()
72+
server.connect(self.testServer, self.remotePort)
73+
try:
74+
server.starttls(context=context)
75+
except smtplib.SMTPException as e:
76+
if e.args[0] == 'STARTTLS extension not supported by server.':
77+
unittest.skip(e.args[0])
78+
else:
79+
raise
80+
server.ehlo()
81+
server.quit()
82+
83+
def test_explicit_connect_host_port_starttls(self):
84+
support.get_attribute(smtplib, 'SMTP_SSL')
85+
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
86+
context.check_hostname = False
87+
context.verify_mode = ssl.CERT_NONE
88+
with socket_helper.transient_internet(self.testServer):
89+
server = smtplib.SMTP()
90+
server.connect(f'{self.testServer}:{self.remotePort}')
91+
try:
92+
server.starttls(context=context)
93+
except smtplib.SMTPException as e:
94+
if e.args[0] == 'STARTTLS extension not supported by server.':
95+
unittest.skip(e.args[0])
96+
else:
97+
raise
98+
server.ehlo()
99+
server.quit()
100+
48101

49102
class SmtpSSLTest(unittest.TestCase):
50103
testServer = SMTP_TEST_SERVER
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed bug where :meth:`smtplib.SMTP.starttls` could fail if :meth:`smtplib.SMTP.connect` is called explicitly rather than implicitly.

0 commit comments

Comments
 (0)