【发布时间】: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