forked from chiho924/PyWeb-03
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_server.py
More file actions
112 lines (92 loc) · 3.4 KB
/
http_server.py
File metadata and controls
112 lines (92 loc) · 3.4 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
111
112
import mimetypes
import pathlib
import socket
import sys
def response_ok(body=b"this is a pretty minimal response", mimetype=b"text/plain"):
"""returns a basic HTTP response as bytes"""
resp = []
resp.append(b"HTTP/1.1 200 OK")
resp.append(b"Content-Type: " + mimetype)
resp.append(b"")
resp.append(body)
return b"\r\n".join(resp)
def response_method_not_allowed():
"""returns a 405 Method Not Allowed response as bytes"""
resp = []
resp.append("HTTP/1.1 405 Method Not Allowed")
resp.append("")
return "\r\n".join(resp).encode('utf8')
def response_not_found():
"""returns a 404 Not Found response as bytes"""
resp = []
resp.append("HTTP/1.1 404 Not Found")
resp.append("")
return "\r\n".join(resp).encode('utf8')
def parse_request(request):
first_line = request.split("\r\n", 1)[0]
method, uri, protocol = first_line.split()
if method != "GET":
raise NotImplementedError("We only accept GET")
return uri
def resolve_uri(uri):
"""This method should return appropriate content and a mime type
Both content and mime type should be expressed as bytes
"""
root_path = pathlib.Path('./')
resource_path = root_path / uri.lstrip('/')
if resource_path.is_dir():
# the resource is a directory, content type is text/html, produce a
# listing of the directory contents
item_template = '* {}'
listing = []
for item_path in resource_path.iterdir():
listing.append(item_template.format(str(item_path)))
content = "\n".join(listing).encode('utf8')
mime_type = 'text/plain'.encode('utf8')
elif resource_path.is_file():
# the resource is a file, figure out its mime type and read
content = resource_path.read_bytes()
mime_type = mimetypes.guess_type(str(resource_path))[0].encode('utf8')
else:
raise NameError()
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)
request = ''
while True:
data = conn.recv(1024)
request += data.decode('utf8')
if len(data) < 1024:
break
try:
uri = parse_request(request)
except NotImplementedError:
response = response_method_not_allowed()
else:
try:
content, mime_type = resolve_uri(uri)
except NameError:
response = response_not_found()
else:
response = response_ok(content, mime_type)
print('sending response', file=log_buffer)
conn.sendall(response)
finally:
conn.close()
except KeyboardInterrupt:
sock.close()
return
if __name__ == '__main__':
server()
sys.exit(0)