Skip to content

Instantly share code, notes, and snippets.

@MineRobber9000
Created June 15, 2021 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MineRobber9000/7af3c82c5fb6bd88f349b3372a36298d to your computer and use it in GitHub Desktop.
Save MineRobber9000/7af3c82c5fb6bd88f349b3372a36298d to your computer and use it in GitHub Desktop.
SRV records in Gemini example spartan client

What is this?

A gemini client that supports SRV records, as stated in gemini://tilde.team/~khuxkm/gemlog/srv-records-gemini.gmi.

How do I test it out?

First, install the srvres library, via pip, Poetry, or whatever Python package manager you use.

Supply the URL as an argument, like so:

python3 spartansrv.py gemini://tilde.team/

Note how for domains without the SRV record, it will still connect as usual.

Now, try using the client to request gemini://khuxkmtest.mywire.org/:

python3 spartansrv.py gemini://khuxkmtest.mywire.org/

See how it works? If you try this in a client that doesn't support SRV records, it will fail.

import ssl, socket, sys, srvres
from urllib.parse import urlparse
#URL = "gemini://khuxkmtest.mywire.org/"
_, URL = sys.argv
PARSED = urlparse(URL)
ctx=ssl.create_default_context()
ctx.check_hostname=False
ctx.verify_mode=ssl.CERT_NONE
def connect(url,hostname,host,port):
with socket.socket(socket.AF_INET,socket.SOCK_STREAM) as s:
s.settimeout(5)
ss = ctx.wrap_socket(s,server_hostname=hostname)
try:
ss.connect((host,port))
except socket.timeout:
return False
except ConnectionRefusedError:
return False
ss.send((URL+"\r\n").encode("utf-8"))
out = b""
while (data:=ss.recv(1024)):
out+=data
return out
for host, port in srvres.SRVResolver("gemini",PARSED.hostname,port=(PARSED.port or 1965)):
print(host,port)
out = connect(URL,PARSED.hostname,host,port)
if out:
sys.stdout.buffer.write(out)
sys.exit(0)
print(f"ERROR: unable to resolve {PARSED.hostname}")
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment