Skip to content

Commit 447896b

Browse files
committed
Clarify assignment.
1 parent afa939c commit 447896b

File tree

1 file changed

+65
-31
lines changed

1 file changed

+65
-31
lines changed

http_server.py

+65-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import socket
22
import sys
3+
import traceback
34

45
def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"):
56
"""
@@ -17,63 +18,74 @@ def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"):
1718
<html><h1>Welcome:</h1></html>\r\n
1819
'''
1920
"""
20-
pass
21+
22+
# TODO: Implement response_ok
23+
return b""
24+
25+
def response_method_not_allowed():
26+
"""Returns a 405 Method Not Allowed response"""
27+
28+
# TODO: Implement response_method_not_allowed
29+
return b""
30+
31+
32+
def response_not_found():
33+
"""Returns a 404 Not Found response"""
34+
35+
# TODO: Implement response_not_found
36+
return b""
2137

2238

2339
def parse_request(request):
2440
"""
25-
Given the content of an HTTP request, returns the uri of that request.
41+
Given the content of an HTTP request, returns the path of that request.
2642
2743
This server only handles GET requests, so this method shall raise a
2844
NotImplementedError if the method of the request is not GET.
2945
"""
30-
pass
31-
32-
33-
def response_method_not_allowed():
34-
"""Returns a 405 Method Not Allowed response"""
35-
pass
3646

47+
# TODO: implement parse_request
48+
return ""
3749

38-
def response_not_found():
39-
"""Returns a 404 Not Found response"""
40-
pass
41-
42-
43-
def resolve_uri(uri):
50+
def response_path(path):
4451
"""
4552
This method should return appropriate content and a mime type.
4653
47-
If the requested URI is a directory, then the content should be a
54+
If the requested path is a directory, then the content should be a
4855
plain-text listing of the contents with mimetype `text/plain`.
4956
50-
If the URI is a file, it should return the contents of that file
57+
If the path is a file, it should return the contents of that file
5158
and its correct mimetype.
5259
53-
If the URI does not map to a real location, it should raise an
60+
If the path does not map to a real location, it should raise an
5461
exception that the server can catch to return a 404 response.
5562
5663
Ex:
57-
resolve_uri('/a_web_page.html') -> (b"<html><h1>North Carolina...",
64+
response_path('/a_web_page.html') -> (b"<html><h1>North Carolina...",
5865
b"text/html")
5966
60-
resolve_uri('/images/sample_1.png')
67+
response_path('/images/sample_1.png')
6168
-> (b"A12BCF...", # contents of sample_1.png
6269
b"image/png")
6370
64-
resolve_uri('/') -> (b"images/, a_web_page.html, make_type.py,...",
71+
response_path('/') -> (b"images/, a_web_page.html, make_type.py,...",
6572
b"text/plain")
6673
67-
resolve_uri('/a_page_that_doesnt_exist.html') -> Raises a NameError
74+
response_path('/a_page_that_doesnt_exist.html') -> Raises a NameError
6875
6976
"""
7077

7178
# TODO: Raise a NameError if the requested content is not present
7279
# under webroot.
7380

74-
# TODO: Fill in the appropriate content and mime_type give the URI.
81+
# TODO: Fill in the appropriate content and mime_type give the path.
7582
# See the assignment guidelines for help on "mapping mime-types", though
7683
# you might need to create a special case for handling make_time.py
84+
#
85+
# If the path is "make_time.py", then you may OPTIONALLY return the
86+
# result of executing `make_time.py`. But you need only return the
87+
# CONTENTS of `make_time.py`.
88+
7789
content = b"not implemented"
7890
mime_type = b"not implemented"
7991

@@ -94,22 +106,44 @@ def server(log_buffer=sys.stderr):
94106
conn, addr = sock.accept() # blocking
95107
try:
96108
print('connection - {0}:{1}'.format(*addr), file=log_buffer)
109+
110+
request = ''
97111
while True:
98-
data = conn.recv(16)
99-
print('received "{0}"'.format(data), file=log_buffer)
100-
if data:
101-
print('sending data back to client', file=log_buffer)
102-
conn.sendall(data)
103-
else:
104-
msg = 'no more data from {0}:{1}'.format(*addr)
105-
print(msg, log_buffer)
112+
data = conn.recv(1024)
113+
request += data.decode('utf8')
114+
115+
if b'\r\n\r\n' in data:
106116
break
117+
118+
119+
print("Request received:\n{}\n\n".format(request))
120+
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+
content=b"Welcome to my web server",
133+
mimetype=b"text/plain"
134+
)
135+
136+
conn.sendall(response)
137+
except:
138+
traceback.print_exc()
107139
finally:
108-
conn.close()
140+
conn.close()
109141

110142
except KeyboardInterrupt:
111143
sock.close()
112144
return
145+
except:
146+
traceback.print_exc()
113147

114148

115149
if __name__ == '__main__':

0 commit comments

Comments
 (0)