@@ -19,12 +19,22 @@ def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"):
1919 '''
2020 """
2121
22- # TODO: Implement response_ok
23- return b""
22+ return b'\r \n ' .join ([
23+ b'HTTP/1.1 200 OK' ,
24+ b'Content-Type: ' + mimetype ,
25+ b'' ,
26+ body ,
27+ ])
2428
2529def response_method_not_allowed ():
2630 """Returns a 405 Method Not Allowed response"""
2731
32+ return b'\r \n ' .join ([
33+ b'HTTP/1.1 405 Method Not Allowed' ,
34+ b'' ,
35+ b'You can\' t do that on this server' ,
36+ ])
37+
2838 # TODO: Implement response_method_not_allowed
2939 return b""
3040
@@ -44,8 +54,12 @@ def parse_request(request):
4454 NotImplementedError if the method of the request is not GET.
4555 """
4656
47- # TODO: implement parse_request
48- return ""
57+ method , path , version = request .split ('\r \n ' )[0 ].split (' ' )
58+
59+ if method != 'GET' :
60+ raise NotImplementedError
61+
62+ return path
4963
5064def response_path (path ):
5165 """
@@ -85,7 +99,7 @@ def response_path(path):
8599 # If the path is "make_time.py", then you may OPTIONALLY return the
86100 # result of executing `make_time.py`. But you need only return the
87101 # CONTENTS of `make_time.py`.
88-
102+
89103 content = b"not implemented"
90104 mime_type = b"not implemented"
91105
@@ -114,30 +128,34 @@ def server(log_buffer=sys.stderr):
114128
115129 if '\r \n \r \n ' in request :
116130 break
117-
118131
119- print ("Request received:\n {}\n \n " .format (request ))
120-
121- # TODO: Use parse_request to retrieve the path from the request.
122132
123- # TODO: Use response_path to retrieve the content and the mimetype,
124- # based on the request path.
133+ print ("Request received:\n {}\n \n " .format (request ))
125134
126- # TODO; If parse_request raised a NotImplementedError, then let
127- # response be a method_not_allowed response. If response_path raised
128- # a NameError, then let response be a not_found response. Else,
129- # use the content and mimetype from response_path to build a
130- # response_ok.
131- response = response_ok (
132- body = b"Welcome to my web server" ,
133- mimetype = b"text/plain"
134- )
135+ # Use parse_request to retrieve the path from the request.
136+ try :
137+ path = parse_request (request )
138+
139+ # TODO: Use response_path to retrieve the content and the mimetype,
140+ # based on the request path.
141+
142+ # TODO; If parse_request raised a NotImplementedError, then let
143+ # response be a method_not_allowed response. If response_path raised
144+ # a NameError, then let response be a not_found response. Else,
145+ # use the content and mimetype from response_path to build a
146+ # response_ok.
147+ response = response_ok (
148+ body = b"Welcome to my web server" ,
149+ mimetype = b"text/plain"
150+ )
151+ except NotImplementedError :
152+ response = response_method_not_allowed ()
135153
136154 conn .sendall (response )
137155 except :
138156 traceback .print_exc ()
139157 finally :
140- conn .close ()
158+ conn .close ()
141159
142160 except KeyboardInterrupt :
143161 sock .close ()
@@ -149,5 +167,3 @@ def server(log_buffer=sys.stderr):
149167if __name__ == '__main__' :
150168 server ()
151169 sys .exit (0 )
152-
153-
0 commit comments