使用私有IPFS網絡測試 IPFS Python Client

閱讀原文

使用私有IPFS網絡測試 IPFS Python Client

1. 部署私有IPFS網絡

  1. 建立3個節點的IPFS網絡,在 docker-compose.yml 文件中寫入下面內容:

    version: '3'
    services:
    ipfs-node1:
      image: ipfs/go-ipfs
      restart: unless-stopped
      ports:
       - "7000:4001"
       - "9000:5001"
       - "8080:8080"
      volumes:
       - ./node1:/home
    
    ipfs-node2:
      image: ipfs/go-ipfs
      restart: unless-stopped
      ports:
       - "7001:4001"
       - "9001:5001"
       - "8081:8080"
      volumes:
       - ./node2:/home
    
    ipfs-node3:
      image: ipfs/go-ipfs
      restart: unless-stopped
      ports:
       - "7002:4001"
       - "9002:5001"
       - "8082:8080"
      volumes:
       - ./node3:/home
  2. docker-compose up -d
  3. 隨便進入一個node進行測試,dokcer-compose exec ipfs-node1 sh
  4. 使用 ipfs swarm peers 查看連接狀態
  5. echo ‘This is node1!’ > node1.txt
  6. ipfs add node1.txt 得到hash值 xxx
  7. ipfs cat "node1.txt hash xxx" 查看文件

2. 使用 IPFS Python Client 測試

  1. pip install ipfsapi
  2. python 測試:
  3. import ipfsapi
  4. api = ipfsapi.connect('xxx.xxx.xxx.xxx', 9001)
  5. api.cat("node1.txt hash xxx")
  6. api.add("hello.txt")
  7. api.id() # 查看 id 信息等
  8. api.get("node1.txt hash xxx") # 下載文件
  9. api.swarm.peers() # 查看連接狀態
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章