-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseeding.py
More file actions
79 lines (64 loc) · 2.51 KB
/
seeding.py
File metadata and controls
79 lines (64 loc) · 2.51 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
73
74
75
76
77
78
79
import os
import hashlib
import threading
import signal
import sys
import time
from upload_chunks import seeding_server
from register_seeder import register_seeder_to_tracker
# CLI seeder script
# Function to upload chunks of a file
def upload_chunks(file_path, chunk_size, output_dir):
"""
Splits a file into chunks and stores them in output_dir.
Returns a list of SHA256 hashes for each chunk.
"""
chunk_hashes = []
file_name = os.path.basename(file_path)
with open(file_path, 'rb') as f:
index = 0
while True:
chunk = f.read(chunk_size)
if not chunk:
break
chunk_hash = hashlib.sha256(chunk).hexdigest()
chunk_hashes.append(chunk_hash)
chunk_file_name = f"chunk_{index}_{file_name}"
chunk_path = os.path.join(output_dir, chunk_file_name)
with open(chunk_path, 'wb') as cf:
cf.write(chunk)
index += 1
return chunk_hashes
# Function to shutdown the seeder gracefully
def shutdown_gracefully(signal, frame):
print("\nGracefully shutting down...")
sys.exit(0)
# Function to start the seeder
def start_seeding(file_path, peer_ip="127.0.0.1", peer_port=5000, chunk_size=1024*1024, output_dir="shared_chunks", tracker_ip="127.0.0.1", tracker_port=9000):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
print("Uploading chunks...")
chunk_hashes = upload_chunks(file_path, chunk_size, output_dir)
print(f"Uploaded {len(chunk_hashes)} chunks.")
file_name = os.path.basename(file_path)
print("Registering seeder with tracker...")
register_seeder_to_tracker(tracker_ip, tracker_port, file_name, peer_ip, peer_port, list(range(len(chunk_hashes))))
print(f"Starting seeder server on {peer_ip}:{peer_port}...")
seeder_thread = threading.Thread(
target=seeding_server,
args=(peer_ip, peer_port, file_name, chunk_size, chunk_hashes, output_dir),
daemon=True
)
seeder_thread.start()
print("Seeder is now live and serving chunks. Press Ctrl+C to stop.")
signal.signal(signal.SIGINT, shutdown_gracefully)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
shutdown_gracefully(None, None)
# Main function to run the seeder
if __name__ == "__main__":
file_path = input("Enter path to the file you want to seed: ")
peer_port = int(input("Enter the port number to use for seeding this file: "))
start_seeding(file_path, peer_port=peer_port)