当前位置: 动力学知识库 > 问答 > 编程问答 >

python - File transferring, File can't be opend after transfer

问题描述:

I am currently working on a file transfer server and ran into

a problem. I am able to transfer the file completly and it works perefect,

But when the client that received the file cannot open it through python.

What I mean is that if I transferr a file, I can see it in the dic of the client the received it, but it cannot open it and I get an:IOError that the file doesn't exist.

The server:

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()

The sender client, Just the relevant part:

 print "connection from:", addr

# From here on, the server works by the File-Transfer RFC.

# Accepting number of retries from the server and the name of the file.

data = clientsock.recv(self.BUFSIZ)

tries, name = data.split(',')

# Opening File.

f = file(name, mode='rb')

if not tries:

clientsock.close()

continue

try:

for i in range(int(tries)):

# Sending the file size.

clientsock.send('Start,'+str(self.file_size(name)))

# Accepting an ok.

ok = clientsock.recv(self.BUFSIZ)

if not ok:

continue

if ok == "ok":

# Sending the file.

clientsock.send(f.read(-1))

# Waiting for confirmation.

ok = clientsock.recv(self.BUFSIZ)

if ok == "ok":

break

else:

continue

f.close()

except IOError as e:

f.close()

print e

# An error has occurred, closing the socket.

#clientsock.send('End,None')

clientsock.close()

The recieve Client:

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

Any Help is welcomed!

分享给朋友:
您可能感兴趣的文章:
随机阅读: