forked from DataStories-UniPi/miniDB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.py
More file actions
72 lines (62 loc) · 2.71 KB
/
Server.py
File metadata and controls
72 lines (62 loc) · 2.71 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
from database import Database
import socket
import sys
from io import StringIO
import SQLcompiler
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
# Function to run when a clinet connects to the server
def clientConnect(receivedData, data, oldDatabase):
#receivedData is a flag that tells the function to either wait for input(first run from client) line 21
# or either tell it that I have a new input from the client so just execute that
# data is the variable that I store the desired query and it contains a value only when the function is recursively called
sendToClient = False # if true the server will send the response to the client
print('Connected by', addr)
try:
if receivedData: # If this is the first query, wait for input
data = conn.recv(1024) # Receive query
data = data.decode("utf-8") # Decode it
data = data.lower()
data2 = data.split(" ")
try:
if "use" in data2:
db, msg = SQLcompiler.sqlCompiler(data)
else:
db = None
db, msg = SQLcompiler.sqlCompiler(data,db)
except:
conn.sendall("Something went wrong with your input..")
conn.close()
else:
db, msg = SQLcompiler.sqlCompiler(data,oldDatabase)
except:
conn.sendall("Something went wrong with your input..")
conn.close()
print(msg)
conn.sendall(str.encode(msg))
data = conn.recv(1024) # Receive query
data = data.decode("utf-8") # Decode it
print(data)
if data != "":
# Recursivly call it's self with setting the receivedData flag to false so it won't wait again for input
clientConnect(False, data, db)
# receivedData -> False, there is already available data to execute
# data -> Now has the received data from the client
else:
print("Empty Input!!!\nTerminating connection...")
conn.close()
while True:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen()
global db
conn, addr = s.accept()
with conn:
clientConnect(True, None, None)
# receivedData -> True, First Run
# data -> None, Since it is the first run no data is available
s.close()
print(
"Clients valid queries were executed\nDropping connection..\nWaiting for next connection...\n")
continue # Break out and wait for next connection