github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/results-processor/test_util.py (about)

     1  # Copyright 2019 The WPT Dashboard Project. All rights reserved.
     2  # Use of this source code is governed by a BSD-style license that can be
     3  # found in the LICENSE file.
     4  
     5  import random
     6  import subprocess
     7  import time
     8  
     9  import requests
    10  
    11  
    12  def start_server(capture):
    13      # TODO(Hexcles): Find a free port properly.
    14      port = random.randint(10000, 20000)
    15      pipe = subprocess.PIPE if capture else subprocess.DEVNULL
    16      server = subprocess.Popen(
    17          ['python', 'test_server.py', '-p', str(port)],
    18          stdout=pipe, stderr=pipe)
    19      base_url = 'http://127.0.0.1:{}'.format(port)
    20      # Wait until the server is responsive.
    21      for _ in range(100):
    22          time.sleep(0.1)
    23          try:
    24              requests.post(base_url).raise_for_status()
    25          except requests.exceptions.HTTPError:
    26              break
    27          except Exception:
    28              pass
    29      return server, base_url