forked from UWPCEWebPython/socket-http-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_server.py
More file actions
112 lines (84 loc) · 3.15 KB
/
http_server.py
File metadata and controls
112 lines (84 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import socket
import sys
def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"):
"""
returns a basic HTTP response
Ex:
response_ok(
b"<html><h1>Welcome:</h1></html>",
b"text/html"
) ->
b'''
HTTP/1.1 200 OK\r\n
Content-Type: text/html\r\n
\r\n
<html><h1>Welcome:</h1></html>\r\n
'''
"""
pass
def parse_request(request):
pass
def response_method_not_allowed():
"""Returns a 405 Method Not Allowed response"""
pass
def response_not_found():
"""Returns a 404 Not Found response"""
pass
def resolve_uri(uri):
"""
This method should return appropriate content and a mime type.
If the requested URI is a directory, then the content should be a
plain-text listing of the contents with mimetype `text/plain`.
If the URI is a file, it should return the contents of that file
and its correct mimetype.
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.
Ex:
resolve_uri('/a_web_page.html') -> (b"<html><h1>North Carolina...",
b"text/html")
resolve_uri('/images/sample_1.png')
-> (b"A12BCF...", # contents of sample_1.png
b"image/png")
resolve_uri('/') -> (b"images/, a_web_page.html, make_type.py,...",
b"text/plain")
resolve_uri('/a_page_that_doesnt_exist.html') -> Raises a NameError
"""
# TODO: Raise a NameError if the requested content is not present
# under webroot.
# TODO: Fill in the appropriate content and mime_type give the URI.
# See the assignment guidelines for help on "mapping mime-types", though
# you might need to create a special case for handling make_time.py
content = b"not implemented"
mime_type = b"not implemented"
return content, mime_type
def server(log_buffer=sys.stderr):
address = ('127.0.0.1', 10000)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print("making a server on {0}:{1}".format(*address), file=log_buffer)
sock.bind(address)
sock.listen(1)
try:
while True:
print('waiting for a connection', file=log_buffer)
conn, addr = sock.accept() # blocking
try:
print('connection - {0}:{1}'.format(*addr), file=log_buffer)
while True:
data = conn.recv(16)
print('received "{0}"'.format(data), file=log_buffer)
if data:
print('sending data back to client', file=log_buffer)
conn.sendall(data)
else:
msg = 'no more data from {0}:{1}'.format(*addr)
print(msg, log_buffer)
break
finally:
conn.close()
except KeyboardInterrupt:
sock.close()
return
if __name__ == '__main__':
server()
sys.exit(0)