示例#1
0
    def bulletin_board(self, sock_c: socket.socket, addr: str) -> None:
        req_json = sock_c.recv(self.buf_size).decode('utf-8')
        print(req_json, type(req_json))
        req_dict = json.loads(req_json)
        mode = req_dict['mode']

        # メッセージ投稿
        if mode == 'post':
            post_json = req_dict['json']
            message = Message(post_json)

            with open('log/log.json', 'r') as f:
                log_dict = json.load(f)

            log_dict['log'].append(message.to_dict(is_have_secret=True))

            with open('log/log.json', 'w') as f:
                json.dump(log_dict, f, indent=4)

            return

        # ログ呼び出し
        if mode == 'read':
            with open('log/log.json', 'r') as f:
                log_dict = json.load(f)

            # クライアントに送るべきでないデータを除去
            log_dict['log'] = [
                Message(json.dumps(log)).to_dict(is_have_secret=False)
                for log in log_dict['log']
            ]
            print(log_dict)
            sock_c.send(json.dumps(log_dict).encode('utf-8'))

            return

        if mode == 'delete':
            post_dict = req_dict['json']
            target_id, password = post_dict['target_id'], post_dict['password']

            with open('log/log.json', 'r') as f:
                log_dict = json.load(f)
            log_dict['log'] = [
                Message(json.dumps(log)).to_dict(is_have_secret=True)
                for log in log_dict['log']
            ]

            target_idx = [
                i for i, message in enumerate(log_dict['log'])
                if message['unique_id'] == target_id and message['password'] ==
                sha256_add(password, message['date'])
            ]

            print(log_dict)
            log_dict['log'].pop(target_idx[0])

            with open('log/log.json', 'w') as f:
                json.dump(log_dict, f, indent=4)

            return

        sock_c.close()
示例#2
0
 def post(self, message: Message) -> None:
     req = {'mode': 'post', 'json': message.to_dict(is_have_secret=True)}
     self.sock.send(json.dumps(req).encode('utf-8'))