-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.py
More file actions
58 lines (44 loc) · 1.67 KB
/
Server.py
File metadata and controls
58 lines (44 loc) · 1.67 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
import socket
import MemiumError
class Server:
def __init__(self, port):
self.PORT = port
self.IP = "127.0.0.1"
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.s.bind((self.IP, self.PORT))
self.s.listen(1)
self.conn = None
print("Server started")
self.commandlist = {}
def add_command(self, creq, overwrite=False):
commandname = creq.commandname.lower()
if commandname in self.commandlist.keys() and not overwrite:
raise MemiumError.CommandAlreadyExistsError
self.commandlist[commandname] = creq.process
def execute_command(self, commandname, args):
if commandname not in self.commandlist.keys():
print(f"{commandname} does not exist!")
return
res = self.commandlist[commandname](args)
lres = str(len(res)).rjust(4, "0")
header = f"len:{lres}"
print("Sending header: " + header)
self.conn.send(header.encode("utf-8"))
ack = self.conn.recv(4)
print(str(ack))
if ack.decode("utf-8") == "true":
print("Sending content: " + str(res))
self.conn.send(res)
def mainloop(self):
self.conn, addr = self.s.accept()
if self.conn:
print("Got connection from " + addr[0])
while True:
data = self.conn.recv(1024)
data = data.decode("utf-8")
print("Recieved: "+data)
data = data.split(" ")
commandname = data[0]
args = data[1:]
self.execute_command(commandname, args)