From a8ebc6eb3c62541271f393ae190d635c153c3501 Mon Sep 17 00:00:00 2001 From: HABTAMU ASFAW Date: Tue, 23 Apr 2019 21:13:27 -0700 Subject: [PATCH 1/4] get working http_server and passed unittest --- http_server.py | 104 ++++++++++++++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 40 deletions(-) diff --git a/http_server.py b/http_server.py index 58d7386..5d7fbad 100644 --- a/http_server.py +++ b/http_server.py @@ -1,8 +1,12 @@ -import socket +import os import sys +import socket import traceback +import mimetypes +# import requests as req -def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"): + +def response_ok(body, mimetype): """ returns a basic HTTP response Ex: @@ -19,21 +23,33 @@ def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"): ''' """ - # TODO: Implement response_ok - return b"" + return b"\r\n".join([ + b"HTTP/1.1 200 OK", + b"Content-Type: " + mimetype, + b"", + body, + ]) + def response_method_not_allowed(): """Returns a 405 Method Not Allowed response""" - # TODO: Implement response_method_not_allowed - return b"" + return b"\r\n".join([ + b"HTTP/1.1 405 Method Not Allowed", + b"", + b"You can't do that on this server!" + ]) def response_not_found(): - """Returns a 404 Not Found response""" + """Returns a 404 response Not Found """ - # TODO: Implement response_not_found - return b"" + + return b"\r\n".join([ + b"HTTP/1.1 404 Request Not Found response", + b"", + b"You can't do that on this server!" + ]) def parse_request(request): @@ -44,8 +60,12 @@ def parse_request(request): NotImplementedError if the method of the request is not GET. """ - # TODO: implement parse_request - return "" + method, path, version = request.split("\r\n")[0].split(" ") + if method != "GET": + raise NotImplementedError + + return path + def response_path(path): """ @@ -75,19 +95,31 @@ def response_path(path): """ - # TODO: Raise a NameError if the requested content is not present - # under webroot. + # path = "webroot" + path + # path was "/a_web_page.html" + path = os.path.join("webroot", path.strip('/')) + + # now path is "webroot/a_web_page.html" - # 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 + # IF the path points to a file eg: webroot/a_web_page.html + if os.path.isfile(path): + with open(path, "rb") as f: + content = f.read() + mime_type = mimetypes.guess_type(path)[0].encode() + + # IF the path points to a directoty eg: webroot/images + elif os.path.isdir(path): + content = " ".join(os.listdir(path)).encode() + mime_type = b"text/plain" + + # IF the path points to a non_existing eg: webroot/asdfoo.html + else: + raise NameError + + # return content, mime_type # - # 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" + # content = b"not implemented" + # mime_type = b"not implemented" return content, mime_type @@ -114,30 +146,24 @@ 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. - # TODO: Use response_path to retrieve the content and the mimetype, - # based on the request path. + print("Request received:\n{}\n\n".format(request)) - # 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" - ) + try: + path = parse_request(request) + content, mime_type = response_path(path) + response = response_ok(body=content, mimetype=mime_type) + except NameError: + response = response_not_found() + except NotImplementedError: + response = response_method_not_allowed() conn.sendall(response) except: traceback.print_exc() finally: - conn.close() + conn.close() except KeyboardInterrupt: sock.close() @@ -149,5 +175,3 @@ def server(log_buffer=sys.stderr): if __name__ == '__main__': server() sys.exit(0) - - From 9257c739a306541218943f8ede456d1010bcb0f4 Mon Sep 17 00:00:00 2001 From: HABTAMU ASFAW Date: Tue, 23 Apr 2019 21:23:26 -0700 Subject: [PATCH 2/4] clean up on http-server py file --- http_server.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/http_server.py b/http_server.py index 5d7fbad..ac19664 100644 --- a/http_server.py +++ b/http_server.py @@ -3,7 +3,6 @@ import socket import traceback import mimetypes -# import requests as req def response_ok(body, mimetype): @@ -116,11 +115,6 @@ def response_path(path): else: raise NameError - # return content, mime_type - # - # content = b"not implemented" - # mime_type = b"not implemented" - return content, mime_type From 23060bd9ec288b1a96c896e2d1fa7b6c6ead5113 Mon Sep 17 00:00:00 2001 From: HABTAMU ASFAW Date: Tue, 23 Apr 2019 22:47:15 -0700 Subject: [PATCH 3/4] lesson03 assignment submission --- http_server.py | 10 +++------- tests.py | 2 +- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/http_server.py b/http_server.py index ac19664..76068dc 100644 --- a/http_server.py +++ b/http_server.py @@ -43,7 +43,6 @@ def response_method_not_allowed(): def response_not_found(): """Returns a 404 response Not Found """ - return b"\r\n".join([ b"HTTP/1.1 404 Request Not Found response", b"", @@ -95,23 +94,20 @@ def response_path(path): """ # path = "webroot" + path - # path was "/a_web_page.html" path = os.path.join("webroot", path.strip('/')) - # now path is "webroot/a_web_page.html" - - # IF the path points to a file eg: webroot/a_web_page.html + # If the path points to a file eg: webroot/a_web_page.html if os.path.isfile(path): with open(path, "rb") as f: content = f.read() mime_type = mimetypes.guess_type(path)[0].encode() - # IF the path points to a directoty eg: webroot/images + # If the path points to a directoty eg: webroot/images elif os.path.isdir(path): content = " ".join(os.listdir(path)).encode() mime_type = b"text/plain" - # IF the path points to a non_existing eg: webroot/asdfoo.html + # If the path points to a non_existing eg: webroot/asdfoo.html else: raise NameError diff --git a/tests.py b/tests.py index 21da57e..f3e3d05 100644 --- a/tests.py +++ b/tests.py @@ -190,7 +190,7 @@ def test_root_index(self): def test_ok_response_at_root_index(self): """ - A call to / at least yields a 200 OK response + A call to / at least yields a 200 OK response """ directory = '' From 14328c98f3c3036ed987c6dd614b8f0e575accf6 Mon Sep 17 00:00:00 2001 From: HABTAMU ASFAW Date: Tue, 23 Apr 2019 22:57:03 -0700 Subject: [PATCH 4/4] update to list content with newlines --- http_server.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/http_server.py b/http_server.py index 76068dc..8642a29 100644 --- a/http_server.py +++ b/http_server.py @@ -104,7 +104,7 @@ def response_path(path): # If the path points to a directoty eg: webroot/images elif os.path.isdir(path): - content = " ".join(os.listdir(path)).encode() + content = "\n".join(os.listdir(path)).encode() mime_type = b"text/plain" # If the path points to a non_existing eg: webroot/asdfoo.html @@ -137,7 +137,6 @@ def server(log_buffer=sys.stderr): if '\r\n\r\n' in request: break - print("Request received:\n{}\n\n".format(request)) try: