-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
105 lines (88 loc) · 3.18 KB
/
client.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
import os
import pickle
import threading
import socket
from threading import *
from peerShare import *
class peerServer:
def __init__(self):
print("Welcome to peer to peer file sharing system")
self.file_name = ""
self.peer_port = 0
def start_server(self):
while True:
choice = input("Menu \n(1) Register\n(2) Search\n(3) List all files\n(4) Download\n(5) Exit server\n")
choice = int(choice)
if choice == 1:
peer_id = input("Enter the 4 digit peer id: ")
self.file_name = input("Enter file name: ")
self.peer_port = int(peer_id)
self.register()
start_sharing(self.peer_port, 'localhost')
elif choice == 2:
self.search()
elif choice == 3:
self.list_all()
elif choice == 4:
peer_id = input("Enter the 4 digit peer id: ")
file_name = input("Enter file name: ")
self.download(int(peer_id), file_name)
elif choice == 5:
break
else:
continue
def register(self):
client = socket.socket()
client.connect(('localhost', 3456))
register_data = [1, self.peer_port, self.file_name]
data = pickle.dumps(register_data)
client.send(data)
state = client.recv(1024)
print(state.decode('utf-8'))
client.close()
def search(self):
client = socket.socket()
client.connect(('localhost', 3456))
file_name = input("Enter file name: ")
register_data = [2, file_name]
data = pickle.dumps(register_data)
client.send(data)
state = pickle.loads(client.recv(1024))
self.print_list(state[0], state[1])
client.close()
def list_all(self):
client = socket.socket()
client.connect(('localhost', 3456))
data = pickle.dumps([3])
client.send(data)
state = pickle.loads(client.recv(1024))
self.print_list(state[0], state[1])
client.close()
def print_list(self, files, keys):
if len(files) == 0:
print("There is no file available")
else:
print("Peer ID File Name Date Added")
for item in files:
print(item[keys[0]]," ",item[keys[1]], " ", item[keys[2]])
def download(self, peer_id, file_name):
client = socket.socket()
client.connect(('localhost', peer_id))
list_data = [4, str(file_name)]
data = pickle.dumps(list_data)
client.send(data)
# file_path = os.path.join(, '..')
file_path = os.path.join(os.getcwd(), 'SharingFiles')
file_path = os.path.join(file_path, 'Downloads')
with open(os.path.join(file_path, file_name), 'wb') as myfile:
while True:
data = client.recv(1024)
print(data.decode("utf-8"))
if not data:
myfile.close()
break
myfile.write(data)
client.close()
print('File is downloaded successfully.')
peer = peerServer()
peer.start_server()