Skip to content

Commit d002952

Browse files
committed
Initial committ for assignment 3
Initial committ for assignment 3
1 parent f7f0dac commit d002952

File tree

2 files changed

+58
-21
lines changed

2 files changed

+58
-21
lines changed

.DS_Store

8 KB
Binary file not shown.

http_server.py

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,34 @@ def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"):
2020
"""
2121

2222
# TODO: Implement response_ok
23-
return b""
23+
return b"\r\n".join([
24+
b"HTTP/1.1 200 OK",
25+
b"Content-Type: " + mimetype,
26+
b"",
27+
body,
28+
])
2429

2530
def response_method_not_allowed():
2631
"""Returns a 405 Method Not Allowed response"""
2732

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

3141

3242
def response_not_found():
3343
"""Returns a 404 Not Found response"""
3444

3545
# TODO: Implement response_not_found
36-
return b""
46+
return b"\r\n".join([
47+
b"HTTP/1.1 404 Method Not Found",
48+
b"",
49+
b"We can't find your method!"
50+
])
3751

3852

3953
def parse_request(request):
@@ -45,7 +59,13 @@ def parse_request(request):
4559
"""
4660

4761
# TODO: implement parse_request
48-
return ""
62+
conn = http.client.HTTPConnection('localhost:10000')
63+
if conn.request('GET', url):
64+
response = conn.getresponse()
65+
conn.close()
66+
return response
67+
68+
return NotImplementedError
4969

5070
def response_path(path):
5171
"""
@@ -74,6 +94,11 @@ def response_path(path):
7494
response_path('/a_page_that_doesnt_exist.html') -> Raises a NameError
7595
7696
"""
97+
try:
98+
response_ok(path)
99+
100+
except NameError:
101+
reponse_not_fount()
77102

78103
# TODO: Raise a NameError if the requested content is not present
79104
# under webroot.
@@ -85,7 +110,9 @@ def response_path(path):
85110
# If the path is "make_time.py", then you may OPTIONALLY return the
86111
# result of executing `make_time.py`. But you need only return the
87112
# CONTENTS of `make_time.py`.
88-
113+
114+
115+
89116
content = b"not implemented"
90117
mime_type = b"not implemented"
91118

@@ -114,30 +141,42 @@ def server(log_buffer=sys.stderr):
114141

115142
if '\r\n\r\n' in request:
116143
break
117-
144+
118145

119146
print("Request received:\n{}\n\n".format(request))
120147

121148
# TODO: Use parse_request to retrieve the path from the request.
122149

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-
)
150+
try:
151+
path = parse_request(request)
152+
153+
# TODO: Use response_path to retrieve the content and the mimetype,
154+
# based on the request path.
155+
156+
# TODO; If parse_request raised a NotImplementedError, then let
157+
# response be a method_not_allowed response. If response_path raised
158+
# a NameError, then let response be a not_found response. Else,
159+
# use the content and mimetype from response_path to build a
160+
# response_ok.
161+
response = response_ok(
162+
body=b"Welcome to my web server",
163+
mimetype=b"text/plain"
164+
)
165+
except NotImplementedError:
166+
response = response_method_not_allowed()
167+
168+
try:
169+
response_path(response)
170+
except NameError:
171+
response = response_method_not_found()
135172

136173
conn.sendall(response)
174+
175+
137176
except:
138177
traceback.print_exc()
139178
finally:
140-
conn.close()
179+
conn.close()
141180

142181
except KeyboardInterrupt:
143182
sock.close()
@@ -149,5 +188,3 @@ def server(log_buffer=sys.stderr):
149188
if __name__ == '__main__':
150189
server()
151190
sys.exit(0)
152-
153-

0 commit comments

Comments
 (0)