-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
61 lines (51 loc) · 1.48 KB
/
server.py
File metadata and controls
61 lines (51 loc) · 1.48 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
# Author: KUMAR DEV
# SAP ID: 500083164
# ROLL NO: R214220637
# Batch: 5
# Course: Container Orchestration and Infrastructure Automation
# Program: BTECH CSE & Spl. CC&VT
import socket
import os
import random
import hashlib
# using sha256 hash_function
def get_checksum(filename):
with open(filename, "rb") as f:
bytes = f.read()
res = hashlib.sha256(bytes).hexdigest()
return res
def main():
# Create serverdata folder
try:
os.mkdir(os.path.join("./","serverdata"))
except:
pass
# Write random data into serverdata/data.txt file
f = open("./serverdata/data.txt", "w")
for i in range(20):
randomlist = random.randint(1,100)
f.write(str(randomlist) + '\n')
f.close()
host = socket.gethostname()
try:
port = os.environ["PORT"]
except:
port = 8081
server_socket = socket.socket()
server_socket.bind((host, port))
server_socket.listen(1)
connection = server_socket.accept()[0]
# Read from serverdata/data.txt file and send it to the client
file_to_read = open("./serverdata/data.txt", "rb")
data = file_to_read.read(1024)
while data:
connection.send(data)
data = file_to_read.read(1024)
file_to_read.close()
# Calculate checkSum and send it to the client
checkSum = get_checksum("./serverdata/data.txt")
connection.send(checkSum.encode())
# Close the connection
connection.close()
if __name__ == '__main__':
main()