11import  socket 
22import  sys 
33import  traceback 
4+ import  os 
5+ import  mimetypes 
6+ 
47
58def  response_ok (body = b"This is a minimal response" , mimetype = b"text/plain" ):
69    """ 
@@ -20,20 +23,34 @@ def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"):
2023    """ 
2124
2225    # TODO: Implement response_ok 
23-     return  b"" 
26+     return  b"\r \n " .join ([
27+         b"HTTP/1.1 200 OK" ,
28+         b"Content-type: "  +  mimetype ,
29+         b"" ,
30+         body ,
31+     ])
32+ 
2433
2534def  response_method_not_allowed ():
2635    """Returns a 405 Method Not Allowed response""" 
2736
2837    # TODO: Implement response_method_not_allowed 
29-     return  b"" 
38+     return  b"\r \n " .join ([
39+         b"HTTP/1.1 405 Method Not Allowed" ,
40+         b"" ,
41+         b"You can't do that here!" 
42+     ])
3043
3144
3245def  response_not_found ():
3346    """Returns a 404 Not Found response""" 
3447
3548    # TODO: Implement response_not_found 
36-     return  b"" 
49+     return  b"\r \n " .join ([
50+         b"HTTP/1.1 404 Not Found" ,
51+         b"" ,
52+         b"What you are looking for does not seem to exist!" 
53+     ])
3754
3855
3956def  parse_request (request ):
@@ -45,7 +62,13 @@ def parse_request(request):
4562    """ 
4663
4764    # TODO: implement parse_request 
48-     return  "" 
65+     method , path , version  =  request .split ("\r \n " )[0 ].split (" " )
66+ 
67+     if  method  !=  "GET" :
68+         raise  NotImplementedError 
69+ 
70+     return  path 
71+ 
4972
5073def  response_path (path ):
5174    """ 
@@ -85,9 +108,20 @@ def response_path(path):
85108    # If the path is "make_time.py", then you may OPTIONALLY return the 
86109    # result of executing `make_time.py`. But you need only return the 
87110    # CONTENTS of `make_time.py`. 
88-     
89-     content  =  b"not implemented" 
90-     mime_type  =  b"not implemented" 
111+ 
112+     req_path  =  os .path .abspath ("." ) +  "/webroot"  +  path 
113+ 
114+     if  os .path .isdir (req_path ):
115+         str_conv  =  " \n " .join (os .listdir (req_path ))
116+         content  =  str_conv .encode ()
117+         mime_type  =  b"text/plain" 
118+     elif  os .path .isfile (req_path ):
119+         content  =  open (req_path , "rb" ).read ()
120+         mime_type  =  mimetypes .guess_type (req_path )[0 ].encode ()
121+     else :
122+         content  =  b"NotImplamented" 
123+         mime_type  =  b"NotImplamented" 
124+         raise  NameError 
91125
92126    return  content , mime_type 
93127
@@ -114,24 +148,31 @@ def server(log_buffer=sys.stderr):
114148
115149                    if  '\r \n \r \n '  in  request :
116150                        break 
117- 		
118151
119152                print ("Request received:\n {}\n \n " .format (request ))
120153
121154                # TODO: Use parse_request to retrieve the path from the request. 
122- 
123-                 # TODO: Use response_path to retrieve the content and the mimetype, 
124-                 # based on the request path. 
125- 
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-                 )
155+                 try :
156+                     path  =  parse_request (request )
157+ 
158+                     content , mime_type  =  response_path (path )
159+ 
160+                     # TODO: Use response_path to retrieve the content and the mimetype, 
161+                     # based on the request path. 
162+ 
163+                     # TODO; If parse_request raised a NotImplementedError, then let 
164+                     # response be a method_not_allowed response. If response_path raised 
165+                     # a NameError, then let response be a not_found response. Else, 
166+                     # use the content and mimetype from response_path to build a 
167+                     # response_ok. 
168+                     response  =  response_ok (
169+                         body = content ,
170+                         mimetype = mime_type 
171+                     )
172+                 except  NotImplementedError :
173+                     response  =  response_method_not_allowed ()
174+                 except  NameError :
175+                     response  =  response_not_found ()
135176
136177                conn .sendall (response )
137178            except :
0 commit comments