Beispiel #1
0
def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("-a", "--address", dest="address", type=valid_address,
                        help="The port or host:port pair at which run server")
    parser.add_argument("-r", "--root-directory", type=is_dir, default=".",
                        help="The root directory. Defaults to current.")
    args = parser.parse_args(argv)

    if args.address:
        address = parse_address(args.address)
    else:
        address = ("", 3000)
    root = os.path.abspath(args.root_directory)

    def _spawn_http_server():
        http_child = os.fork()
        if http_child == 0:
            run(root, address)
            os._exit(0)
        else:
            return http_child

    http_child = _spawn_http_server()
    ch_read, ch_write = os.pipe()
    watch_child = os.fork()
    if watch_child == 0:
        os.close(ch_read)
        watch(root, channel=ch_write)
        os._exit(0)

    def kill_children(signum, frame):
        os.kill(http_child, signal.SIGTERM)
        os.kill(watch_child, signal.SIGTERM)
        print("Exiting...")
        sys.exit()
    signal.signal(signal.SIGINT, kill_children)

    os.close(ch_write)
    channel = os.fdopen(ch_read)
    while True:
        command = channel.readline()
        if "reload" in command:
            print("Change detected, reloading http server...")
            os.kill(http_child, signal.SIGTERM)
            try:
                os.wait()
            except OSError:
                pass
            http_child = _spawn_http_server()
        time.sleep(1)
Beispiel #2
0
 def test_host_port(self):
     addr = "localhost:3000"
     expected = ("localhost", 3000)
     self.assertEqual(expected, utils.parse_address(addr))
Beispiel #3
0
 def test_port(self):
     addr = "3000"
     expected = ("", 3000)
     self.assertEqual(expected, utils.parse_address(addr))