-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_final.py
164 lines (148 loc) · 6.53 KB
/
server_final.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
import socket
import ssl
import threading
import csv
import pickle
TCP_SERVER_HOST = 'localhost'
TCP_SERVER_PORT = 12345#5555
CERTFILE = 'server.crt'
KEYFILE = 'server.key'
UDP_SERVER_HOST = 'localhost'
UDP_SERVER_PORT = 12000#5555
sum_value=0
def tcp_server():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((TCP_SERVER_HOST, TCP_SERVER_PORT))
server_socket.listen(1)
ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ssl_context.load_cert_chain(certfile=CERTFILE, keyfile=KEYFILE)
print("TCP Server is listening for connections...")
while True:
connection, address = server_socket.accept()
print("TCP Connection from:", address)
connection = ssl_context.wrap_socket(connection, server_side=True)
message_to_client = "This is a test message from the server to the client! SSL established"
connection.sendall(message_to_client.encode())
connection.close()
def handle_client(server_socket):
sum_value=0
while True:
data, client_address = server_socket.recvfrom(1024)
print(f"Received data from {client_address}: {data.decode()}")
i=0
lst=[]
if(i==0):
lst.append(client_address[1])
i=i+1
if(client_address[1] in lst):
message = data.decode("utf-8")
if message == "1":
data1, _ = server_socket.recvfrom(1024)
message1 = data1.decode("utf-8")
browse(client_address, message1, server_socket)
elif message == "2":
file = open("item3.csv", "r")
sum2=0
message2, _ = server_socket.recvfrom(1024)
message2 = message2.decode("utf-8")
message3, _ = server_socket.recvfrom(1024)
message3 = message3.decode("utf-8")
sum2+=purchase(file, message2, message3, client_address, server_socket)
elif message == "3":
modified_message1 = pickle.dumps(receipt)
server_socket.sendto(modified_message1, client_address)
elif message == "4":
break
else:
modified_message = "Wrong choice"
server_socket.sendto(modified_message.encode("utf-8"), client_address)
else:
message = data.decode("utf-8")
if message == "1":
data1, _ = server_socket.recvfrom(1024)
message1 = data1.decode("utf-8")
browse(client_address, message1, server_socket)
elif message == "2":
sum1=0
file = open("item3.csv", "r")
message2, _ = server_socket.recvfrom(1024)
message2 = message2.decode("utf-8")
message3, _ = server_socket.recvfrom(1024)
message3 = message3.decode("utf-8")
sum1+=purchase(file, message2, message3, client_address, server_socket)
elif message == "3":
modified_message1 = pickle.dumps(receipt)
server_socket.sendto(modified_message1, client_address)
# modified_message2 = str(sum_value).encode("utf-8")
# server_socket.sendto(modified_message2, client_address)
elif message == "4":
break
else:
modified_message = "Wrong choice"
server_socket.sendto(modified_message.encode("utf-8"), client_address)
def purchase(csvfile, message2, message3, client_address, server_socket):
sum_value = 0
csvreader = csv.reader(csvfile)
rows = list(csvreader)
for row in rows:
if row[0] == 'Sno':
continue
if int(message3) < int(row[2]):
if row[0] == message2:
for i in receipt:
if i[0] == message2:
i[2] = str(int(i[2]) + int(message3))
sum_value = sum_value+ (int(row[3]) * int(message3))
row[2] = str(int(row[2]) - int(message3))
modified_message = pickle.dumps(receipt)
server_socket.sendto(modified_message, client_address)
break
else:
row[2] = str(int(row[2]) - int(message3))
sum_value= sum_value+ (int(row[3]) * int(message3))
lst = list((row[0], row[1], message3, row[3]))
receipt.append(lst)
modified_message = pickle.dumps(receipt)
server_socket.sendto(modified_message, client_address)
else:
modified_message = "Insufficient quantity!!"
server_socket.sendto(modified_message.encode("utf-8"), client_address)
with open("item3.csv", "w", newline="") as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerows(rows)
return sum_value
def browse(client_address, msg1, server_socket):
try:
with open("item3.csv", "r") as csvfile:
csvreader = csv.reader(csvfile)
next(csvreader)
for row in csvreader:
if msg1 == "1" and 100 < int(row[0]) < 106:
modified_message = pickle.dumps(row)
server_socket.sendto(modified_message, client_address)
elif msg1 == "2" and 200 < int(row[0]) < 206:
modified_message = pickle.dumps(row)
server_socket.sendto(modified_message, client_address)
elif msg1 == "3" and 300 < int(row[0]) < 306:
modified_message = pickle.dumps(row)
server_socket.sendto(modified_message, client_address)
elif msg1 == "4" and 400 < int(row[0]) < 406:
modified_message = pickle.dumps(row)
server_socket.sendto(modified_message, client_address)
elif msg1 == "5" and 500 < int(row[0]) < 506:
modified_message = pickle.dumps(row)
server_socket.sendto(modified_message, client_address)
except UnicodeDecodeError:
print("Error")
receipt=[]
# Start TCP server thread
tcp_thread = threading.Thread(target=tcp_server)
tcp_thread.start()
s=0
# Start UDP server
server_socket_udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket_udp.bind((UDP_SERVER_HOST, UDP_SERVER_PORT))
print("UDP Server is listening for connections...")
# Handle UDP client requests
thread = threading.Thread(target=handle_client, args=(server_socket_udp,))
thread.start()