From d002952c32659d9b17925a1e9a6fd513d6c2fcc7 Mon Sep 17 00:00:00 2001 From: mdenko Date: Mon, 20 Apr 2020 20:25:49 -0700 Subject: [PATCH] Initial committ for assignment 3 Initial committ for assignment 3 --- .DS_Store | Bin 0 -> 8196 bytes http_server.py | 79 ++++++++++++++++++++++++++++++++++++------------- 2 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..400bbd6af16c848ca5580a89204875e5bac5f8ce GIT binary patch literal 8196 zcmeHMU2GIp6h5ad^v(cwic~twPd9C98)#u$Xe&SB_77Wu(6B8nrLfHI3}xbUrtZw{ z0;Q&EG(H%NKZ)`01%0c^6N&QTi@(wM6C3{$eb5&ljYbon^xS)=4Yc%u7nLw~bMLux z&pC6?`R+G!b7uhnyYglWKs5j`s$6QTsJTJmdR~{5l5nJqB$7XXN53Fb^d~dU66_a&P~siF4ftJ85(_Ib-639PdL?~VFiF6b79X-&)f z))9;B?1I%p*8Yf1x8r6SDzmGrHr3v}HPN>FzMi=?cT{MVYgKiWfN=x+v~A^vinbY? zIcE4-%QbCxe7IoycE+&=U1P$k#NCQAch~yrHF`|nIImjl!n8UzlF!%=vzXcyrU*@4jjkW(n(mxMt;v`eZfMliL7zu#goaJcsyg@}t*GWx2t4qX zR#hKzjEv=IrXtKhm)PUev;38(C~x9la@}vG;89TF{stXvDx@00G0RtVj(LT}#W5n}GcnqF`i|{nO46nmw zxB~COhwv$U4qw2R@ICwlzrb(sJNyBE!r#cS3fE&b>R5~0a62Y&7w*F(c48MEzyUmh zDICNTm_-Xu<2X*@Sv-$ZcmW^Bi}(yai_hUJIE!!MTX-4Y#&_^4zK8GQXZRJ)$@og< zNqZ=aHx&2-brwCFL|Xc`CP;hoMx>4P?pOQ%8`6HaS~B&{byb_|8k<`?_I56;=%O{e zmP~>{k|KgS^VATFcpV@4s_U^0>Zav5Jw(j2&N5CPtz25iW3`MJqY29BvHDm8BU)=E zLaT0xk)^@3Qfa(hZDB-HZH3hCRNECIvQ{RwyVO05_^Pdx+I?zL;g6aI^142zDD37r zcnN0VDtrK+5cj@@AK_>C75+lRa;y}*+khK!Gd5r&Zo#eCjP2M#4BU--u^0DaANCUq z2XP38aRf(+g{EMkjXCr&j|F1mBlswB@d Raises a NameError """ + try: + response_ok(path) + + except NameError: + reponse_not_fount() # TODO: Raise a NameError if the requested content is not present # under webroot. @@ -85,7 +110,9 @@ 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" @@ -114,30 +141,42 @@ 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. - - # 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) + + # TODO: Use response_path to retrieve the content and the mimetype, + # based on the request 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" + ) + except NotImplementedError: + response = response_method_not_allowed() + + try: + response_path(response) + except NameError: + response = response_method_not_found() conn.sendall(response) + + except: traceback.print_exc() finally: - conn.close() + conn.close() except KeyboardInterrupt: sock.close() @@ -149,5 +188,3 @@ def server(log_buffer=sys.stderr): if __name__ == '__main__': server() sys.exit(0) - -