diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-07-19 10:32:08 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-07-19 10:32:08 +0000 |
commit | f06b0bccfdb7459be0947a6fd6f726eba4bcf92d (patch) | |
tree | 7fafc5f2d22eaf68c9dcfea5a74fe03d6b0676c8 /ewww.py | |
parent | f1590591451f135ca903e20f24ca5a9d0e7fba14 (diff) | |
parent | b130e74f187b2ba22db6654508a6e7e4c721a16e (diff) | |
download | ewww-f06b0bccfdb7459be0947a6fd6f726eba4bcf92d.tar.gz |
Merge branch 'serve-files' into 'master'
Serve files
See merge request larswirzenius/ewww!7
Diffstat (limited to 'ewww.py')
-rw-r--r-- | ewww.py | 21 |
1 files changed, 20 insertions, 1 deletions
@@ -8,6 +8,7 @@ import random import re import shutil import signal +import socket import subprocess import time import urllib.parse @@ -82,7 +83,7 @@ def start_server(ctx, filename=None): logging.debug(f"Starting ewww with config file {filename}") config = get_file(filename).decode("UTF-8") config = yaml.safe_load(config) - config["port"] = random.randint(2000, 30000) + port = config["port"] = random.randint(2000, 30000) logging.debug(f"Picked randomly port for ewww: {config['port']}") ctx["config"] = config config = yaml.safe_dump(config) @@ -90,6 +91,24 @@ def start_server(ctx, filename=None): start_daemon(ctx, "ewww", [_binary("ewww"), filename]) + if not port_open("localhost", port, 5.0): + stderr = open(ctx["daemon"]["ewww"]["stderr"]).read() + logging.debug(f"Stderr from daemon: {stderr!r}") + + +# Wait for a port to be open +def port_open(host, port, timeout): + logging.debug(f"Waiting for port localhost:{port} to be available") + started = time.time() + while time.time() < started + timeout: + try: + socket.create_connection((host, port), timeout=timeout) + return True + except socket.error: + pass + logging.error(f"Port localhost:{port} is not open") + return False + # Stop previously started server. def stop_server(ctx): |