3535 is_apple , import_helper , os_helper , threading_helper
3636)
3737from test .support .script_helper import kill_python , spawn_python
38+ from test .support .socket_helper import find_unused_port
3839
3940try :
4041 import ssl
@@ -1490,7 +1491,7 @@ def parse_cli_output(self, output):
14901491 return None , None , None
14911492 return matches .group (1 ), matches .group (2 ), int (matches .group (3 ))
14921493
1493- def wait_for_server (self , proc , protocol ):
1494+ def wait_for_server (self , proc , protocol , port ):
14941495 """Check the server process output."""
14951496 for _ in range (10 ):
14961497 line = proc .stdout .readline ()
@@ -1499,32 +1500,32 @@ def wait_for_server(self, proc, protocol):
14991500 continue
15001501 if support .verbose > 1 :
15011502 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
1503+ parsed_protocol , host , parsed_port = self .parse_cli_output (line )
1504+ if protocol == parsed_protocol and parsed_port == port :
1505+ return host
1506+ return None
15061507
15071508 def test_http_client (self ):
1508- proc = spawn_python ('-u' , '-m' , 'http.server' , text = True )
1509+ port = find_unused_port ()
1510+ proc = spawn_python ('-u' , '-m' , 'http.server' , str (port ), text = True )
15091511 self .addCleanup (kill_python , proc )
15101512 self .addCleanup (proc .terminate )
1511- bind , port = self .wait_for_server (proc , 'http' )
1513+ bind = self .wait_for_server (proc , 'http' , port )
15121514 self .assertIsNotNone (bind )
1513- self .assertIsNotNone (port )
15141515 res = self .fetch_file (f'http://{ bind } :{ port } /{ self .served_file_name } ' )
15151516 self .assertEqual (res , self .served_data )
15161517
15171518 def test_https_client (self ):
1518- proc = spawn_python ('-u' , '-m' , 'http.server' ,
1519+ port = find_unused_port ()
1520+ proc = spawn_python ('-u' , '-m' , 'http.server' , str (port ),
15191521 '--tls-cert' , self .tls_cert ,
15201522 '--tls-key' , self .tls_key ,
15211523 '--tls-password-file' , self .tls_password_file ,
15221524 text = True )
15231525 self .addCleanup (kill_python , proc )
15241526 self .addCleanup (proc .terminate )
1525- bind , port = self .wait_for_server (proc , 'https' )
1527+ bind = self .wait_for_server (proc , 'https' , port )
15261528 self .assertIsNotNone (bind )
1527- self .assertIsNotNone (port )
15281529 url = f'https://{ bind } :{ port } /{ self .served_file_name } '
15291530 res = self .fetch_file (url , context = self .get_ssl_context ())
15301531 self .assertEqual (res , self .served_data )
0 commit comments