Tidy up client code

This commit is contained in:
Michael Lazar 2020-05-19 01:14:44 -04:00
parent d66e11c9ce
commit ee9efd3f5f
1 changed files with 15 additions and 21 deletions

View File

@ -1,8 +1,6 @@
#!/usr/bin/env python3
"""
A dead-simple gemini client intended to be used for server development and testing.
./jetforce-client gemini://mozz.us
A very basic gemini client to use for testing server configurations.
"""
import argparse
import socket
@ -22,11 +20,10 @@ def fetch(url, host=None, port=None, use_sni=False):
host = host or parsed_url.hostname
port = port or parsed_url.port or 1965
server_hostname = host if use_sni else None
sni = host if use_sni else None
with socket.create_connection((host, port)) as sock:
with context.wrap_socket(sock, server_hostname=server_hostname) as ssock:
with context.wrap_socket(sock, server_hostname=sni) as ssock:
ssock.sendall((url + "\r\n").encode())
fp = ssock.makefile("rb", buffering=0)
data = fp.read(1024)
@ -38,26 +35,23 @@ def fetch(url, host=None, port=None, use_sni=False):
def run_client():
parser = argparse.ArgumentParser(description="A simple gemini client")
parser.add_argument("url")
parser.add_argument("--host", help="Server host")
parser.add_argument("--port", help="Server port")
parser.add_argument("--tls-certfile", help="Client certificate")
parser.add_argument("--tls-keyfile", help="Client private key")
parser.add_argument("--tls-alpn-protocol", help="Protocol for ALPN negotiation")
parser.add_argument(
"--host", help="Optional server to connect to, will default to the URL"
)
parser.add_argument(
"--port", help="Optional port to connect to, will default to the URL"
)
parser.add_argument("--certfile", help="Optional client certificate")
parser.add_argument("--keyfile", help="Optional client key")
parser.add_argument("--alpn-protocol", help="Indicate the protocol using ALPN")
parser.add_argument(
"--use-sni", action="store_true", help="Specify the server hostname via SNI"
"--tls-enable-sni", action="store_true", help="Specify the hostname using SNI"
)
args = parser.parse_args()
if args.certfile:
context.load_cert_chain(args.certfile, args.keyfile)
if args.alpn_protocol:
context.set_alpn_protocols([args.alpn_protocol])
if args.tls_certfile:
context.load_cert_chain(args.tls_certfile, args.tls_keyfile)
fetch(args.url, args.host, args.port, args.use_sni)
if args.tls_alpn_protocol:
context.set_alpn_protocols([args.tls_alpn_protocol])
fetch(args.url, args.host, args.port, args.tls_enable_sni)
if __name__ == "__main__":