Commit Diff


commit - 0982bc0632b5463a07be9a492c7dfa970a1b9071
commit + 383d16eab70db42770d846c360c60d1130c3feab
blob - 8bb808f91a6f222e4d8c54eeb08d57b02493d8bd
blob + cddaa204cb2866389111cfcc98b89caee1a30525
--- lonk.py
+++ lonk.py
@@ -1,11 +1,15 @@
 from json import loads as json_loads
 from pathlib import Path
+from socket import socket
+from ssl import PROTOCOL_TLS_SERVER, SSLContext
 from urllib.parse import urlencode, urlunsplit, urljoin, urlsplit
 from urllib.request import urlopen
 from html.parser import HTMLParser
 
 
-_SETTINGS = json_loads((Path(__file__).parent / ".local" / "settings.json").read_text(encoding="utf8"))
+_SETTINGS = json_loads(
+    (Path(__file__).parent / ".local" / "settings.json").read_text(encoding="utf8")
+)
 assert set(_SETTINGS.keys()) == {"token", "server"}
 
 
@@ -30,6 +34,7 @@ def _load_from(action, page, c=None):
     with urlopen(_build_url(path="api", query=urlencode(query)), timeout=15) as f:
         return json_loads(f.read().decode("utf8"))
 
+
 class _BaseTag:
     def __init__(self, tag, attrs):
         self.tag = tag
@@ -191,7 +196,7 @@ def _format_honk(honk):
     return "\n".join(lines)
 
 
-if __name__ == '__main__':
+def demo():
     collected = {}
 
     home = _load_from(action="gethonks", page="home")
@@ -221,3 +226,25 @@ if __name__ == '__main__':
     for honk in reversed(collected.values()):
         print("\n")
         print(_format_honk(honk))
+
+
+def server():
+    from argparse import ArgumentParser
+    parser.add_argument("--settings", required=True, help="path to server settings (JSON file)")
+    parser.add_argument("--certfile", required=True, help="path server certificate file")
+    parser.add_argument("--keyfile", required=True, help="path certificate private key file")
+    args = parser.parse_args()
+
+    ssl_ctx = SSLContext(PROTOCOL_TLS_SERVER)
+    ssl_ctx.load_cert_chain(args.certfile, args.keyfile)
+
+    with socket() as sock:
+        sock.bind(("127.0.0.1", 1998))
+        sock.listen()
+        with ssl_ctx.wrap_socket(sock, server_side=True) as ssl_sock:
+            conn, addr = ssl_sock.accept()
+
+
+if __name__ == '__main__':
+    demo()
+    # server()