Esempio n. 1
0
    def _sendrq1(self, filepath):
        """文件开始上传请求"""
        t = message.Taskinfo()
        t.sgw_ip = message.ipv4_to_int(self.sgw_addr)
        t.file_md5 = utils.calcmd5(filepath).encode("utf-8")
        import os
        sendpath = filepath.replace(os.sep, "/")
        t.file_name = sendpath.encode("utf-8")
        T = t.pack()

        # header
        h = message.Header()
        h.command = 0x00020001
        h.total_size = hdrlen + tsklen
        import os
        h.total = os.stat(filepath).st_size
        h.offset = 0
        h.count = 1
        H = h.pack()

        # entire packet
        pkt = H + T

        # send
        self.sock.sendall(pkt)
Esempio n. 2
0
def sendrq1(sock):
    """发送获取检查列表请求"""
    args_unpack = dict()
    args_unpack["timestamp_before"] = int(time.time())
    args_unpack["agent_id"] = 7777
    args_unpack["nr_study"] = 10
    args_unpack["study_state"] = 0
    args_unpack["nr_files"] = 0
    args_unpack["study_id"] = "useless_study_id"

    args_pack = json.dumps(args_unpack).encode("utf-8")
    args_size = len(args_pack)
    head_size = config.Constant.HEAD_LENGTH

    # header
    h = message.Header()
    h.command = config.Constant.CLIENT_QUERY_STUDY_LIST
    h.total_size = head_size + args_size
    header_pack = h.pack()

    # entire packet
    pkt = header_pack + args_pack

    # send
    sock.sendall(pkt)
Esempio n. 3
0
 def _sendrq3(self):
     """文件结束上传请求"""
     h = message.Header()
     h.command = 0x00020005
     h.total_size = hdrlen
     H = h.pack()
     self.sock.sendall(H)
Esempio n. 4
0
 def _sendrq12(self):
     """发送文件结束下载请求"""
     h = message.Header()
     h.command = 0x0002000B
     h.total_size = hdrlen
     H = h.pack()
     self.sock.sendall(H)
Esempio n. 5
0
    def generate_header(self, command):
        command_ = command
        version = b"\x01"

        header = message.Header(command_, version, self.PADDING_2)

        return header
    def run(self):
        """ Run the handler thread """

        recv_header = self.received_message.header

        try:
            questions = self.received_message.questions[0]
            qd_count = len(questions)
        except IndexError:
            return

        # if recv_header.rd:
        try:
            resolver = Resolver(True)
            answer_sname, addresses, aliasses = resolver.gethostbyname(questions[0].rdata.data)
        except ResolverException as e:
            print e
            return

        answers = list()
        for address in addresses:
            answer = message.Section(answer_sname, Type.A, Class.IN, 86400, len(address), address)
            answers.append(answer)

        send_header = message.Header(recv_header.ident, 0, qd_count, len(addresses), 0, 0)
        send_header.qr = 1
        send_header.opcode = 0
        send_header.rd = recv_header.rd
        response_message = message.Message(send_header, questions, answers)
        response_bytes = response_message.to_bytes()
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.settimeout(3)
        sock.send(response_bytes, (self.ip_address, 53))
Esempio n. 7
0
 def _sendrq11(self, offset, count):
     """发送下载文件内容请求,从偏移 offset 处下载 count 个字节"""
     # header
     h = message.Header()
     h.command = 0x00020009
     h.offset = offset
     h.count = count
     h.total_size = hdrlen
     H = h.pack()
     # send
     self.sock.sendall(H)
Esempio n. 8
0
 def _sendrq2(self, offset, block):
     """文件内容上传请求"""
     blocklen = len(block)
     h = message.Header()
     h.command = 0x00020003
     h.total_size = hdrlen + blocklen
     h.offset = offset
     h.count = blocklen
     H = h.pack()
     pkt = H + block
     self.sock.sendall(pkt)
Esempio n. 9
0
    def _sendrq9(self, studyid):
        """发送获取 studyid 列表请求"""
        h = message.Header()
        h.command = 0x0002000F
        h.total_size = hdrlen + tsklen
        H = h.pack()

        t = message.Taskinfo()
        t.file_name = studyid.encode("utf-8")
        T = t.pack()

        pkt = H + T
        self.sock.sendall(pkt)
Esempio n. 10
0
    def _sendrq4(self, filepath):
        """顺序下载文件请求"""
        h = message.Header()
        h.command = 0x00020011
        h.total_size = hdrlen + tsklen
        H = h.pack()

        t = message.Taskinfo()
        t.file_name = filepath.encode("utf-8")
        T = t.pack()

        pkt = H + T
        self.sock.sendall(pkt)
Esempio n. 11
0
    def _sendrq8(self, delete_list):
        """删除客户端中 studyid 下文件的通知请求"""
        x = dict()
        x["nr_delete"] = len(delete_list)
        x["delete_list"] = delete_list
        X = json.dumps(x).encode("utf-8")

        h = message.Header()
        h.command = 0x00000024
        h.total_size = 64 + len(X)
        h.src_id = self.agent_id
        H = h.pack()

        pkt = H + X
        self.sock.sendall(pkt)
Esempio n. 12
0
    def _sendrq7(self, finish_list):
        """上传 studyid 成功通知请求"""
        x = dict()
        x["nr_upload"] = len(finish_list)
        x["upload_list"] = finish_list
        X = json.dumps(x).encode("utf-8")

        h = message.Header()
        h.command = 0x00000026
        h.total_size = 64 + len(X)
        h.src_id = self.agent_id
        H = h.pack()

        pkt = H + X
        self.sock.sendall(pkt)
Esempio n. 13
0
    def _sendrq10(self, filepath):
        """发送文件开始下载请求"""
        # taskinfo
        t = message.Taskinfo()
        t.sgw_ip = message.ipv4_to_int(self.sgw_addr)
        t.file_name = filepath.encode("utf-8")
        T = t.pack()

        # header
        h = message.Header()
        h.command = 0x00020007
        h.total_size = hdrlen + tsklen
        H = h.pack()

        # send
        self.sock.sendall(H + T)
Esempio n. 14
0
    def _sendrq5(self, study_state):
        """查询 studyid 列表请求"""
        x = dict()
        x["timestamp_before"] = int(time.time())
        x["study_state"] = study_state
        x["agent_id"] = self.agent_id
        x["nr_study"] = self.nr_study
        x["study_id"] = "from-test-program"
        x["nr_files"] = 0
        X = json.dumps(x).encode("utf-8")

        h = message.Header()
        h.command = 0x00000010
        h.total_size = 64 + len(X)
        h.src_id = self.agent_id
        H = h.pack()

        pkt = H + X
        self.sock.sendall(pkt)
Esempio n. 15
0
    def _sendrq6(self, study_state):
        """查询 studyid 列表请求(版本 2)"""
        x = dict()
        dt = datetime.datetime.strptime("2010-01-01 00:00:00",
                                        "%Y-%m-%d %H:%M:%S")
        x["agent_id"] = self.agent_id
        x["timestamp_beg"] = int(dt.timestamp())
        x["timestamp_end"] = int(time.time())
        x["study_state"] = study_state
        x["nr_study"] = self.nr_study
        X = json.dumps(x).encode("utf-8")

        h = message.Header()
        h.command = 0x00000028
        h.total_size = 64 + len(X)
        h.src_id = self.agent_id
        H = h.pack()

        pkt = H + X
        self.sock.sendall(pkt)