1
1
import socket
2
2
import sys
3
+ import traceback
3
4
4
5
def response_ok (body = b"This is a minimal response" , mimetype = b"text/plain" ):
5
6
"""
@@ -17,63 +18,74 @@ def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"):
17
18
<html><h1>Welcome:</h1></html>\r \n
18
19
'''
19
20
"""
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""
21
37
22
38
23
39
def parse_request (request ):
24
40
"""
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.
26
42
27
43
This server only handles GET requests, so this method shall raise a
28
44
NotImplementedError if the method of the request is not GET.
29
45
"""
30
- pass
31
-
32
-
33
- def response_method_not_allowed ():
34
- """Returns a 405 Method Not Allowed response"""
35
- pass
36
46
47
+ # TODO: implement parse_request
48
+ return ""
37
49
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 ):
44
51
"""
45
52
This method should return appropriate content and a mime type.
46
53
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
48
55
plain-text listing of the contents with mimetype `text/plain`.
49
56
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
51
58
and its correct mimetype.
52
59
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
54
61
exception that the server can catch to return a 404 response.
55
62
56
63
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...",
58
65
b"text/html")
59
66
60
- resolve_uri ('/images/sample_1.png')
67
+ response_path ('/images/sample_1.png')
61
68
-> (b"A12BCF...", # contents of sample_1.png
62
69
b"image/png")
63
70
64
- resolve_uri ('/') -> (b"images/, a_web_page.html, make_type.py,...",
71
+ response_path ('/') -> (b"images/, a_web_page.html, make_type.py,...",
65
72
b"text/plain")
66
73
67
- resolve_uri ('/a_page_that_doesnt_exist.html') -> Raises a NameError
74
+ response_path ('/a_page_that_doesnt_exist.html') -> Raises a NameError
68
75
69
76
"""
70
77
71
78
# TODO: Raise a NameError if the requested content is not present
72
79
# under webroot.
73
80
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 .
75
82
# See the assignment guidelines for help on "mapping mime-types", though
76
83
# 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
+
77
89
content = b"not implemented"
78
90
mime_type = b"not implemented"
79
91
@@ -94,22 +106,44 @@ def server(log_buffer=sys.stderr):
94
106
conn , addr = sock .accept () # blocking
95
107
try :
96
108
print ('connection - {0}:{1}' .format (* addr ), file = log_buffer )
109
+
110
+ request = ''
97
111
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 :
106
116
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 ()
107
139
finally :
108
- conn .close ()
140
+ conn .close ()
109
141
110
142
except KeyboardInterrupt :
111
143
sock .close ()
112
144
return
145
+ except :
146
+ traceback .print_exc ()
113
147
114
148
115
149
if __name__ == '__main__' :
0 commit comments