multiple send from server to client python

限于喜欢 提交于 2019-11-26 23:47:24

问题


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)

回答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.



回答2:


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.



来源:https://stackoverflow.com/questions/30615187/multiple-send-from-server-to-client-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!