|
35 | 35 | is_apple, import_helper, os_helper, threading_helper |
36 | 36 | ) |
37 | 37 | from test.support.script_helper import kill_python, spawn_python |
38 | | -from test.support.socket_helper import find_unused_port |
39 | 38 |
|
40 | 39 | try: |
41 | 40 | import ssl |
@@ -1491,48 +1490,38 @@ def parse_cli_output(self, output): |
1491 | 1490 | return None, None, None |
1492 | 1491 | return matches.group(1), matches.group(2), int(matches.group(3)) |
1493 | 1492 |
|
1494 | | - def wait_for_server(self, proc, protocol, port, bind, timeout=50): |
1495 | | - """Check the server process output. |
1496 | | -
|
1497 | | - Return True if the server was successfully started |
1498 | | - and is listening on the given port and bind address. |
1499 | | - """ |
1500 | | - while timeout > 0: |
1501 | | - line = proc.stdout.readline() |
1502 | | - if not line: |
1503 | | - time.sleep(0.1) |
1504 | | - timeout -= 1 |
1505 | | - continue |
1506 | | - protocol_, host_, port_ = self.parse_cli_output(line) |
1507 | | - if not protocol_ or not host_ or not port_: |
1508 | | - time.sleep(0.1) |
1509 | | - timeout -= 1 |
1510 | | - continue |
1511 | | - if protocol_ == protocol and host_ == bind and port_ == port: |
1512 | | - return True |
1513 | | - break |
1514 | | - return False |
| 1493 | + def wait_for_server(self, proc, protocol): |
| 1494 | + """Check the server process output.""" |
| 1495 | + line = proc.stdout.readline() |
| 1496 | + if line: |
| 1497 | + if support.verbose > 1: |
| 1498 | + print(line) |
| 1499 | + parsed_protocol, host, port = self.parse_cli_output(line) |
| 1500 | + if protocol == parsed_protocol: |
| 1501 | + return host, port |
| 1502 | + return None, None |
1515 | 1503 |
|
1516 | 1504 | def test_http_client(self): |
1517 | | - bind, port = 'localhost', find_unused_port() |
1518 | | - proc = spawn_python('-u', '-m', 'http.server', str(port), '-b', bind, |
1519 | | - bufsize=1, text=True) |
| 1505 | + proc = spawn_python('-u', '-m', 'http.server', bufsize=1, text=True) |
1520 | 1506 | self.addCleanup(kill_python, proc) |
1521 | 1507 | self.addCleanup(proc.terminate) |
1522 | | - self.assertTrue(self.wait_for_server(proc, 'http', port, bind)) |
| 1508 | + bind, port = self.wait_for_server(proc, 'http') |
| 1509 | + self.assertIsNotNone(bind) |
| 1510 | + self.assertIsNotNone(port) |
1523 | 1511 | res = self.fetch_file(f'http://{bind}:{port}/{self.served_file_name}') |
1524 | 1512 | self.assertEqual(res, self.served_data) |
1525 | 1513 |
|
1526 | 1514 | def test_https_client(self): |
1527 | | - bind, port = 'localhost', find_unused_port() |
1528 | | - proc = spawn_python('-u', '-m', 'http.server', str(port), '-b', bind, |
| 1515 | + proc = spawn_python('-u', '-m', 'http.server', |
1529 | 1516 | '--tls-cert', self.tls_cert, |
1530 | 1517 | '--tls-key', self.tls_key, |
1531 | 1518 | '--tls-password-file', self.tls_password_file, |
1532 | 1519 | bufsize=1, text=True) |
1533 | 1520 | self.addCleanup(kill_python, proc) |
1534 | 1521 | self.addCleanup(proc.terminate) |
1535 | | - self.assertTrue(self.wait_for_server(proc, 'https', port, bind)) |
| 1522 | + bind, port = self.wait_for_server(proc, 'https') |
| 1523 | + self.assertIsNotNone(bind) |
| 1524 | + self.assertIsNotNone(port) |
1536 | 1525 | url = f'https://{bind}:{port}/{self.served_file_name}' |
1537 | 1526 | res = self.fetch_file(url, context=self.get_ssl_context()) |
1538 | 1527 | self.assertEqual(res, self.served_data) |
|
0 commit comments