From 24b15765386e2a97a6d8cbc3c20708cea4af2f98 Mon Sep 17 00:00:00 2001 From: Chandok Date: Mon, 14 Oct 2019 16:15:42 -0700 Subject: [PATCH] Committed --- http_server.py | 82 ++++++++++++++++++++++++++------------------------ tests.py | 3 +- unit-tests.py | 2 +- 3 files changed, 45 insertions(+), 42 deletions(-) diff --git a/http_server.py b/http_server.py index 58d7386..b8a4a53 100644 --- a/http_server.py +++ b/http_server.py @@ -1,6 +1,8 @@ import socket import sys import traceback +import mimetypes +import os def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"): """ @@ -10,7 +12,6 @@ def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"): b"

Welcome:

", b"text/html" ) -> - b''' HTTP/1.1 200 OK\r\n Content-Type: text/html\r\n @@ -20,64 +21,59 @@ def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"): """ # TODO: Implement response_ok - return b"" + posting = b"HTTP/1.1 200 OK\r\nContent-Type: " + posting += mimetype + posting += b"\r\n\r\n" + posting += body + return posting def response_method_not_allowed(): """Returns a 405 Method Not Allowed response""" - - # TODO: Implement response_method_not_allowed - return b"" - + # TODO: Implement response_method_not_allowed + posting = b"HTTP/1.1 405 Method Not Allowed" + b"\n Server Error" + return posting def response_not_found(): """Returns a 404 Not Found response""" - - # TODO: Implement response_not_found - return b"" - + # TODO: Implement response_not_found + posting = b"HTTP/1.1 404 Not Found" + b"\n Data Error" + return posting def parse_request(request): """ Given the content of an HTTP request, returns the path of that request. - This server only handles GET requests, so this method shall raise a NotImplementedError if the method of the request is not GET. """ - - # TODO: implement parse_request - return "" + # TODO: implement parse_request + method, path, ver = request.split('\r\n')[0].split(' ') + if method != 'GET': + raise NotImplementedError + return path def response_path(path): """ This method should return appropriate content and a mime type. - If the requested path is a directory, then the content should be a plain-text listing of the contents with mimetype `text/plain`. - If the path is a file, it should return the contents of that file and its correct mimetype. - If the path does not map to a real location, it should raise an exception that the server can catch to return a 404 response. - Ex: response_path('/a_web_page.html') -> (b"

North Carolina...", b"text/html") - response_path('/images/sample_1.png') -> (b"A12BCF...", # contents of sample_1.png b"image/png") - response_path('/') -> (b"images/, a_web_page.html, make_type.py,...", b"text/plain") - response_path('/a_page_that_doesnt_exist.html') -> Raises a NameError - """ # TODO: Raise a NameError if the requested content is not present # under webroot. - + # TODO: Fill in the appropriate content and mime_type give the path. # See the assignment guidelines for help on "mapping mime-types", though # you might need to create a special case for handling make_time.py @@ -85,10 +81,20 @@ def response_path(path): # If the path is "make_time.py", then you may OPTIONALLY return the # result of executing `make_time.py`. But you need only return the # CONTENTS of `make_time.py`. - - content = b"not implemented" - mime_type = b"not implemented" - + homedir = "C:/Users/Andy - Desktop/Python230/socket-http-server/webroot" + destination = homedir + path + if os.path.isdir(destination) is True: + content = os.listdir(destination) + content = ''.join(content) + content = content.encode('utf-8') + mime_type = b'' + else: + if os.path.exists(destination) is True: + with open(os.path.join(destination), "rb") as f: + content = f.read() + mime_type = mimetypes.guess_type(destination)[0].encode('utf-8') + else: + raise NameError return content, mime_type @@ -114,29 +120,27 @@ def server(log_buffer=sys.stderr): if '\r\n\r\n' in request: break - print("Request received:\n{}\n\n".format(request)) - # TODO: Use parse_request to retrieve the path from the request. - + path = parse_request(request) # TODO: Use response_path to retrieve the content and the mimetype, # based on the request path. - + content, mime_type = response_path(path) # TODO; If parse_request raised a NotImplementedError, then let # response be a method_not_allowed response. If response_path raised # a NameError, then let response be a not_found response. Else, # use the content and mimetype from response_path to build a # response_ok. - response = response_ok( - body=b"Welcome to my web server", - mimetype=b"text/plain" - ) - - conn.sendall(response) + response = response_ok(content, mime_type) + except NotImplementedError: + response = response_method_not_allowed() + except NameError: + response = response_not_found() except: traceback.print_exc() finally: + conn.sendall(response) conn.close() except KeyboardInterrupt: @@ -148,6 +152,4 @@ def server(log_buffer=sys.stderr): if __name__ == '__main__': server() - sys.exit(0) - - + sys.exit(0) \ No newline at end of file diff --git a/tests.py b/tests.py index 21da57e..274c5f4 100644 --- a/tests.py +++ b/tests.py @@ -153,6 +153,7 @@ def test_get_404(self): error_comment = "Error encountered while visiting " + web_path response = self.get_response(web_path) + self.assertEqual(response.getcode(), 404, error_comment) @@ -202,4 +203,4 @@ def test_ok_response_at_root_index(self): if __name__ == '__main__': - unittest.main() + unittest.main() \ No newline at end of file diff --git a/unit-tests.py b/unit-tests.py index a0c657a..36d8e1e 100644 --- a/unit-tests.py +++ b/unit-tests.py @@ -75,4 +75,4 @@ def test_response_path_not_found(self): if __name__ == '__main__': - unittest.main() + unittest.main() \ No newline at end of file