summary refs log tree commit diff
path: root/protocol
diff options
context:
space:
mode:
authorZach DeCook <zachdecook@librem.one>2022-12-23 19:08:28 -0500
committerZach DeCook <zachdecook@librem.one>2022-12-23 19:08:28 -0500
commit259946e39ff7382a84a432a300c92d0f3744f12d (patch)
treef194ce47ec65ff05afdd75f7266eaea26ecac137 /protocol
parentf62aa8b56139a758dbe1cd7b093d980c260cea18 (diff)
downloadbrowset-259946e39ff7382a84a432a300c92d0f3744f12d.tar.gz
Browsing: Actually browse gemini!
Diffstat (limited to 'protocol')
-rw-r--r--protocol/gemini.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/protocol/gemini.py b/protocol/gemini.py
new file mode 100644
index 0000000..c55e523
--- /dev/null
+++ b/protocol/gemini.py
@@ -0,0 +1,29 @@
+import socket
+import ssl
+
+class GeminiProtocol():
+    def get(url):
+        hostname = _gethostname(url)
+        try:
+            s = socket.create_connection((hostname, 1965),timeout=2)
+            context = ssl.SSLContext()
+            context.check_hostname = False
+            context.verify_mode = ssl.CERT_NONE
+            s = context.wrap_socket(s, server_hostname = hostname)
+            s.sendall((url + '\r\n').encode("UTF-8"))
+            fp = s.makefile("rb")
+        except:
+            return ("error",["error"])
+        header = fp.readline()
+        header = header.decode("UTF-8").strip()
+        if header[0:1] == "2":
+            return (header[3:], fp)
+        return ("error", [header])
+
+def _gethostname(url):
+    return url.split('/')[2]
+
+if __name__ == "__main__":
+    (mime, fp) = GeminiProtocol.get("gemini://gemini.zachdecook.com/")
+    print(mime)
+    print(fp)