-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerSocket.py
More file actions
121 lines (91 loc) · 3.49 KB
/
Copy pathServerSocket.py
File metadata and controls
121 lines (91 loc) · 3.49 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
113
114
115
116
117
118
119
120
121
import socket
import os
import time
READ_BUFFER_SIZE = 1024
HOST = "localhost"
PORT = 80
def getInfomation(directory):
file_list = []
for i in os.listdir(directory):
a = os.stat(os.path.join(directory, i))
file_list.append([i, time.ctime(a.st_atime), a.st_size])
return file_list
def serverSocket(server):
while True:
# Chấp nhận kết nối
(conn, addr) = server.accept()
request = conn.recv(READ_BUFFER_SIZE).decode("utf8")
print(request)
# Phân tích các yêu cầu của client bằng " "
file_part = request.split(" ")
# Xử lý request của client
method = file_part[0]
request_file = file_part[1]
# Phân tích file
out_file = request_file.lstrip("/")
# Nếu file request trống thì sẽ trả về file index.html
if(out_file == ""):
out_file = ("index.html")
if (method == "POST"):
accuracy = request.split("\n")[-1]
print(accuracy)
# Kiểm tra username và password
if('{"username":"admin","password":"admin"}' in accuracy):
buffer = ("HTTP/1.1 200 OK\n")
buffer += ("Content-type: text/json\n")
buffer += ("\n")
buffer += ('{ "login": "passed" }')
conn.send(buffer.encode("utf8"))
else:
buffer = ("HTTP/1.1 200 OK\n")
buffer += ("Content-type: text/json\n")
buffer += ("\n")
buffer += ('{ "login": "failed" }')
conn.send(buffer.encode("utf8"))
else:
file = open(out_file, "rb")
res = file.read()
if(out_file.endswith(".png")):
mimetype = "image/png"
elif(out_file.endswith(".css")):
mimetype = "text/css"
elif(out_file.endswith(".jpg")):
mimetype = "image/jpg"
elif(out_file.endswith(".html")):
mimetype = "text/html"
elif(out_file.endswith(".mp3")):
mimetype = "video/mp3"
elif(out_file.endswith(".txt")):
mimetype = "text/plain"
elif(out_file.endswith(".webm")):
mimetype = "audio/webm"
elif(out_file.endswith(".pdf")):
mimetype = "application/pdf"
else:
mimetype = "image/vnd.microsoft.ico"
buffer = ("HTTP/1.1 200 OK\n")
if ((method == "GET") and ("/download" in request_file)):
buffer += ("Transfer Encoding: " 'chunked\n')
buffer += "Content-Type: "
buffer += mimetype
buffer += "\n\n"
print(buffer)
end = buffer.encode("utf-8")
end += res
conn.send(end)
file.close()
# Đóng socket
conn.close()
if __name__ == "__main__":
try:
# Tạo socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Socket listening on port", PORT)
# Nếu không khởi tạo được thì sẽ in ra lỗi
except socket.error as err:
print("Socket creation failed with error %s" % (err))
# Bind tại địa chỉ host và port
server.bind((HOST, PORT))
# Lắng nghe client request
server.listen(1)
serverSocket(server)