Napster-style peer-to-peer (P2P) file sharing system using rpyc and message-orianted(Python)

579 Views Asked by At

I have a task in which I should make Napster-style peer-to-peer (P2P) file sharing system. I used rpyc and message-oriented at the same time, but I have a problem when I download a file from other peer - the code just runs infinite and never stops, no output.

Peer has two classes Client and server

from socket   import *
import socket
import os
import pickle
import rpyc
from rpyc.utils.server import ThreadedServer
from const import *

class Client():

conn = rpyc.connect(HOST, PORT)  # Connect to the index_server

def lookUp(self,filename):
    PeerList = self.conn.root.exposed_search(filename)
    if PeerList==False:
        print "no File with this Name"
    else:
        print PeerList

def register_on_server(self,Filename,port):
    self.conn.root.exposed_register(Filename,port)

def download(self, serverhost, serverport, filename):  # function download a file from another peer
        sock.connect((serverhost,serverport))
        print("Client Connected to download a file")
        sock.send(pickle.dumps(filename))
        localpath = "C:\Users\aa\PycharmProjects\task1\downloadfiles"
        data = sock.recv(1024)
        totalRecv = len(data)
        f = open(localpath + '/' + filename, 'wb')
        f.write(data)
        filesize = os.path.getsize('C:\Users\aa\PycharmProjects\task1\uploadfiles' + '/' + filename)
        while totalRecv < filesize:
            data = sock.recv(1024)
            totalRecv += len(data)
            f.write(data)
            print("File is downloaded Successfully")
        sock.close()

class Server(rpyc.Service):

    def __init__(self, host, port):
        self.host = host
        self.port = port  # the port it will listen to
        global sock
        sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)  # socket for incoming calls
        sock.bind((self.host, self.port))  # bind socket to an address
        sock.listen(5)  # max num connections
    def obtain(self):
        remotepath = "C:\Users\aa\PycharmProjects\task1\uploadfiles"
        while True:
            client, address = sock.accept()
            print("Client Connected to download a file")
            try:
                filename = client.recv(1024)
                if os.path.exists(remotepath + '/' + filename):
                    filesize = os.path.getsize(remotepath + '/' + filename)
                    if filesize > 0:
                        client.send(str(filesize))
                        with open(remotepath + '/' + filename, 'rb') as f:
                            bytes = f.read(1024)
                            client.send(bytes)
                            while bytes != "":
                                bytes = f.read(1024)
                                client.send(bytes)
                    else:
                        client.send("Empty")
                else:
                    client.send("False")
            except:
                client.close()
                return False

if __name__ == "__Server__":
         server = ThreadedServer(Server, hostname=Server.host, port=Server.port)
         server.start()




{Peer2}
from time import sleep
import rpyc
from peer import *
from const import *

peer2 = Client()
print ('1-register')
print ('2-search')
print ('3-download')
while(True):
 commend = raw_input("enter your commend")
 if commend == 'register':
    filename = raw_input("write the file name")
    peer2.register_on_server(filename,PeeR2PORT)
 elif commend == 'search':
    filename = raw_input("write the file name")
    peer2.lookUp(filename)
 elif commend == 'download':
     port = raw_input("enter the other peer port")
     host = raw_input("enter the other peer host")
     filename = raw_input("enter the file name")
     peer1 = Server(PeeR1HOST, PeeR1PORT)
     peer1.obtain()
     peer2.download(host, port, filename)
1

There are 1 best solutions below

2
On

You create a call to peer1.obtain() which runs the peer to accept calls from different peers to download the file. However, you try to call peer1.download() from the same peer while it is already listening for incoming calls. You need to separate peer1.download() to run from different peer.

You need to revise how Napster FileSharing System works.

We are not here to solve your assignment. You seem to have good knowledge with python, the issue is that you do not understand the task good enough. We can help you with understanding its concept, helping you with syntax errors,..,etc.