Skip to content

Commit 239e951

Browse files
committed
Add homework changes.
1 parent 34e5cf6 commit 239e951

File tree

2 files changed

+75
-20
lines changed

2 files changed

+75
-20
lines changed

README.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# SOCKET HTTP SERVER
2+
3+
Once you're done, you should be able to start the web server inside the homework directory using `python -u http_server.py` and then point your web browser at locations like:
4+
* http://localhost:10000/sample.txt
5+
* http://localhost:10000/a_web_page.html
6+
* http://localhost:10000/images/sample_1.png
7+
8+
and see the corresponding file located under homework/webroot. Take a moment to look into the homework/webroot and see these files. 
9+
10+
Inside this repository you'll find the http_server.py file we’ve just written in class. I've added enough stub code for the missing functions to let the server run. And there are more tests for you to make pass!
11+
12+
You do NOT need to execute the `make_time.py` Python file. When a web user visits `http://localhost:1000/make_time.py` you only need to _serve up_ the contents of that file. But if you'd like to take on a challenge, then you _can_ choose to execute the file and serve up the result of performming that execution.
13+
14+
15+
## Building the Response
16+
17+
Your `response_path` function will need to accomplish the following tasks:
18+
19+
* It should take a URI as the sole argument
20+
* It should map the pathname represented by the URI to a filesystem location.
21+
* It should have a ‘home directory’, and look only in that location.
22+
* If the URI is a directory, it should return a plain-text listing of the directory contents and the mimetype text/plain.
23+
* If the URI is a file, it should return the contents of that file and its correct mimetype.
24+
* If the URI does not map to a real location, it should raise an exception that the server can catch to return a 404 response.
25+
26+
Because your server will be transmitting files as bytes, you might want to try searching for "reading a file as bytes in Python".
27+
28+
To find the correct mimetype for a file, you might find the following code helpful:
29+
30+
```
31+
>>> import mimetypes
32+
>>> mimetypes.guess_type('file.txt')[0]
33+
'text/plain'
34+
>>> mimetypes.types_map['.txt']
35+
'text/plain'
36+
```
37+
38+
## Use Your Tests
39+
40+
As you work your way through the steps outlined above, look at your tests. Write code that makes them pass.
41+

http_server.py

+34-20
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,21 @@ def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"):
1919
'''
2020
"""
2121

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

2529
def response_method_not_allowed():
2630
"""Returns a 405 Method Not Allowed response"""
2731

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

3138

3239
def response_not_found():
@@ -44,8 +51,12 @@ def parse_request(request):
4451
NotImplementedError if the method of the request is not GET.
4552
"""
4653

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

5061
def response_path(path):
5162
"""
@@ -118,20 +129,23 @@ def server(log_buffer=sys.stderr):
118129

119130
print("Request received:\n{}\n\n".format(request))
120131

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

136150
conn.sendall(response)
137151
except:

0 commit comments

Comments
 (0)