@@ -18,16 +18,16 @@ def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"):
1818 Content-Type: text/html\r \n
1919 \r \n
2020 <html><h1>Welcome:</h1></html>\r \n
21- """
22- print ( mimetype )
23- print ( body )
24- response = f"""HTTP/1.1 200 OK \r
25- Content-Type: { mimetype } \r \n \r
26- <html><body> { body } </body></html> \r
27- """
28- # response = "HTTP/1.1 200 OK \r\n"
29- # response += "Content-Type:{}\r\n ".format(mimetype )
30- # response += "\r\n"
21+ # """
22+ # response = f"""HTTP/1.1 200 OK\r
23+ # Content-Type:{mimetype}\r\n\r
24+ # {body}
25+ # """
26+ response = "HTTP/1.1 200 OK \r \n "
27+ response += "Content-Type:{} \r \n " . format ( mimetype )
28+ response + = "\r \n "
29+ response += "{} " .format (body )
30+
3131 # response += "html><body>{}</body></html>\r\n".format(body)
3232
3333 return response .encode ()
@@ -117,9 +117,15 @@ def response_path(path):
117117 mime_type = types .get (extension )
118118 print (mime_type )
119119
120- with open (path , 'rt' ) as file :
121- content = file .read ()
120+ if "text" in mime_type :
121+ with open (path , 'r' , newline = "\r \n " ) as file :
122+ content = file .read ()
122123
124+ else :
125+ with open (path , 'rb' ) as file :
126+ content = file .read ()
127+ # print(content)
128+
123129
124130 # TODO: Fill in the appropriate content and mime_type give the path.
125131 # See the assignment guidelines for help on "mapping mime-types", though
@@ -256,7 +262,7 @@ def server(log_buffer=sys.stderr):
256262 body = content ,
257263 mimetype = mime_type
258264 )
259- print (response )
265+ # print(response)
260266 conn .sendall (response )
261267 except NotImplementedError :
262268 response = response_method_not_allowed ()
0 commit comments