-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponse.py
35 lines (30 loc) · 1018 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
34
35
import json
from common.headers.headers import Headers
class Response:
def __init__(self,conn):
self.conn = conn
# default values
self.Headers = Headers("RESPONSE")
self.StatusCode = 0
self.StatusText = ""
self.Body = {}
def setHeaders(self,headers):
self.Headers.headers = headers
def setHeader(self,header,value):
self.Headers.setHeader(header,value)
def setStatus(self,code,text):
self.Headers.setHeader("STATUS-TEXT", text)
self.Headers.setHeader("STATUS-CODE", code)
self.StatusCode = code
self.StatusText = text
self.Headers.setHeader("STATUS", f"{code} {text}")
def setBody(self,body):
self.Body = body
def getResponse(self):
response = {
"headers": self.Headers.getHeaders(),
"body": self.Body
}
return json.dumps(response)
def SendResponse(self):
self.conn.sendall(bytes(self.getResponse(), "utf-8"))