add perimitive gopher parser

This commit is contained in:
randomuser 2021-07-19 16:17:45 -05:00
parent 44213df44a
commit b6de76f44d
1 changed files with 44 additions and 0 deletions

44
esgd.py
View File

@ -5,6 +5,50 @@ import os
class GopherError(BaseException): pass class GopherError(BaseException): pass
class RequestError(GopherError): 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): def recieveRequest(context):
data = b"" data = b""
while data[-2:] != b"\r\n": while data[-2:] != b"\r\n":