【问题标题】:File Server Upload Python文件服务器上传 Python
【发布时间】:2016-12-18 19:37:07
【问题描述】:

文件服务器下载问题 Python 2.5.1

所以我正在开发一个文件服务器作为一个爱好项目。我有一些问题。我可以使用客户端成功地将文件上传到服务器,但是说要上传的文件是 50,000 字节(50 mbs),它只会上传 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()

【问题讨论】:

  • 您尝试过使用较小的文件吗?
  • 在客户端完成上传后尝试关闭套接字。

标签: python file upload server client


【解决方案1】:

您在服务器端关闭客户端连接,但永远不要像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()

【讨论】:

  • 感谢 Xander Luciano。它几乎奏效了。但它卡在 s.send("Upload Complete!") 这可能是因为我在 python 2.5.1 而不是 python 3 上。你问“你试过更小的文件吗?”我确实喜欢 500 kbs 它不起作用我仍然需要关闭服务器。如果你能有一个修改过的答案,那就太好了。
  • 如果这个答案有帮助,你应该投票或接受这个答案。 4 天内没有发布其他答案,因此如果您有其他问题,可以考虑发布一个新问题。 @Rogocraft
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
  • 2020-06-28
  • 2012-03-19
  • 2010-10-15
  • 1970-01-01
相关资源
最近更新 更多