#!/usr/bin/env python3 """shrlok server: receive text/files from a socket, put them on a Web server. The server expects messages with the following format to come through the socket: 1. a length, as ASCII digits, 2. a null byte, 3. a JSON object containing at least the "type" key with a known value, 4. another null byte, 5. the text or file itself. The length is of the JSON object + the null char + the content itself. Example, with \\0 representing a null byte: 28\\0{"type":"txt"}\\0hello shrlok! The content is 13 bytes (assuming no LF at the end), the header is 14 bytes, plus the null byte it is 13 + 14 + 1 = 28 bytes, thus the prefixed length. After the content is succesfully retrieved and put on an appropriate location, the server will reply the file path through the socket and close the connection. """ import argparse import json import os import socketserver import tempfile from pathlib import Path ARGS = None HTML_TEMPLATE = """\
{}".format(data.decode(errors="replace")) html = HTML_TEMPLATE.format(title=title or "?", content=content).encode() write_content(html) def write_content(data: bytes): path = str(Path(ARGS.root) / "txt") if not os.path.isdir(path): print(f"Missing '{path}' folder.") return None with tempfile.NamedTemporaryFile( mode="wb", dir=path, suffix=".html", delete=False ) as output_file: output_file.write(data) os.chmod(output_file.name, 0o644) return output_file.name def main(): argp = argparse.ArgumentParser(description="Share stuff through a socket.") argp.add_argument("root", help="root path where to put files") argp.add_argument("-s", "--socket", help="socket path") global ARGS ARGS = argp.parse_args() socket_path = ARGS.socket if not socket_path: runtime_dir = os.environ.get("RUNTIME_DIRECTORY") if not runtime_dir: exit("No socket path nor runtime directory specified.") socket_path = os.path.join(runtime_dir, "shr.sock") print("Socket path:", socket_path) if os.path.exists(socket_path): os.unlink(socket_path) try: with socketserver.UnixStreamServer(socket_path, Handler) as server: os.chmod(socket_path, 0o664) server.serve_forever() except KeyboardInterrupt: print("Stopping server.") finally: os.unlink(socket_path) if __name__ == "__main__": main()