-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.py
More file actions
69 lines (53 loc) · 2.38 KB
/
Server.py
File metadata and controls
69 lines (53 loc) · 2.38 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
import socket
import random
def generate_partial_phrase(x):
random.seed(x)
x = random.random()
return x
# this is the data structure where the received values are stored from the client
received_phrases = []
print("Server is listening..........")
# always specify the private ip address
HOST = '127.0.0.1'
PORT = 9090
# defining the server and the type of socket, sock_stream means that it is a tcp socket,
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# now we need to bind the server to a host and the port
server.bind((HOST, PORT))
# keep listening, at max 5 connections,
# if more than 5 connections are waiting then we reject
server.listen(5)
while True:
# we communicate using the communication socket
communication_socket, address = server.accept()
print(f"Connected to {address}")
# specifying the number of bytes to receive
# you need to be able to decode the messages in order to read the messages
# has the ability to send 1024 bytes
message = communication_socket.recv(1024).decode('utf-8')
print(f"Message from the client is {message}")
communication_socket.send(f"Got your message, Thank You!".encode('utf-8'))
communication_socket.send(
f"Your random seed is {str(generate_partial_phrase(abs(random.randint(1, 1000))))}".encode('utf-8'))
# We do not need the ticket id here for this case
# ticket_id = ticket_id + 1 # important to increment the number after sent to the client
# We need to store the message2 as the instance of the password received due to the random seed sent from the server
message2 = communication_socket.recv(1024).decode('utf-8')
print(f"Message from the client is {message2}")
# message2 = float(message2) * (10 ** 8)
# Count is used here to ensure that the message2 is only append into the received_phrases only once and not
# repeatedly
count = 0
if count < 1:
received_phrases.append(message2)
print("These are the received phrases from the assisting clients/peers so far: ")
print(received_phrases)
f = open('vault.txt', 'a')
message2 = str(message2)
message2 = str(message2[0:])
message = f.write(f"{message2}\n")
f.close()
# closing the connection with the server and client
communication_socket.close()
print(f"Connection with {address} ended")
count = count + 1