simplify the file sending code so the canonical implimentation is sendFileArray

This commit is contained in:
randomuser 2021-07-19 15:20:26 -05:00
parent f7b79fdc1b
commit bd50cd7e1e

20
esgd.py
View File

@ -48,10 +48,13 @@ def invalid(context):
def sendFile(file, context): def sendFile(file, context):
with open(file, "r") as fd: with open(file, "r") as fd:
[context.request.sendall( sendFileArray(fd.readlines(), context)
(i.rstrip() + "\r\n").encode("utf-8")
) for i in fd.readlines()] def sendFileArray(arr, context):
context.request.sendall(b".\r\n") [context.request.sendall(
(i.rstrip() + "\r\n").encode("utf-8")
) for i in arr]
context.request.sendall(b".\r\n")
def cgi(file, query, context): def cgi(file, query, context):
env = {} env = {}
@ -81,22 +84,17 @@ class gopherHandler(socketserver.BaseRequestHandler):
invalid(self) invalid(self)
return return
print(self.client_address)
try: file = returnRelative(parsed[0]) try: file = returnRelative(parsed[0])
except RequestError: except RequestError:
notFound(self) notFound(self)
return return
if fileCGI(file[0]): if fileCGI(file[0]):
ret = cgi(file[0], parsed[1], self) sendFileArray(cgi(file[0], parsed[1], self), self)
for i in ret:
self.request.sendall((i + '\r\n').encode('utf-8'))
self.request.sendall(b'.\r\n')
elif fileSendable(file[0]): sendFile(file[0], self) elif fileSendable(file[0]): sendFile(file[0], self)
else: notFound(self) else: notFound(self)
if __name__ == "__main__": if __name__ == "__main__":
HOST, PORT = "localhost", 72 HOST, PORT = "localhost", 75
with socketserver.TCPServer((HOST, PORT), gopherHandler) as server: with socketserver.TCPServer((HOST, PORT), gopherHandler) as server:
server.serve_forever() server.serve_forever()