From 9f3804d5e4f28ea61a8abc856210422ad794b55e Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Tue, 31 Mar 2020 11:39:49 -0700 Subject: Add Run.Que.Website server This is a simple website server that uses que.run itself to host the que webpages. I had to rename Run.Que to Run.Que.Server because nix was complaining about Run.Que being both a derivation and an attrset with Run.Que.Website in it. --- Run/Que/client.py | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100755 Run/Que/client.py (limited to 'Run/Que/client.py') diff --git a/Run/Que/client.py b/Run/Que/client.py new file mode 100755 index 0000000..8058a05 --- /dev/null +++ b/Run/Que/client.py @@ -0,0 +1,95 @@ +#!/usr/bin/env python3 +""" +simple client for que.run +""" + +import argparse +import urllib.request as request +import urllib.parse +import time +import subprocess +import sys + +# set to something ridiculously high so we don't run into timeouts while polling +# or waiting for a message +MAX_TIMEOUT = 100000000 + + +def main(argv=None): + cli = argparse.ArgumentParser(description=__doc__) + cli.add_argument( + "--host", default="https://que.run", help="where que-server is running" + ) + cli.add_argument( + "--poll", default=False, action="store_true", help="stream data from the que" + ) + cli.add_argument( + "--then", + help="when polling, run this shell command after each response, replacing '\que' with the target and '\msg' with the body of the response", + ) + cli.add_argument( + "--serve", + default=False, + action="store_true", + help="when posting to the que, do so continuously in a loop. this can be used for serving a webpage or other file continuously", + ) + cli.add_argument( + "target", help="namespace and path of the que, like 'ns/path/subpath'" + ) + cli.add_argument( + "infile", + nargs="?", + type=argparse.FileType("r"), + help="data to put on the que. Use '-' for stdin, otherwise should be a readable file", + ) + + if argv is None: + args = cli.parse_args() + else: + args = cli.parse_args(argv) + + if args.infile: + # send input data + data = args.infile.read().encode("utf-8").strip() + if args.serve: + # loop until ^C + while not time.sleep(1): + with request.urlopen( + f"{args.host}/{args.target}", data=data, timeout=MAX_TIMEOUT + ) as req: + pass + else: + with request.urlopen( + f"{args.host}/{args.target}", data=data, timeout=MAX_TIMEOUT + ) as req: + pass + else: + # no input data, do a read instead + params = urllib.parse.urlencode({"poll": args.poll}) + url = f"{args.host}/{args.target}?{params}" + with request.urlopen(url) as req: + if args.poll: + while not time.sleep(1): + msg = req.readline().decode("utf-8").strip() + print(msg) + if args.then: + subprocess.run( + args.then.replace("\msg", msg).replace("\que", args.target), + shell=True, + ) + else: + msg = req.read().decode("utf-8").strip() + print(msg) + if args.then: + subprocess.run( + args.then.replace("\msg", msg).replace("\que", args.target), + shell=True, + ) + + +if __name__ == "__main__": + try: + main() + except KeyboardInterrupt: + print("Interrupted") + sys.exit(0) -- cgit v1.2.3