0

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)

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

could you give me a short example please? i am still facing issues
where exactly are you having problems?i am not sure how to write code in comments.
Got to run, i'll probably take a look later or tomorrow.
i've eventually solved the problem. thank you for detailing the steps
0

A TCP stream needs to be formatted in both ends, since it is a stream not packets. I suppose you could look for a nullbyte ('\x00') which should signal end of file.

2 Comments

Binary files often have \0s in their content.
I like @CristiFati's soltution. Send the length of the file, read that number of bytes then you know you're done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.