Skip to content

Commit 1501d63

Browse files
committed
Lesson 3 Assignment submit
1 parent 239e951 commit 1501d63

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

http_server.py

+33-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import socket
22
import sys
33
import traceback
4+
import os.path
5+
import mimetypes
46

57
def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"):
68
"""
@@ -30,7 +32,7 @@ def response_method_not_allowed():
3032
"""Returns a 405 Method Not Allowed response"""
3133

3234
return b"\r\n".join([
33-
b"HTTP/1.1 403 Method Not Allowed",
35+
b"HTTP/1.1 405 Method Not Allowed",
3436
b"",
3537
b"You can't do that on this server!",
3638
])
@@ -40,7 +42,11 @@ def response_not_found():
4042
"""Returns a 404 Not Found response"""
4143

4244
# TODO: Implement response_not_found
43-
return b""
45+
return b"\r\n".join([
46+
b"HTTP/1.1 404 Page Not Found",
47+
b"",
48+
b"I could not find that page!",
49+
])
4450

4551

4652
def parse_request(request):
@@ -96,10 +102,26 @@ def response_path(path):
96102
# If the path is "make_time.py", then you may OPTIONALLY return the
97103
# result of executing `make_time.py`. But you need only return the
98104
# CONTENTS of `make_time.py`.
99-
100-
content = b"not implemented"
101-
mime_type = b"not implemented"
102-
105+
#1: The path indicates a FILE that exists inside of webroot.
106+
#2: The path indicates a DIRECTORY that exists inside of webroot.
107+
#3: The path does NOT indicate a FILE or DIRECTORY that exists inside of webroot
108+
#If path was '/sample.txt'
109+
absolute_path = os.path.join(os.getcwd(), "webroot", path.strip('/'))
110+
#Then absolute path is now 'C:\Users\navgill/pythonweb/socket-server-hw/socket-http-server-homework'
111+
if os.path.isfile(absolute_path):
112+
content = b""
113+
with open(absolute_path, "rb") as f:
114+
byte = f.read(1)
115+
while byte != b"":
116+
content += byte
117+
byte = f.read(1)
118+
mime_type = mimetypes.guess_type(absolute_path)[0].encode()
119+
elif os.path.isdir(absolute_path):
120+
# Option 1: content = str(os.listdir(absolute_path))
121+
content = " ".join(os.listdir(absolute_path)).encode()
122+
mime_type = b"text/plain"
123+
else:
124+
raise NameError
103125
return content, mime_type
104126

105127

@@ -134,16 +156,19 @@ def server(log_buffer=sys.stderr):
134156

135157
# TODO: Use response_path to retrieve the content and the mimetype,
136158
# based on the request path.
159+
body, mimetype = response_path(path)
137160

138161
# TODO:
139162
# If response_path raised
140163
# a NameError, then let response be a not_found response. Else,
141164
# use the content and mimetype from response_path to build a
142165
# response_ok.
143166
response = response_ok(
144-
body=b"Welcome to my web server",
145-
mimetype=b"text/plain"
167+
body= body,
168+
mimetype=mimetype
146169
)
170+
except NameError:
171+
response = response_not_found()
147172
except NotImplementedError:
148173
response = response_method_not_allowed()
149174

0 commit comments

Comments
 (0)