使用rpyc和message-orianted(Python)的Napster式对等(P2P)文件共享系统

时间:2017-12-16 12:45:05

标签: python p2p rpyc

我有一个任务,我应该制作Napster式的点对点(P2P)文件共享系统。我同时使用了rpyc和面向消息,但是当我从其他对等端下载文件时我遇到了问题 - 代码运行无限且永不停止,没有输出。

Peer有两个类Client和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()

类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 个答案:

答案 0 :(得分:0)

您创建对peer1.obtain()的调用,该调用运行对等方以接受来自不同对等方的调用以下载文件。但是,当它已经在侦听来电时,您尝试从同一个对等方呼叫peer1.download()。您需要将peer1.download()与不同的同行分开运行。

您需要修改 Napster FileSharing系统的工作原理。

我们不是来解决你的任务。你似乎对python有很好的了解,问题是你不能很好地理解任务。我们可以帮助您理解其概念,帮助您解决语法错误,......等。