从服务器到客户端python的多个发送 [英] multiple send from server to client python

查看:306
本文介绍了从服务器到客户端python的多个发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我将请求的文件从服务器发送到客户端,然后再将文件的计算出的sha信息从服务器发送到客户端,以便客户端可以检查是否已发送的sha信息和接收的文件是相同的.

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.

我设法发送文件,但是当我也尝试发送sha(这是一个变量)时,我收到一个错误消息(我相信sha也已添加到文件内容中)

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)

我如何分别发送它们?

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)

和客户:

while True:
    data = sock.recv(1024)

    if not data:
        break
    #print data
    file_to_write.write(data)

推荐答案

您应该重新设计应用程序的工作方式:

You should redesign a bit how your app works:

  • 首先,服务器将文件大小发送给客户端
  • 客户端读取文件大小(将其转换为数字)并通知服务器(例如,将" OK "发送到服务器)
  • 服务器从客户端读取"确定",并开始发送文件内容(最好以较小的块为单位)
  • 客户端一直读取数据,直到它精确地读取了"文件大小"字节或发生错误为止
  • 如果未发生错误,则客户端将计算刚刚接收到的文件的哈希并将其发送到服务器
  • 服务器从客户端读取哈希值,并将其与本地文件之一进行比较-如果它们匹配,它将向客户端发送" OK "" ERROR ",否则
  • 客户端从服务器读取响应:如果收到" ERROR ",则文件被删除
  • 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

这篇关于从服务器到客户端python的多个发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆