simplify the file sending code so the canonical implimentation is sendFileArray
This commit is contained in:
parent
f7b79fdc1b
commit
bd50cd7e1e
14
esgd.py
14
esgd.py
|
@ -48,9 +48,12 @@ def invalid(context):
|
||||||
|
|
||||||
def sendFile(file, context):
|
def sendFile(file, context):
|
||||||
with open(file, "r") as fd:
|
with open(file, "r") as fd:
|
||||||
|
sendFileArray(fd.readlines(), context)
|
||||||
|
|
||||||
|
def sendFileArray(arr, context):
|
||||||
[context.request.sendall(
|
[context.request.sendall(
|
||||||
(i.rstrip() + "\r\n").encode("utf-8")
|
(i.rstrip() + "\r\n").encode("utf-8")
|
||||||
) for i in fd.readlines()]
|
) for i in arr]
|
||||||
context.request.sendall(b".\r\n")
|
context.request.sendall(b".\r\n")
|
||||||
|
|
||||||
def cgi(file, query, context):
|
def cgi(file, query, context):
|
||||||
|
@ -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()
|
||||||
|
|
Loading…
Reference in New Issue