From 0696008af8a5348f586f19fa802a2bd05ade3d9a Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Wed, 4 Dec 2024 18:28:59 -0500 Subject: [PATCH] Initial implementation of nntp.tiny.client --- lib/nntp/tiny/client.py | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 lib/nntp/tiny/client.py diff --git a/lib/nntp/tiny/client.py b/lib/nntp/tiny/client.py new file mode 100644 index 0000000..d164dd7 --- /dev/null +++ b/lib/nntp/tiny/client.py @@ -0,0 +1,46 @@ +import re +import socket +import ssl + +from nntp.tiny.socket import Connection +from nntp.tiny.host import Host +from nntp.tiny.response import Response, ResponseCode + +class Client(Connection): + def __init__(self, host: str, port: int, tls: bool=False): + sock = socket.create_connection((host, port)) + + if tls: + sslctx = ssl.create_default_context() + sslctx.check_hostname = False + sslctx.verify_mode = ssl.CERT_NONE + + if Host.is_hostname(host): + sock = sslctx.wrap_socket(sock, server_hostname=host) + else: + sock = sslctx.wrap_socket(sock) + + self.host = host + self.port = port + self.tls = tls + + super().__init__(sock) + + RE_SPLIT = re.compile(r'\s+') + + def request(self, *args): + self.print(' '.join(args)) + + line = self.readline() + + if line == '': + return + + parts = self.RE_SPLIT.split(line.rstrip(), 1) + + if len(parts) == 0: + return + elif len(parts) == 1: + return Response(ResponseCode(int(parts[0]))) + else: + return Response(ResponseCode(int(parts[0])), parts[1])