| 
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,41 @@ 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:  | 
 | 1493 | +    def wait_for_server(self, proc, protocol):  | 
 | 1494 | +        """Check the server process output."""  | 
 | 1495 | +        for _ in range(10):  | 
1501 | 1496 |             line = proc.stdout.readline()  | 
1502 | 1497 |             if not line:  | 
1503 |  | -                time.sleep(0.1)  | 
1504 |  | -                timeout -= 1  | 
 | 1498 | +                time.sleep(0.5)  | 
1505 | 1499 |                 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  | 
 | 1500 | +            if support.verbose > 1:  | 
 | 1501 | +                print(line)  | 
 | 1502 | +            parsed_protocol, host, port = self.parse_cli_output(line)  | 
 | 1503 | +            if protocol == parsed_protocol:  | 
 | 1504 | +                return host, port  | 
 | 1505 | +        return None, None  | 
1515 | 1506 | 
 
  | 
1516 | 1507 |     def test_http_client(self):  | 
1517 |  | -        _, (bind, port) = server._get_best_family(None, find_unused_port())  | 
1518 |  | -        proc = spawn_python('-u', '-m', 'http.server', str(port), '-b', bind,  | 
1519 |  | -                            bufsize=1, text=True)  | 
 | 1508 | +        proc = spawn_python('-u', '-m', 'http.server', text=True)  | 
1520 | 1509 |         self.addCleanup(kill_python, proc)  | 
1521 | 1510 |         self.addCleanup(proc.terminate)  | 
1522 |  | -        self.assertTrue(self.wait_for_server(proc, 'http', port, bind))  | 
 | 1511 | +        bind, port = self.wait_for_server(proc, 'http')  | 
 | 1512 | +        self.assertIsNotNone(bind)  | 
 | 1513 | +        self.assertIsNotNone(port)  | 
1523 | 1514 |         res = self.fetch_file(f'http://{bind}:{port}/{self.served_file_name}')  | 
1524 | 1515 |         self.assertEqual(res, self.served_data)  | 
1525 | 1516 | 
 
  | 
1526 | 1517 |     def test_https_client(self):  | 
1527 |  | -        _, (bind, port) = server._get_best_family(None, find_unused_port())  | 
1528 |  | -        proc = spawn_python('-u', '-m', 'http.server', str(port), '-b', bind,  | 
 | 1518 | +        proc = spawn_python('-u', '-m', 'http.server',  | 
1529 | 1519 |                             '--tls-cert', self.tls_cert,  | 
1530 | 1520 |                             '--tls-key', self.tls_key,  | 
1531 | 1521 |                             '--tls-password-file', self.tls_password_file,  | 
1532 |  | -                            bufsize=1, text=True)  | 
 | 1522 | +                            text=True)  | 
1533 | 1523 |         self.addCleanup(kill_python, proc)  | 
1534 | 1524 |         self.addCleanup(proc.terminate)  | 
1535 |  | -        self.assertTrue(self.wait_for_server(proc, 'https', port, bind))  | 
 | 1525 | +        bind, port = self.wait_for_server(proc, 'https')  | 
 | 1526 | +        self.assertIsNotNone(bind)  | 
 | 1527 | +        self.assertIsNotNone(port)  | 
1536 | 1528 |         url = f'https://{bind}:{port}/{self.served_file_name}'  | 
1537 | 1529 |         res = self.fetch_file(url, context=self.get_ssl_context())  | 
1538 | 1530 |         self.assertEqual(res, self.served_data)  | 
 | 
0 commit comments