-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
84 lines (48 loc) · 1.79 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
import socket
import threading
import sys
# from peer import Peer
class ClientConnection:
def __init__(self, addr):
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.s.connect((addr, 5000))
self.previous_data = None
i_thread = threading.Thread(target=self.send_message)
i_thread.daemon = True
i_thread.start()
while True:
r_thread = threading.Thread(target=self.recieve_message)
r_thread.start()
r_thread.join()
data = self.recieve_message()
if not data:
print("-" * 21 + " Server failed " + "-" * 21)
break
elif data[0:1] == b'\x11':
print("Got peers")
self.update_peers(data[1:])
print("\n",data[1:], "\n")
def recieve_message(self):
try:
print("Recieving -------")
data = self.s.recv(1024)
print(data.decode("utf-8"))
return data
except KeyboardInterrupt:
self.send_disconnect_signal()
def update_peers(self, peers):
p2p.peers = str(peers, "utf-8").split(',')[:-1]
def send_message(self):
try:
self.s.send("req".encode('utf-8'))
# check if the user wants to quit the connection
#if data[0:1].lower() == "q":
# self.send_disconnect_signal()
except KeyboardInterrupt as e:
self.send_disconnect_signal()
return
def send_disconnect_signal(self):
print("Disconnected from server")
self.s.send("q".encode('utf-8'))
sys.exit()