File size: 1,569 Bytes
0914710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import logging
import time
import unittest


class LocalTilesHttpServer(unittest.TestCase):
    from contextlib import contextmanager

    @staticmethod
    @contextmanager
    def http_server(host: str, port: int, directory: str):
        """Function http_server defined within this test class to avoid pytest error "fixture 'host' not found"."""
        from functools import partial
        from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
        from threading import Thread

        server = ThreadingHTTPServer(
            (host, port), partial(SimpleHTTPRequestHandler, directory=directory)
        )
        print("dir:", directory, "#")
        server_thread = Thread(target=server.serve_forever, name="http_server")
        server_thread.start()
        logging.info(f"listen:: host {host}, port {port}.")

        try:
            yield
        finally:
            server.shutdown()
            server_thread.join()


if __name__ == '__main__':
    # from tests import TEST_ROOT_FOLDER
    from pathlib import Path

    PROJECT_ROOT_FOLDER = Path(globals().get("__file__", "./_")).absolute().parent.parent

    TEST_ROOT_FOLDER = PROJECT_ROOT_FOLDER / "tests"
    TEST_EVENTS_FOLDER = TEST_ROOT_FOLDER / "events"

    main_listen_port = 8000
    logging.info(f"http_basedir_serve: {TEST_ROOT_FOLDER}.")
    with LocalTilesHttpServer.http_server("localhost", main_listen_port, directory=str(TEST_ROOT_FOLDER)):
        time.sleep(1000)
        logging.info("""import time; time.sleep(10)""")
    # logging.info("Http server stopped.")