文件服务器上传 Python

File Server Upload Python

文件服务器下载问题Python 2.5.1

所以我正在将文件服务器作为一个业余项目。不过我遇到了一些问题。我可以使用客户端成功将文件上传到服务器,但说要上传的文件是 50,000 字节(50 MB),它只会上传 49,945 字节,然后如果我尝试打开它,它说它已损坏。如果我关闭服务器,它会变为 50,000 然后工作。有没有办法在不需要关闭和重新打开服务器的情况下解决这个问题?

(下载没有这个问题)

完整客户端代码: Client

完整服务器: Server

客户端上传功能:

def Uploader(s):
    IsReal = True
    data = "UploaderReady"
    if data == "UploaderReady":
        List = []
        FilePath = dir_path = os.path.dirname(os.path.realpath(__file__))
        List.append(os.listdir(FilePath))
        FileUpload = raw_input("Pick a file? -> ")
        for Item in List:
            if FileUpload == Item:
                IsReal = True #checks if item exists
        if IsReal == True:
            File = open(FileUpload,'rb')
            bytestosend = File.read(1024)
            FileSize = os.path.getsize(FileUpload)
            s.send(FileUpload)
            s.send(str(FileSize))
            s.send(bytestosend)
            while bytestosend != "":
                bytestosend = File.read(8192)
                s.send(bytestosend)
            print"Processing"
            File.close()
            time.sleep(1.5)
            s.send("COMPLETE")
            print"File Successfully Uploaded"
            time.sleep(2)
            print"    \n    " * 10
            Main()
        if IsReal == "False":
            print"Item doesn't Exist"
            time.sleep(2)
            print"    \n    " * 10
            s.close()
            Main()

服务器上传功能:

Todo = sock.recv(1024)
if Todo == "U":
    print str(addr)+" Uploading"
    UploadingThread = threading.Thread(target=Uploader,args=(c,c,))
    UploadingThread.start()

def Uploader(c,s):
    filename = s.recv(1024)
    filesize = s.recv(1024)
    f = open(filename,'wb')
    totalRecv = 0
    while totalRecv < filesize:
        FileContent = s.recv(8192)
        totalRecv += len(FileContent)
        f.write(FileContent)
    print"Download Complete"
    f.close()
    s.close()

您在服务器端关闭了客户端连接,但永远不会像Cory Shay所说的那样在客户端关闭它。
不过,您不需要关闭它,而是需要 shutdown 套接字并用 s.shutdown(socket.SHUT_WR)

表示它已完成写入

这是寻找客户的方式:

def Uploader(s):
    IsReal = True
    data = "UploaderReady"
    if data == "UploaderReady":
        List = []
        FilePath = dir_path = os.path.dirname(os.path.realpath(__file__))
        List.append(os.listdir(FilePath))
        FileUpload = raw_input("Pick a file? -> ")
        for Item in List:
            if FileUpload == Item:
                IsReal = True #checks if item exists
        if IsReal == True:
            File = open(FileUpload,'rb')
            bytestosend = File.read(1024)
            FileSize = os.path.getsize(FileUpload)
            s.send(FileUpload)
            s.send(str(FileSize))
            s.send(bytestosend)
            while bytestosend != "":
                bytestosend = File.read(8192)
                s.send(bytestosend)
                print"Processing"
            s.shutdown(socket.SHUT_WR) # End the writing stream
            print(s.recv(1024)) # Expecting the server to say 'upload complete'
            s.close() # close the socket
            File.close()
            time.sleep(1.5)
            s.send("COMPLETE")
            s.close() #To close connection after uploading
            print"File Successfully Uploaded"
            time.sleep(2)
            print"    \n    " * 10
            Main()

和服务器:

def Uploader(c,s):
    filename = s.recv(1024)
    filesize = s.recv(1024)
    f = open(filename,'wb')
    totalRecv = 0
    while totalRecv < filesize:
        FileContent = s.recv(8192)
        totalRecv += len(FileContent)
        f.write(FileContent)
    s.send("Upload Complete!") # Tell client the upload is complete
    print"Download Complete"
    f.close()
    s.close() # Close the socket

此外,您正在向服务器传递 Uploader 2 个相同的参数,并且只使用一个,而您应该只传递一个:

UploadingThread = threading.Thread(target=Uploader,args=(c,c,))
# should be
UploadingThread = threading.Thread(target=Uploader,args=(c,))

同样,你的密码线程只需要2:

c, addr = s.accept()
print"Client Connection: <"+str(addr)+">"
PasswordThread = threading.Thread(target=Password,args=(c,addr))

def Password(c,addr):
    c.send("WAITINGPASSWORD")
    PASSWORD = "123"
    password = c.recv(1024)

你的密码查询功能可以更简单:

def Password(c,addr):
    password = "123"
    c.send("WAITINGPASSWORD")
    attempt = c.recv(1024)[::-1]
    if attempt == password:
        doStuff()