-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
166 lines (115 loc) · 8.11 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
import socket
from _thread import *
import threading
import pickle
from bcolors import *
from request_handler import processRequest
from error import *
import multiprocessing
from hard_write import hardWrite
# bht ganda kam, allowing server thread to access one variable from ram
import ram
# from root_cli import root_cli
huge_file = "v9n79D60ifjhXbh6bluIjRQErcYuGaEBhu5vYmXl4mSxboWN1koFteqrkNRE89WoBdo5RFPn4Mz05Ie5nfueaWCrwdh1WwHhMeyAehUj0VAOF0q0Pzc7b9dE9kJHdWVAvF80JOcNAxKk6I5fVnvZwks9M3JQ9MUjst9gid6oLYUadkXUXB7WRx4ryRWQTywtpH0cgy9UIU7fQXBPvW78ZJLmVxpvADsIBGCeX5gKleeeScrJ07C0HVjTyvVAIOLmafJTEixC5SsKX2dUaQBncj9KwlBL9AUziyoEIm8OxqqucKgeyRpTUZocYt4QCpLm2BTN9DmyzsJX2iBDZPkW02wznJal4cY2LWRZ8OzesAsRUo164J57NDTrhPL81G2KZZyBD0mXVXskqjeiQtLTCgut7JtdeDGu3jMKXASAFZc2WlKJGfWCEHwspwXyCo4DNiTWKZuDm01hEkOCDL6QBQ7TK3nXviPER0SJueHzk6A4sCEJoWKz1BxBU7nHIGgHOeItSQ7f67IJLx4rCL5fKZsE8N897Gs8zZZs03DYFeZgkq0lGGyB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92q4EkJv6I12XBnOTF6VYPT3JIzHDDi97L2ygLtSqObUpsHTYvL9dRTcc6jrlaAcPatS2xuV8BHSfibYOpNhKRaUm7TgX3HNniNChlusyuWSrpG4IU6DG9l4h9s4fCVup8N1ATzhLziMUboDTE0jx1NQs71304G2reHlR4nXSH7JMYfpKCNHArOlqHxFVhxMI8JxomrZ18jWkHgYyb5W320iuohxhXBNeKsAt1s60SyF1fKh93RK79vlYScmLlWsv4FY6kTVa1ZiLqVdJ9NlKvzYje2P3g1"
# from repl2 import *
import traceback
def make_socket():
ssocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
return ssocket
def start_listening(port = 55555, ssocket = None, clients = 3):
# host = socket.gethostname()
host = ''
ssocket.bind((host, port))
ssocket.listen(clients)
print(f"Listeing on port {port}")
def prepare_response(response):
size = len(response);
msg = str(size) + "-" + response;
return msg.encode('utf-8')
def make_response(response, connection):
connection.send(response)
def receive_request(connection):
request = connection.recv(8).decode('utf-8')
# no would be length of message to be received
no = ""
# start index of actual data in first string
start = 0
for i,ch in enumerate(request):
if ch!='-':
no += ch
continue
start = i;
break
# size (no of chars) of data to receive
size = int(no)
# actual data
command = request[start+1:]
while len(command) < size:
# 64 K is max packet size for TCP
command = command + connection.recv(6400).decode('utf-8')
return command
def handle_client(connection, currentUser, mode):
while True:
if ram.changes == ram.changes_threashold:
p1 = multiprocessing.Process(target=hardWrite, args=("autoSaved"+ram.current_fileSystem, ))
p1.start()
ram.changes = 0
command = receive_request(connection)
# If client sends -1 then close that clients connection.
if command == "e":
# remove userSpace first
stat = processRequest(command, currentUser)
# send ok
make_response("0-".encode('utf-8'), connection)
# then close connection
connection.close()
if mode == 0:
print(f'{bcolors.WARNING}Connection with user {currentUser} is closed.{bcolors.ENDC}')
# then close thread
return
# response to be sent to client
msg = processRequest(command, currentUser)
if msg == "0":
continue
size = len(msg)
msg = str(size) + "-" + msg;
connection.send((msg.encode('utf-8')))
connection.close()
def server_thread(ssocket, mode):
while True:
connection, address = ssocket.accept()
if address[0] in ram.user_space:
connection.close();
continue;
currentUser = address[0]
# in user space of user save current inode #
# in the start this is just 0
# userspace = [inode#, array of parent stack, array of threads_output
ram.user_space[currentUser] = [0,[],[]]
# if it is server mode then show prompt regarding new connections
# else keep an array for of connections
if mode == 0:
print(f'{bcolors.WARNING}Connected to: {address[0]} on port {str(address[1])}{bcolors.ENDC}')
start_new_thread(handle_client, (connection,currentUser, mode))
ssocket.close()
def setup_server():
print(f'Provide port number and client limmit, space separated - {bcolors.WARNING}port clients{bcolors.ENDC}')
while True:
port_client = input().split()
if len(port_client) != 2:
wrong_argument()
continue
try:
port = int(port_client[0])
clients_allowed = int(port_client[1])
except:
arguments_not_int()
continue
try:
ssocket = make_socket();
start_listening(port=port,ssocket=ssocket, clients = clients_allowed);
return ssocket;
except Exception as e:
traceback.print_exc()
print(f'{bcolors.WARNING}WRITE AGAIN!{bcolors.ENDC}')
continue