-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.py
33 lines (26 loc) · 964 Bytes
/
response.py
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
import logging
class Response:
def __init__(self, status, reason, headers: dict = None, body=None):
self.status = status
self.reason = reason
self.headers = headers
self.body = body
@staticmethod
def send_response(conn, resp):
log_response = "\n"
wfile = conn.makefile('wb')
status_line = f'HTTP/1.1 {resp.status} {resp.reason}\r\n'
log_response += status_line[:-1]
wfile.write(status_line.encode('iso-8859-1'))
if resp.headers:
for (key, value) in resp.headers.items():
header_line = f'{key}: {value}\r\n'
log_response += header_line
wfile.write(header_line.encode('iso-8859-1'))
wfile.write(b'\r\n')
if resp.body:
log_response += resp.body
wfile.write(resp.body.encode('iso-8859-1'))
logging.info(log_response)
wfile.flush()
wfile.close()