-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain-http-server.py
More file actions
315 lines (274 loc) · 10.3 KB
/
main-http-server.py
File metadata and controls
315 lines (274 loc) · 10.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
from socket import *
import os
import sys
import mimetypes
from config import *
from lib import *
import time
import datetime
import logging
import random
import threading
import webbrowser
COOKIE_ID = 0
server_tcp_sock = socket(AF_INET, SOCK_STREAM)
dns_udp_sock = socket(AF_INET, SOCK_DGRAM)
def find_ip_address():
try:
dns_udp_sock.connect('8.8.8.8', 8000) # find our why 8000
# 0- IP addr, 1 - port no. (returns a tuple)
ip_address = dns_udp_sock.getsockname()[0]
except:
ip_address = '127.0.0.1'
dns_udp_sock.close()
return ip_address
client_ip_address = str(find_ip_address()) # ip address of host or client
# print(client_ip_address)
logging.basicConfig(
filename=LOGFILE,
format=LOG_FORMAT,
level=logging.INFO)
# create logger with given name, if not specified - create root logger
logger = logging.getLogger()
# Function to return current date
def current_date():
curr_time = time.ctime().split(' ') # check
curr_time[0] = curr_time[0] + ','
time_msg = ''.join(curr_time)
time_msg = 'Date: ' + time_msg
return time_msg
# Function to return last modified date of data
def modified_date(data):
mod_time = time.ctime(os.path.getmtime(data)).split(' ')
for i in mod_time: # check
if(len(i) == 0):
mod_time.remove(i)
mod_time[0] = mod_time[0] + ','
time_msg = ''.join(mod_time)
time_msg = 'Last-Modified: ' + time_msg
return time_msg
# Function to return status codes output - in html file
def status_codes_output(status_code, connectionSocket):
#response = []
#response.append('HTTP/1.1' + status_code + status_codes_messages[status_code])
if status_code == '400':
fp = open(ROOT+'/utils/400.html', 'r')
if status_code == '401':
fp = open(ROOT+'/utils/401.html', 'r')
if status_code == '403':
fp = open(ROOT+'/utils/403.html', 'r')
if status_code == '404':
fp = open(ROOT+'/utils/404.html', 'r')
if status_code == '415':
fp = open(ROOT+'/utils/415.html', 'r')
if status_code == '500':
fp = open(ROOT+'/utils/500.html', 'r')
if status_code == '503':
fp = open(ROOT+'/utils/503.html', 'r')
if status_code == '505':
fp = open(ROOT+'/utils/505.html', 'r')
file_content = fp.read()
connectionSocket.send(file_content.encode())
#return file_content
#Function to handle get method
def handle_get_method(connectionSocket, client_request, clientList, COOKIE_ID):
get_response = []
data_element = ROOT + client_request[1]
if(os.path.isfile(data_element)):
if(os.access(data_element, os.W_OK) and os.access(data_element, os.R_OK)):
get_response.append('HTTP/1.1 200 OK')
get_response.append(current_date())
get_response.append('Server: HTTP/1.1')
get_response.append(modified_date(data_element))
element_size = os.path.getsize(data_element)
elem_size_str = str(element_size)
elem_size = 'Content-Length: ' + elem_size_str
get_response.append(elem_size)
file_details = os.path.splitext(data_element)
if file_details[1] in media_types.keys():
file_exten = media_types[file_details[1]]
file_exten_msg = 'Content-Type: ' + file_exten
get_response.append(file_exten_msg)
cookie_msg = 'Set-Cookie : Id = ' + str(COOKIE_ID) + ' '
cookie_msg = cookie_msg + 'Max-Age = 3000'
get_response.append(cookie_msg)
get_response.append('Connection closed\n\n')
fp = open(data_element, 'r')
file_content = fp.read()
encoded_response = '\r\n'.join(get_response).encode()
connectionSocket.send(encoded_response)
connectionSocket.send(file_content.encode())
clientList.remove(connectionSocket)
connectionSocket.close()
else:
status_codes_output('403', connectionSocket)
clientList.remove(connectionSocket)
connectionSocket.close()
else:
status_codes_output('404', connectionSocket)
clientList.remove(connectionSocket)
connectionSocket.close()
#Function to handle head method
def handle_head_method(connectionSocket, client_request, clientList, COOKIE_ID):
head_response = []
data_element = ROOT + client_request[1]
if(os.path.isfile(data_element)):
if(os.access(data_element, os.W_OK) and os.access(data_element, os.R_OK)):
head_response.append('HTTP/1.1 200 OK')
head_response.append(current_date())
head_response.append('Server: HTTP/1.1')
head_response.append(modified_date(data_element))
element_size = os.path.getsize(data_element)
elem_size_str = str(element_size)
elem_size = 'Content-Length: ' + elem_size_str
head_response.append(elem_size)
file_details = os.path.splitext(data_element)
if file_details[1] in media_types.keys():
file_exten = media_types[file_details[1]]
file_exten_msg = 'Content-Type: ' + file_exten
head_response.append(file_exten_msg)
cookie_msg = 'Set-Cookie : Id = ' + str(COOKIE_ID) + ' '
cookie_msg = cookie_msg + 'Max-Age = 3000'
head_response.append(cookie_msg)
head_response.append('Connection closed\n\n')
encoded_response = '\r\n'.join(head_response).encode()
connectionSocket.send(encoded_response)
put_msg = 'Put method successful!'
connectionSocket.send(put_msg.encode())
clientList.remove(connectionSocket)
connectionSocket.close()
else:
status_codes_output('403', connectionSocket)
clientList.remove(connectionSocket)
connectionSocket.close()
else:
status_codes_output('404', connectionSocket)
clientList.remove(connectionSocket)
connectionSocket.close()
#Function to handle put method
def handle_put_method(connectionSocket, client_request, clientList):
# Done on terminal
file = ROOT + client_request[1]
host_msg = 'Host: '
connectionSocket.send(host_msg.encode())
host_val = connectionSocket.recv(1024).decode()
content_type_msg = 'Content-Type: '
connectionSocket.send(content_type_msg.encode())
file_type_val = connectionSocket.recv(1024).decode()
content_msg = 'Content: '
connectionSocket.send(content_msg.encode())
content_val = connectionSocket.recv(1024).decode()
fp = open(file, 'a')
fp.write(content_val)
fp.close()
clientList.remove(connectionSocket)
connectionSocket.close()
#Function to handle delete method
def handle_delete_method(connectionSocket, client_request, clientList):
file = ROOT + client_request[1]
username_msg = 'Enter Username: '
connectionSocket.send(username_msg.encode())
username_val = connectionSocket.recv(1024).decode()
username_val = username_val.split()
password_msg = 'Enter Password: '
connectionSocket.send(password_msg.encode())
password_val = connectionSocket.recv(1024).decode()
password_val = password_val.split()
flag = 0
if(username_val[0] == USERNAME and password_val[0] == PASSWORD):
flag = 1
if(flag == 1):
if(os.path.isfile(file)):
os.remove(file)
del_success_msg = 'File Deleted Successfully\n'
connectionSocket.send(del_success_msg.encode())
else:
status_codes_output('404', connectionSocket) # check
else:
status_codes_output('401', connectionSocket)
clientList.remove(connectionSocket)
connectionSocket.close()
#Function to handle post method
def handle_post_method(connectionSocket, client_request, clientList):
post_response = []
uri_sentence = connectionSocket.recv(1024).decode()
line = uri_sentence.split('\r\n\r\n')
fp = fopen('post_data.txt', 'w')
post_data = line[0].split('&')
fp = open('post_data.txt', 'a')
post_data = line[1].split('&')
fp.write(post_data[0] + '\n')
fp.write(post_data[1] + '\n')
fp.write(post_data[2] + '\n')
fp.wrie('\n')
fp.close()
print('{}\n {} \n {} \n'.format(post_data[0], post_data[1],post_data[2]))
post_response.append('HTTP/1.1 200 OK')
modified_date = modified_date('post_data.txt')
post_response.append(modified_date)
post_response.append('Server: HTTP/1.1 (Ubuntu)')
post_response.append('Content-Language: en-US, en')
file_size = os.path.getsize('post_data.txt')
file_size = os.path.getsize('post_data.txt')
post_response.append('Content Length: ', str(file_size))
post_response.append('Content-Type: text/html')
len_response = len(post_response)
for i in range(len_response):
print(response[i])
clientList.remoce(connectionSocket)
connectionSocket.close()
#Function to handle all methods
def handle_all_methods(connectionSocket, addr, clientList, COOKIE_ID):
data = connectionSocket.recv(1024).decode()
client_request = data.split()
request = 'Request: '
address = 'Client Address: '
port = 'Port No.: '
logging.info('{} {} {} {} {} {}\n'.format(
address, addr[0], port, addr[1], request, data))
if(client_request[0] == 'GET'):
handle_get_method(connectionSocket, client_request, clientList, COOKIE_ID)
if(client_request[0] == 'HEAD'):
handle_head_method(connectionSocket, client_request, clientList, COOKIE_ID)
if(client_request[0] == 'PUT'):
handle_put_method(connectionSocket, client_request, clientList)
if(client_request[0] == 'DELETE'):
handle_delete_method(connectionSocket, client_request, clientList)
if(client_request[0] == 'POST'):
handle_post_method(connectionSocket, client_request, clientList)
#Function to connect with clients, by starting a new thread for each client until ,max no of requests are exceeded
def connect_func():
COOKIE_ID = 0
while(True):
connectionSocket, addr = serverSocket.accept()
print(connectionSocket)
clientList.append(connectionSocket)
if(len(clientList) <= MAX_NO_OF_REQUESTS):
print('Connected with Client Address : ', addr)
COOKIE_ID += 1
try:
th1 = threading.Thread(target=handle_all_methods, args=(connectionSocket, addr, clientList, COOKIE_ID))
th1.start()
except:
print('Cannot create a Thread')
else:
status_codes_output('503', connectionSocket)
clientList.remove(connectionSocket)
connectionSocket.close()
if __name__ == '__main__':
try:
server_port = int(sys.argv[1])
except:
print('Please Enter a Port Number for Server.')
print('Usage: python3 main-http-server.py <port_number>')
exit(1)
serverSocket = socket(AF_INET, SOCK_STREAM)
try:
serverSocket.bind(('127.0.0.1', server_port))
except:
print('Cannot start server')
exit(1)
serverSocket.listen(6)
print('HTTP Server has started running on Port: {}'.format(server_port))
connect_func()
serverSocket.close()