-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathannouncement .py
83 lines (50 loc) · 1.98 KB
/
announcement .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
import os
import math
import fnmatch
import time
from socket import *
import json
from collections import defaultdict
cs = socket(AF_INET, SOCK_DGRAM)
cs.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
cs.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
os.listdir("C://Users//Şevval//Desktop//CMP2204-PROJE")#Users must update the path addresses
listOfFiles = os.listdir('.') #to search the contents that user has
pattern = "*.png"
print("Choose a file name without entering '.png' ")
for entry in listOfFiles:
if fnmatch.fnmatch(entry, pattern):
print(entry)
content_name = input("--> ")#Enter the name of the content to be downloaded.
filename = content_name + '.png'
c = os.path.getsize(filename) #dividing the content
CHUNK_SIZE = math.ceil(math.ceil(c) / 5)
save_path = r'C://Users//Şevval//Desktop//CMP2204-PROJE//chunks'
if not os.path.exists(save_path):
os.makedirs(save_path)
index = 1
with open(filename, 'rb') as infile:
chunk = infile.read(int(CHUNK_SIZE))
while chunk:
chunkname = content_name + '_' + str(index)
completeName = os.path.join(save_path, chunkname)
with open(completeName, 'wb+') as chunk_file:
chunk_file.write(chunk)
index += 1
chunk = infile.read(int(CHUNK_SIZE))
chunk_file.close()
print('There are 5 chunks which named: ')
for i in range(1, 6):
print(content_name + "_" + str(i) + ", ")
print('Announcing process has started with these chunks')
while True:
a_dictionary = defaultdict(list)
pattern1 = "*" #read the names into the dictionary
listOfFiles1 = os.listdir(save_path)
for entry in listOfFiles1:
if fnmatch.fnmatch(entry, pattern1):
a_dictionary['chunks'].append(entry)
json_obj = json.dumps(a_dictionary)
cs.sendto(json_obj.encode("utf-8"), ('25.255.255.255', 5001))#broadcast message
print('Sending...')
time.sleep(15)