1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
import socket
import ssl
class GeminiProtocol():
def relativeURL(path, prevurl):
if path.startswith("/"):
url = '/'.join(prevurl.split("/")[0:3])+path
else:
base = prevurl
if base.count('/') >= 3:
base = '/'.join(prevurl.split('/')[0:-1])
while path.startswith("../"):
base = '/'.join(base.split('/')[0:-1])
path = path.replace("../","",1)
url = base + '/' + path
return url
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 ("text/error",["error"])
header = fp.readline()
header = header.decode("UTF-8").strip()
if header[0:1] == "2":
return (header[3:], fp)
return ("text/error", [header])
def _gethostname(url):
return url.split('/')[2]
if __name__ == "__main__":
(mime, fp) = GeminiProtocol.get("gemini://gemini.zachdecook.com/")
print(mime)
print(fp)
|