Skip to content

Commit 7123976

Browse files
authored
now it should work
1 parent 4dfcfe3 commit 7123976

File tree

1 file changed

+18
-29
lines changed

1 file changed

+18
-29
lines changed

Lib/test/test_httpservers.py

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
is_apple, import_helper, os_helper, threading_helper
3636
)
3737
from test.support.script_helper import kill_python, spawn_python
38-
from test.support.socket_helper import find_unused_port
3938

4039
try:
4140
import ssl
@@ -1491,48 +1490,38 @@ def parse_cli_output(self, output):
14911490
return None, None, None
14921491
return matches.group(1), matches.group(2), int(matches.group(3))
14931492

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
15151503

15161504
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)
15201506
self.addCleanup(kill_python, proc)
15211507
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)
15231511
res = self.fetch_file(f'http://{bind}:{port}/{self.served_file_name}')
15241512
self.assertEqual(res, self.served_data)
15251513

15261514
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',
15291516
'--tls-cert', self.tls_cert,
15301517
'--tls-key', self.tls_key,
15311518
'--tls-password-file', self.tls_password_file,
15321519
bufsize=1, text=True)
15331520
self.addCleanup(kill_python, proc)
15341521
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)
15361525
url = f'https://{bind}:{port}/{self.served_file_name}'
15371526
res = self.fetch_file(url, context=self.get_ssl_context())
15381527
self.assertEqual(res, self.served_data)

0 commit comments

Comments
 (0)