@@ -1259,6 +1259,38 @@ def testGetServBy(self):
12591259 # Find one service that exists, then check all the related interfaces.
12601260 # I've ordered this by protocols that have both a tcp and udp
12611261 # protocol, at least for modern Linuxes.
1262+ def query_available_service (* protocols ):
1263+ services_file = '/etc/services'
1264+ if not os .path .exists (services_file ):
1265+ return None
1266+ services_found = dict ()
1267+ with open (services_file , 'r' ) as f :
1268+ for line in f :
1269+ line = line .strip ()
1270+ if line .startswith ('#' ):
1271+ # Skip comment line.
1272+ continue
1273+ tokens = line .split ()
1274+ if len (tokens ) < 2 :
1275+ continue
1276+ if '/' not in tokens [1 ]:
1277+ continue
1278+ try :
1279+ _ , entry_protocol = tokens [1 ].split ('/' )
1280+ except :
1281+ continue
1282+ entry_name = tokens [0 ]
1283+ if entry_name not in services_found :
1284+ services_found [entry_name ] = [entry_protocol ]
1285+ else :
1286+ services_found [entry_name ].append (entry_protocol )
1287+ for protocol in protocols :
1288+ if protocol not in services_found [entry_name ]:
1289+ break
1290+ else :
1291+ return entry_name
1292+ return None
1293+
12621294 if (
12631295 sys .platform .startswith (
12641296 ('linux' , 'android' , 'freebsd' , 'netbsd' , 'gnukfreebsd' ))
@@ -1276,7 +1308,12 @@ def testGetServBy(self):
12761308 except OSError :
12771309 pass
12781310 else :
1279- raise OSError
1311+ service = query_available_service ('tcp' , 'udp' )
1312+ if service is None :
1313+ service = query_available_service ('tcp' )
1314+ if service is None :
1315+ self .skipTest ('No available service found.' )
1316+ port = socket .getservbyname (service , 'tcp' )
12801317 # Try same call with optional protocol omitted
12811318 # Issue gh-71123: this fails on Android before API level 23.
12821319 if not (support .is_android and platform .android_ver ().api_level < 23 ):
0 commit comments