@@ -1478,31 +1478,26 @@ def fetch_file(self, path, context=None):
1478
1478
return res .read ()
1479
1479
1480
1480
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 :
1483
1483
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 ))
1485
1485
1486
- def wait_for_server (self ,proc ,protocol ,port ):
1487
- """Extract the serverbind 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."""
1488
1488
line = proc .stdout .readline ().strip ()
1489
1489
if support .verbose :
1490
1490
print ()
1491
1491
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 )
1496
1493
1497
1494
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 ,
1500
1497
bufsize = 1 ,text = True )
1501
1498
self .addCleanup (kill_python ,proc )
1502
1499
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 ))
1506
1501
res = self .fetch_file (f'http://{ bind } :{ port } /{ self .served_filename } ' )
1507
1502
self .assertEqual (res ,self .served_data )
1508
1503
@@ -1514,16 +1509,14 @@ def test_https_client(self):
1514
1509
context .verify_mode = ssl .CERT_NONE
1515
1510
1516
1511
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 ,
1519
1513
'--tls-cert' ,self .tls_cert ,
1520
1514
'--tls-key' ,self .tls_key ,
1521
1515
'--tls-password-file' ,self .tls_password_file ,
1522
1516
bufsize = 1 ,text = True )
1523
1517
self .addCleanup (kill_python ,proc )
1524
1518
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 ))
1527
1520
url = f'https://{ bind } :{ port } /{ self .served_filename } '
1528
1521
res = self .fetch_file (url ,context = context )
1529
1522
self .assertEqual (res ,self .served_data )