Skip to content

Commit a5cc307

Browse files
committed
implemented functions based on videos
1 parent f156b29 commit a5cc307

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

http_server.py

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
import traceback
44

5+
56
def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"):
67
"""
78
returns a basic HTTP response
@@ -19,14 +20,22 @@ def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"):
1920
'''
2021
"""
2122

22-
# TODO: Implement response_ok
23-
return b""
23+
response = b"HTTP/1.1 200 OK\r\n"
24+
response += b"Content-Type: " + mimetype + b"\r\n"
25+
response += b"\r\n"
26+
response += body + b"\r\n"
27+
28+
return response
29+
2430

2531
def response_method_not_allowed():
2632
"""Returns a 405 Method Not Allowed response"""
2733

28-
# TODO: Implement response_method_not_allowed
29-
return b""
34+
return b"\r\n".join([
35+
b"HTTP/1.1 405 Method Not Allowed",
36+
b"",
37+
b"You can't do that on this server!"
38+
])
3039

3140

3241
def response_not_found():
@@ -44,8 +53,13 @@ def parse_request(request):
4453
NotImplementedError if the method of the request is not GET.
4554
"""
4655

47-
# TODO: implement parse_request
48-
return ""
56+
method, path, version = request.split("\r\n")[0].split(" ")
57+
58+
if method != "GET":
59+
raise NotImplementedError
60+
61+
return path
62+
4963

5064
def response_path(path):
5165
"""
@@ -114,24 +128,25 @@ def server(log_buffer=sys.stderr):
114128

115129
if '\r\n\r\n' in request:
116130
break
117-
118131

119132
print("Request received:\n{}\n\n".format(request))
120133

121-
# 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-
)
134+
try:
135+
path = parse_request(request)
136+
# TODO: Use response_path to retrieve the content and the mimetype,
137+
# based on the request path.
138+
139+
# TODO; If parse_request raised a NotImplementedError, then let
140+
# response be a method_not_allowed response. If response_path raised
141+
# a NameError, then let response be a not_found response. Else,
142+
# use the content and mimetype from response_path to build a
143+
# response_ok.
144+
response = response_ok(
145+
body=b"Welcome to my web server",
146+
mimetype=b"text/plain"
147+
)
148+
except NotImplementedError:
149+
response = response_method_not_allowed()
135150

136151
conn.sendall(response)
137152
except:

0 commit comments

Comments
 (0)