Python 文件传输,文件可以';转机后不能打开
我目前在一个文件传输服务器上工作,遇到了 一个问题。我能够完整地传输文件,并且工作正常, 但是当接收文件的客户端无法通过python打开它时 我的意思是,如果我传输一个文件,我可以在收到它的客户机的dic中看到它,但它无法打开它,我得到一个:IOError,该文件不存在 服务器:Python 文件传输,文件可以';转机后不能打开,python,file,sockets,tcp,Python,File,Sockets,Tcp,我目前在一个文件传输服务器上工作,遇到了 一个问题。我能够完整地传输文件,并且工作正常, 但是当接收文件的客户端无法通过python打开它时 我的意思是,如果我传输一个文件,我可以在收到它的客户机的dic中看到它,但它无法打开它,我得到一个:IOError,该文件不存在 服务器: def download_manager(self, sock, ADDR, name): sock.send('Starting file download: '+name) # Getting th
def download_manager(self, sock, ADDR, name):
sock.send('Starting file download: '+name)
# Getting the socket that has the file
# Getting the user ip address
BUFSIZ = 1024
fileSock = socket(AF_INET, SOCK_STREAM)
fileSock.connect(ADDR)
print 'connected;'
# Starting the file request protocol.
tries = "2"
# sending number of retries.
fileSock.send(tries + "," + name)
sock.send(tries)
for i in range(int(tries)):
# Accepting the start message.
data, size = fileSock.recv(BUFSIZ).split(',')
sock.send(size)
size = int(size)
if data == 'Start':
fileSock.send('ok')
# Getting first data from the supplier.
data = fileSock.recv(BUFSIZ)
# Sending the data to the request client.
sock.send(data)
current_size = BUFSIZ
while current_size <= size:
# Getting data from the supplier.
data = fileSock.recv(BUFSIZ)
# The server "sleeps" in order to keep a synchronization between the client and the server.
# The client works slower than the server(It needs to save the file as well.
time.sleep(0.0001)
# Sending the data to the request client.
sock.send(data)
current_size += BUFSIZ
print current_size
# Receive for the request client the end message.
#sock.send(data)
data = sock.recv(1024)
if data == 'ok':
fileSock.send(data)
break
else:
fileSock.send(data)
else:
sock.send("wrong")
fileSock.close()
接收客户端:
def get_file(self, name):
"""
This function receives and saves the requested file from the server.
:param name: The name of the file( How it is saved )
:return: None.
"""
name = name.split('.')
tries = int(self.sock.recv(self.BUFSIZ))
progress = 0
for i in range(tries):
f = file(name[0] + '.' + name[1], mode='wb')
final_size = int(self.sock.recv(self.BUFSIZ))
data = self.sock.recv(self.BUFSIZ)
f.write(data)
current_size = self.BUFSIZ
while current_size <= final_size:
progress = (float(current_size)/final_size)
if progress > 0.01:
self.app.progress = progress
data = self.sock.recv(self.BUFSIZ)
f.write(data)
current_size += self.BUFSIZ
f.close()
print "Current: " + str(current_size)
print "real: " + str(self.file_size(name[0] + '.' + name[1]))
print "Wanted: " + str(final_size)
self.app.progress = None
if self.file_size(name[0] + '.' + name[1]) == final_size:
print 'ok'
self.sock.send('ok')
break
else:
print 'Bad'
os.remove(name[0] + '.' + name[1])
self.sock.send('Bad')
continue
def get_文件(self,name):
"""
此函数用于从服务器接收并保存请求的文件。
:param name:文件名(如何保存)
:返回:无。
"""
name=name.split(“.”)
trys=int(self.sock.recv(self.BUFSIZ))
进度=0
对于范围内的i(尝试):
f=文件(名称[0]+'.+name[1],模式='wb')
最终尺寸=整数(self.sock.recv(self.BUFSIZ))
数据=self.sock.recv(self.BUFSIZ)
f、 写入(数据)
当前大小=self.BUFSIZ
当前_大小为0.01时:
self.app.progress=进度
数据=self.sock.recv(self.BUFSIZ)
f、 写入(数据)
当前_大小+=self.BUFSIZ
f、 关闭()
打印“当前:”+str(当前大小)
打印“real:”+str(self.file_size(名称[0]+'.+name[1]))
打印“需要:”+str(最终尺寸)
self.app.progress=无
如果self.file_size(名称[0]+'.+name[1])==最终_大小:
打印“ok”
self.sock.send('ok')
打破
其他:
打印“坏”
os.remove(名称[0]+'.+名称[1])
self.sock.send('Bad')
持续
欢迎任何帮助
def get_file(self, name):
"""
This function receives and saves the requested file from the server.
:param name: The name of the file( How it is saved )
:return: None.
"""
name = name.split('.')
tries = int(self.sock.recv(self.BUFSIZ))
progress = 0
for i in range(tries):
f = file(name[0] + '.' + name[1], mode='wb')
final_size = int(self.sock.recv(self.BUFSIZ))
data = self.sock.recv(self.BUFSIZ)
f.write(data)
current_size = self.BUFSIZ
while current_size <= final_size:
progress = (float(current_size)/final_size)
if progress > 0.01:
self.app.progress = progress
data = self.sock.recv(self.BUFSIZ)
f.write(data)
current_size += self.BUFSIZ
f.close()
print "Current: " + str(current_size)
print "real: " + str(self.file_size(name[0] + '.' + name[1]))
print "Wanted: " + str(final_size)
self.app.progress = None
if self.file_size(name[0] + '.' + name[1]) == final_size:
print 'ok'
self.sock.send('ok')
break
else:
print 'Bad'
os.remove(name[0] + '.' + name[1])
self.sock.send('Bad')
continue