-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
188 lines (175 loc) · 8.74 KB
/
server.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from socket import *
import math
import os
serverPort = 8080
serverSocket = socket(AF_INET,SOCK_STREAM) #IPv4 - TCP
serverSocket.bind(("localhost", serverPort))
serverSocket.listen(1)
print("The server is ready to receive")
# efficiently checks if the given parameter (num) is prime
def isPrime(num):
if num < 2:
return False
i=2
while i<=math.sqrt(num):
if num%i<1:
return False
i+=1
return num>1
while True:
# Wait for client connections
client_connection, client_address = serverSocket.accept()
print("New Connection\n----------------")
print(client_address)
print("----------------")
# Get the client request
request = client_connection.recv(1024).decode()
# parse HTTP request message
request_line = request.split('\r\n')[0] # for ex: req_line -> GET /isPrime?number=5 HTTP/1.1
metot = request_line.split()[0] # GET, POST, PUT, DELETE
url = request_line.split()[1] #/isPrime?number=5, upload, rename/..., remove?fileName=file1.png, download/...
response = 'HTTP/1.1 404 NOT FOUND\n\n'
isDownload = False
if metot == 'GET':
correctEndPoint = False
hasParam = True
try:
endpoint, parameters = url.split('?')
except:
hasParam = False
if hasParam:
if endpoint == '/isPrime':
correctEndPoint = True
response = 'HTTP/1.1 200 OK\nContent-Type: application/json\n\n'
containsNumber = False
for param in parameters.split('&'):
try:
key, value = param.split('=')
except:
continue
if key == 'number':
containsNumber = True
try:
result = isPrime(int(value))
response += '{"number": ' + value + ', "isPrime": ' + str(result) + '}'
except:
response = 'HTTP/1.1 400 Bad Request\nContent-Type: application/json\n\n{"message": "Lutfen tam sayi giriniz"}\r\n\r\n'
if not containsNumber:
response = 'HTTP/1.1 400 Bad Request\nContent-Type: application/json\n\n{"message": "Parametreler arasinda number yok!"}\r\n\r\n'
if endpoint == '/download':
correctEndPoint = True
containsFile = False
for param in parameters.split('&'):
try:
key, value = param.split('=')
except:
continue
if key == 'fileName':
containsFile = True
if not os.path.exists(value):
response = 'HTTP/1.1 200 OK\nContent-Type: application/json\n\n{"filename": "' + value + '", "message": "Dosya bulunamadi"}\r\n\r\n'
else:
isDownload = True
response = 'HTTP/1.1 200 OK\nContent-Type: application/octet-stream\nContent-Disposition: attachment; filename="' + value + '"\n\n'
client_connection.sendall(response.encode())
with open(value, 'rb') as file_to_send:
for data in file_to_send:
client_connection.sendall(data)
if not containsFile:
response = 'HTTP/1.1 400 Bad Request\nContent-Type: application/json\n\n{"message": "Parametreler arasinda fileName yok!"}\r\n\r\n'
elif correctEndPoint:
response = 'HTTP/1.1 400 Bad Request\nContent-Type: application/json\n\n{"message": "Eksik parametre!"}\r\n\r\n'
if metot == 'POST':
if url == '/upload':
print(request)
#some hard-code for extract filename and boundary value from request message
boundary_start = request.find("boundary=")+9
boundary_end = request.find("\r", boundary_start)
boundary = request[boundary_start:boundary_end]
start_index = request.find("filename=")
if start_index != -1:
end_index = request.find("\"", start_index+10)
file_name = request[start_index+10:end_index]
data = client_connection.recv(4096)
while True:
incoming_packet = client_connection.recv(4096)
x = incoming_packet.find(bytes(boundary, 'ascii'))
if x != -1:
new_packet = incoming_packet.replace(bytes(boundary, 'ascii'), b'')
data += new_packet[:-8]
break
data += incoming_packet
file = open(file_name, "wb")
print(data)
file.write(data)
file.close()
response = 'HTTP/1.1 200 OK\nContent-Type: application/json\n\n{"message": "Dosya basariyla yuklendi!"}\r\n\r\n'
else:
response = 'HTTP/1.1 400 Bad Request\nContent-Type: application/json\n\n{"message": "Dosya gonderilmedi!"}\r\n\r\n'
if metot == 'PUT':
hasParam = True
try:
endpoint, parameters = url.split('?')
except:
hasParam = False
if hasParam:
if endpoint == '/rename':
oldExist = False
newExist = False
oldName, newName = "", ""
for param in parameters.split('&'):
try:
key, value = param.split('=')
except:
continue
if key == "oldFileName":
oldExist = True
oldName = value
if key == "newName":
newExist = True
newName = value
if oldExist and newExist:
if not os.path.exists(oldName):
response = 'HTTP/1.1 200 OK\nContent-Type: application/json\n\n{"filename": "' + oldName + '", "message": "Dosya bulunamadi!"}\r\n\r\n'
elif os.path.isfile(newName):
response = 'HTTP/1.1 200 OK\nContent-Type: application/json\n\n{"message": "Yeni dosya ismi zaten mevcut!"}\r\n\r\n'
else:
os.rename(oldName, newName)
response = 'HTTP/1.1 200 OK\nContent-Type: application/json\n\n' + \
'{"oldName": "' + oldName + '", "newName": "' + newName + '", "message": "Dosya ismi basariyla degistirildi!"}\r\n\r\n'
else:
response = 'HTTP/1.1 400 Bad Request\nContent-Type: application/json\n\n{"message": "Eksik query parametresi!"}\r\n\r\n'
else:
response = 'HTTP/1.1 400 Bad Request\nContent-Type: application/json\n\n{"message": "Eksik query parametresi!"}\r\n\r\n'
if metot == 'DELETE':
hasParam = True
try:
endpoint, parameters = url.split('?')
except:
hasParam = False
if hasParam:
fileNameExist = False
if endpoint == '/remove':
for param in parameters.split('&'):
try:
key, value = param.split('=')
except:
continue
if key == "fileName":
fileNameExist = True
if not os.path.exists(value):
response = 'HTTP/1.1 200 OK\nContent-Type: application/json\n\n{"filename": "' + oldName + '", "message": "Dosya bulunamadi"}\r\n\r\n'
else:
os.remove(value)
response = 'HTTP/1.1 200 OK\nContent-Type: application/json\n\n{"message": "Dosya basarili bir sekilde silindi"}\r\n\r\n'
if not fileNameExist:
response = 'HTTP/1.1 400 Bad Request\nContent-Type: application/json\n\n{"message": "fileName parametresi eksik"}\r\n\r\n'
else:
response = 'HTTP/1.1 400 Bad Request\nContent-Type: application/json\n\n{"message": "fileName parametresi eksik"}\r\n\r\n'
print("Client send " + metot + " request to endpoint " + url)
# Send HTTP response
if not isDownload:
print('Server response...\n' + response)
client_connection.sendall(response.encode())
print('Connection closing...')
client_connection.close()