simplify the file sending code so the canonical implimentation is sendFileArray
This commit is contained in:
parent
f7b79fdc1b
commit
bd50cd7e1e
20
esgd.py
20
esgd.py
|
@ -48,10 +48,13 @@ def invalid(context):
|
|||
|
||||
def sendFile(file, context):
|
||||
with open(file, "r") as fd:
|
||||
[context.request.sendall(
|
||||
(i.rstrip() + "\r\n").encode("utf-8")
|
||||
) for i in fd.readlines()]
|
||||
context.request.sendall(b".\r\n")
|
||||
sendFileArray(fd.readlines(), context)
|
||||
|
||||
def sendFileArray(arr, context):
|
||||
[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):
|
||||
env = {}
|
||||
|
@ -81,22 +84,17 @@ class gopherHandler(socketserver.BaseRequestHandler):
|
|||
invalid(self)
|
||||
return
|
||||
|
||||
print(self.client_address)
|
||||
|
||||
try: file = returnRelative(parsed[0])
|
||||
except RequestError:
|
||||
notFound(self)
|
||||
return
|
||||
if fileCGI(file[0]):
|
||||
ret = cgi(file[0], parsed[1], self)
|
||||
for i in ret:
|
||||
self.request.sendall((i + '\r\n').encode('utf-8'))
|
||||
self.request.sendall(b'.\r\n')
|
||||
sendFileArray(cgi(file[0], parsed[1], self), self)
|
||||
elif fileSendable(file[0]): sendFile(file[0], self)
|
||||
else: notFound(self)
|
||||
|
||||
if __name__ == "__main__":
|
||||
HOST, PORT = "localhost", 72
|
||||
HOST, PORT = "localhost", 75
|
||||
|
||||
with socketserver.TCPServer((HOST, PORT), gopherHandler) as server:
|
||||
server.serve_forever()
|
||||
|
|
Loading…
Reference in New Issue