From b6de76f44db48849088c41c4050e39fe316500de Mon Sep 17 00:00:00 2001 From: randomuser Date: Mon, 19 Jul 2021 16:17:45 -0500 Subject: [PATCH] add perimitive gopher parser --- esgd.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/esgd.py b/esgd.py index cd4f119..275497c 100644 --- a/esgd.py +++ b/esgd.py @@ -5,6 +5,50 @@ import os class GopherError(BaseException): pass class RequestError(GopherError): pass +class GopherLine: + def __init__(self, line): + self.item = "i" + self.text = "" + self.location = "null" + self.host = "null.host" + self.port = "0" + self.line = line + self.parse() + def parse(self): + split = self.line.rstrip().split('\t') + location = False + print(split) + if len(split) == 1: + self.text = split[0] + return + + try: self.item = split[0][0] + except IndexError: pass + + try: self.text = split[0][1:] + except IndexError: pass + + try: + self.location = split[1] + location = True + except IndexError: pass + + try: self.host = split[2] + except IndexError: + if location == True: self.host = "beepboop.systems" + + try: self.port = split[3] + except IndexError: + if location == True: self.port = "70" + def render(self): + return "{}{}\t{}\t{}\t{}".format( + self.item, + self.text, + self.location, + self.host, + self.port + ) + def recieveRequest(context): data = b"" while data[-2:] != b"\r\n":