文件服务器上载Python
文件服务器下载问题Python 2.5.1 因此,我在一个文件服务器上工作作为一个爱好项目。不过我有一些问题。我可以使用客户端成功地将文件上载到服务器,但如果要上载的文件是50000字节(50 mbs),它将只上载49945字节,如果我尝试打开它,它会说它已损坏。如果我关闭服务器,它将变为50000然后工作。有没有一种方法可以在不需要关闭和重新打开服务器的情况下修复此问题 (下载没有这个问题) 完整客户端代码: 完整服务器: 客户端上传功能:文件服务器上载Python,python,file,upload,server,client,Python,File,Upload,Server,Client,文件服务器下载问题Python 2.5.1 因此,我在一个文件服务器上工作作为一个爱好项目。不过我有一些问题。我可以使用客户端成功地将文件上载到服务器,但如果要上载的文件是50000字节(50 mbs),它将只上载49945字节,如果我尝试打开它,它会说它已损坏。如果我关闭服务器,它将变为50000然后工作。有没有一种方法可以在不需要关闭和重新打开服务器的情况下修复此问题 (下载没有这个问题) 完整客户端代码: 完整服务器: 客户端上传功能: def Uploader(s): IsR
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()
服务器上传功能:
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()
Todo=sock.recv(1024)
如果Todo==“U”:
打印str(地址)+“上传”
UploadingThread=threading.Thread(目标=Uploader,args=(c,c,))
UploadingThread.start()
def上传器(c、s):
filename=s.recv(1024)
filesize=s.recv(1024)
f=打开(文件名为“wb”)
totalRecv=0
totalRecv<文件大小:
FileContent=s.recv(8192)
totalRecv+=len(文件内容)
f、 写入(文件内容)
打印“下载完成”
f、 关闭()
s、 关闭()
您可以关闭服务器端的客户端连接,但决不能像上面所说的那样关闭客户端连接。但是,您需要
关闭
套接字并发出完成写入的信号,而不是关闭它
以下是它应该如何查找客户端:
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
同样,您的密码线程只需要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)
您是否尝试过使用较小的文件?在客户端完成上载后,请尝试关闭套接字。谢谢Xander Luciano。它几乎成功了。但是它在s.send(“上传完成!”)时被卡住了,这可能是因为我使用的是python 2.5.1,而不是python 3。你问我“你试过更小的文件吗?”我试了500 kbs,但没用,我仍然需要关闭服务器。如果你能有一个修改过的答案那就太好了。如果这个答案有用,你应该投票或者接受这个答案。在4天内没有其他的答案,所以如果你有更多的问题,可以考虑发表一个新的问题。罗戈克拉夫特