Skip to content

Commit 7e35ead

Browse files
committed
smtplib: store the server name in ._host in .connect()
Original patch by gigaplastik, extended with a few more tests. Addresses: #70039 BPO 25852
1 parent 57d31ec commit 7e35ead

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

Lib/smtplib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ def __init__(self, host='', port=0, local_hostname=None,
244244
will be used.
245245
246246
"""
247-
self._host = host
248247
self.timeout = timeout
249248
self.esmtp_features = {}
250249
self.command_encoding = 'ascii'
@@ -335,6 +334,7 @@ def connect(self, host='localhost', port=0, source_address=None):
335334
port = int(port)
336335
except ValueError:
337336
raise OSError("nonnumeric port")
337+
self._host = host
338338
if not port:
339339
port = self.default_port
340340
sys.audit("smtplib.connect", self, host, port)

Lib/test/test_smtplib.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,32 @@ def test_debuglevel_2(self):
158158
re.MULTILINE)
159159
self.assertRegex(stderr.getvalue(), expected)
160160

161+
def test_host_port_host(self):
162+
mock_socket.reply_with(b"220 Hello world")
163+
# create instance without arguments
164+
smtp = smtplib.SMTP(f"{HOST}:{self.port}")
165+
# .connect must set ._host since it is used by .starttls
166+
self.assertEqual(smtp._host, HOST)
167+
smtp.close()
168+
169+
def test_explicit_connect(self):
170+
mock_socket.reply_with(b"220 Hello world")
171+
# create instance without arguments
172+
smtp = smtplib.SMTP()
173+
# .connect must set ._host since it is used by .starttls
174+
smtp.connect(HOST, self.port)
175+
self.assertEqual(smtp._host, HOST)
176+
smtp.close()
177+
178+
def test_explicit_connect_host_port(self):
179+
mock_socket.reply_with(b"220 Hello world")
180+
# create instance without arguments
181+
smtp = smtplib.SMTP()
182+
# .connect must set ._host since it is used by .starttls
183+
smtp.connect(f"{HOST}:{self.port}")
184+
self.assertEqual(smtp._host, HOST)
185+
smtp.close()
186+
161187

162188
class SMTPGeneralTests(GeneralTests, unittest.TestCase):
163189

Lib/test/test_smtpnet.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,59 @@ def test_connect_starttls(self):
4242
server.ehlo()
4343
server.quit()
4444

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

4699
class SmtpSSLTest(unittest.TestCase):
47100
testServer = 'smtp.gmail.com'

0 commit comments

Comments
 (0)