-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
43 lines (35 loc) · 1.37 KB
/
handler.py
File metadata and controls
43 lines (35 loc) · 1.37 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
from parsing import *
import sys
CRLN = "\r\n"
def extractPath(theString):
return theString.split()
def getLines(data):
return (data.split('\r\n'))
def createResponse(responseN, randStr, type="text/plain"):
if randStr != None:
return (responseN + CRLN + "Content-Type: " + type + CRLN + "Content-Length: " + str(len(randStr)) + crln + crln + randStr + crln)
else:
return (responseN + CRLN + CRLN)
def createClassicResponse(responseN, body, type):
return (responseN + CRLN + "Content-Type: " + type + CRLN + CRLN + body + CRLN)
def respFromStatusCode(code):
if code == 200:
response = "HTTP/1.1 200 OK"
elif code == 404:
response = "HTTP/1.1 404 Not Found"
elif code == 201:
response = "HTTP/1.1 201 Created"
return response
def clientHandler(client_socket, directory):
data = client_socket.recv(1024)
if (data):
print(data.decode('utf-8'))
dataList = getLines(data.decode('utf-8'))
body, httpCode, theType = extractBody(dataList, directory)
responseN = respFromStatusCode(httpCode)
if theType == "text/html" or theType == "text/css":
theResponse = createClassicResponse(responseN, body, theType)
else:
theResponse = createResponse(responseN, body)
client_socket.send(theResponse.encode('utf-8'))
client_socket.close()