This repository was archived by the owner on Feb 3, 2022. It is now read-only.
forked from asimshrestha2/socket-python-OS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequestapp.py
More file actions
47 lines (39 loc) · 1.3 KB
/
requestapp.py
File metadata and controls
47 lines (39 loc) · 1.3 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
class Requestapp:
resstate = {
200 : "HTTP/1.1 200 OK",
400 : "HTTP/1.1 400 Bad Request",
404 : "HTTP/1.1 404 Not Found"
}
def __init__(self, path, body, status=200):
self.status = status
self.header = {}
self.body = body
self.response = """\
%s
%s"""
if path != "":
self.__findContentType(path)
else:
print "No Path Given."
def __findContentType(self, path):
pathsplit = path.split('.')
fileType = pathsplit[len(pathsplit)-1]
if(fileType == "png" or fileType == "jpg"):
self.__contentType("image/" + fileType)
elif(fileType == "html" or fileType == "css"):
self.__contentType("text/" + fileType)
elif(fileType == "js"):
self.__contentType("text/javascript")
else:
self.__contentType("text/plain")
def __contentType(self, type):
self.header["Content-Type"] = type
print self.header
def getResponse(self):
status = Requestapp.resstate[self.status]
hres = status + "\r\n"
for k, v in self.header.items():
hres = hres + k + ": " + v + "\r\n"
self.response = self.response % (hres, self.body)
print self.response
return self.response