【问题标题】:How to parse data received through IPFS Python API如何解析通过 IPFS Python API 接收的数据
【发布时间】:2019-07-25 15:17:40
【问题描述】:

我想用基于 IPFS 的 Python 做一个聊天室。 我尝试使用 pubsub 发送和接收信息。

import ipfsapi;
api=ipfsapi.connect("127.0.0.1",5001);
with api.pubsub_sub("topic",discover=True) as sub:
    for message in sub:
        print(message);

收到的数据看起来像

{'from': 'EiDByeJhC7IHqQ2x2G+uGiFSSj1WUeNDp11qpwYDap5rNw==', 'data': 'dGVzdCBwdWIgbW9kZQ==', 'seqno': 'FYijZ/yJJXw=', 'topicIDs': ['topic']}

不知道如何解析源IPFS地址和接收到的数据?

【问题讨论】:

    标签: python publish-subscribe ipfs


    【解决方案1】:

    base64,需要解码:

    import base64
    # This is and example of decoding your message['data']
    coded_string = "dGVzdCBwdWIgbW9kZQ=="
    base64.b64decode(coded_string)  # b'test pub mode'
    

    【讨论】:

    【解决方案2】:

    它确实是常规的 Base64 编码 (try with this online decoder),但不要(像我一样)在“from”字段的解码上绊倒,以为它无法解码。 'from' 的结果似乎是二进制的,不会解码为任何人类可读的内容(除非我弄错了)。另请参阅 Rust 客户端中的 this issue,了解其他苦苦挣扎的伙伴的 cmets。

    【讨论】:

      猜你喜欢
      • 2017-12-13
      • 1970-01-01
      • 1970-01-01
      • 2018-10-19
      • 2020-09-29
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多