From 59f8fac26f0ae67810268241ca8d03664ba7c31e Mon Sep 17 00:00:00 2001 From: Juhani Haverinen Date: Tue, 2 Aug 2016 21:30:34 +0300 Subject: [PATCH] Add preliminary support for opening files --- neomi.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/neomi.py b/neomi.py index 49a9778..c97a6a3 100644 --- a/neomi.py +++ b/neomi.py @@ -521,14 +521,21 @@ class Serve(threading.Thread): try: full_path = get_full_path(path, config = self.config) mimetype = get_mimetype(full_path, config = self.config) + file = open(str(full_path), 'rb') + except FileNotFoundError: reader = StringReader('%s not found\n' % path) send_header(self.sock, protocol, Status.notfound, 'text/plain', config = self.config) send_file(self.sock, reader, protocol, 'text/plain', config = self.config) + else: - reader = StringReader('Path: %s\nProtocol: %s\nRest of header data: %s\nMime type: %s\n' % (path, protocol, rest, mimetype)) - send_header(self.sock, protocol, Status.ok, 'text/plain', config = self.config) - send_file(self.sock, reader, protocol, 'text/plain', config = self.config) + reader = FileReader(file) + + send_header(self.sock, protocol, Status.ok, mimetype, config = self.config) + send_file(self.sock, reader, protocol, mimetype, config = self.config) + + file.close() + except BaseException as err: reader = StringReader('Internal server error\n') send_header(self.sock, protocol, Status.error, 'text/plain', config = self.config)