Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

file - multiple send from server to client python

First I am sending the requested file from the server to the client and after that I want to send the computed sha of the file from the server to the client, so that the client can check if both the sha from the sent and the received files are the same.

I manage to send the file but when I try to also send the sha (which is a variable) I receive a error ( i believe that the sha is also added to the file content)

How can i send them separately?

if (reqCommand == 'get'):
    with open (reqFile, 'rb') as in_file, open(encFile, "wb") as out_file:
        encrypt(in_file, out_file, "abc")
        f = open(encFile,'rb')
        for data in f:
            # print 'here3'
            conn.sendall(data)
        f.close()

        file_sh = hashfile(reqFile)
        print 'the sha1 function from the server: ', file_sh
        conn.send(file_sh)

and the client:

while True:
    data = sock.recv(1024)

    if not data:
        break
    #print data
    file_to_write.write(data)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should redesign a bit how your app works:

  • First the servers sends to the client the file size
  • The client reads the file size (converts it to a number) and notifies the server (sends "OK" to the server for example)
  • The server reads the "OK" from the client and starts to send the file contents (preferably in smaller chunks)
  • The client keeps reading data until either it reads exactly "file size" bytes or error occurs
  • If no error occurred the client computes the hash of the file that it just received and sends it to the server
  • The server reads the hash from client and compares with the one of its local file - if they match it sends "OK" to the client "ERROR" otherwise
  • The client reads the response from server: if "ERROR" is received the file is deleted

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...