Skip to content

Commit f735ee5

Browse files
committed
reuse 127.0.0.1 everywhere???
1 parent 4ad442a commit f735ee5

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

Lib/test/test_httpservers.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,31 +1478,26 @@ def fetch_file(self, path, context=None):
14781478
return res.read()
14791479

14801480
def parse_cli_output(self, output):
1481-
matches = re.search(r'Serving (HTTP|HTTPS) on (.+) port (\d+)', output)
1482-
if matches is None:
1481+
match = re.search(r'Serving (HTTP|HTTPS) on (.+) port (\d+)', output)
1482+
if match is None:
14831483
return None, None, None
1484-
return matches.group(1).lower(), matches.group(2), int(matches.group(3))
1484+
return match.group(1).lower(), match.group(2), int(match.group(3))
14851485

1486-
def wait_for_server(self, proc, protocol, port):
1487-
"""Extract the server bind address once it has been started."""
1486+
def wait_for_server(self, proc, protocol, bind, port):
1487+
"""Check that the server has been successfully started."""
14881488
line = proc.stdout.readline().strip()
14891489
if support.verbose:
14901490
print()
14911491
print('python -m http.server: ', line)
1492-
parsed_protocol, host, parsed_port = self.parse_cli_output(line)
1493-
if protocol == parsed_protocol and parsed_port == port:
1494-
return host
1495-
return None
1492+
return self.parse_cli_output(line) == (protocol, bind, port)
14961493

14971494
def test_http_client(self):
1498-
port = find_unused_port()
1499-
proc = spawn_python('-u', '-m', 'http.server', str(port),
1495+
bind, port = '127.0.0.1', find_unused_port()
1496+
proc = spawn_python('-u', '-m', 'http.server', str(port), '-b', bind,
15001497
bufsize=1, text=True)
15011498
self.addCleanup(kill_python, proc)
15021499
self.addCleanup(proc.terminate)
1503-
bind = self.wait_for_server(proc, 'http', port)
1504-
self.assertIsNotNone(bind)
1505-
# localhost may be redirected to something else for whatever reason
1500+
self.assertTrue(self.wait_for_server(proc, 'http', bind, port))
15061501
res = self.fetch_file(f'http://{bind}:{port}/{self.served_filename}')
15071502
self.assertEqual(res, self.served_data)
15081503

@@ -1514,16 +1509,14 @@ def test_https_client(self):
15141509
context.verify_mode = ssl.CERT_NONE
15151510

15161511
bind, port = '127.0.0.1', find_unused_port()
1517-
proc = spawn_python('-u', '-m', 'http.server', str(port),
1518-
'-b', bind,
1512+
proc = spawn_python('-u', '-m', 'http.server', str(port), '-b', bind,
15191513
'--tls-cert', self.tls_cert,
15201514
'--tls-key', self.tls_key,
15211515
'--tls-password-file', self.tls_password_file,
15221516
bufsize=1, text=True)
15231517
self.addCleanup(kill_python, proc)
15241518
self.addCleanup(proc.terminate)
1525-
bind = self.wait_for_server(proc, 'https', port)
1526-
self.assertIsNotNone(bind)
1519+
self.assertTrue(self.wait_for_server(proc, 'https', bind, port))
15271520
url = f'https://{bind}:{port}/{self.served_filename}'
15281521
res = self.fetch_file(url, context=context)
15291522
self.assertEqual(res, self.served_data)

0 commit comments

Comments
 (0)