-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcp_server.py
56 lines (48 loc) · 1.06 KB
/
tcp_server.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
import socket
import os
from _thread import *
import pickle
def index_peers(files_data,address):
l=list()
k=files_data.split('\n')
m=k[:-1]
n=k[-1]
l.append(n)
l.append(m)
return l
# Code begins here
all_files=list() # stores list of peers with file details
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM,0)
host="159.65.148.201"
port=9999
s.bind((host,port))
print("waiting..."+str(host))
s.listen(5)
flag=0
while True:
conn,addr=s.accept()
print("connected "+str(addr))
conn.sendall(b"Index Server wants File list")
files_data=conn.recv(1024).decode()
received_files=index_peers(files_data,addr[0])
ip=received_files[0]
upgrade_flag=1
if(flag):
print(ip,all_files[0][0])
for i,j in enumerate(all_files):
if(str(all_files[i][0]) == str(ip)):
print("True")
del all_files[i]
all_files.append(received_files)
upgrade_flag=0
break
if(upgrade_flag):
all_files.append(received_files)
flag=1
for i in all_files:
print(i)
data=pickle.dumps(all_files)
conn.sendall(data)
print("Sent data to peer")
conn.close()
s.close()