使用私有IPFS網絡測試 IPFS Python Client
1. 部署私有IPFS網絡
-
建立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
- docker-compose up -d
- 隨便進入一個node進行測試,dokcer-compose exec ipfs-node1 sh
- 使用 ipfs swarm peers 查看連接狀態
- echo ‘This is node1!’ > node1.txt
- ipfs add node1.txt 得到hash值 xxx
- ipfs cat "node1.txt hash xxx" 查看文件
2. 使用 IPFS Python Client 測試
- pip install ipfsapi
- python 測試:
- import ipfsapi
- api = ipfsapi.connect('xxx.xxx.xxx.xxx', 9001)
- api.cat("node1.txt hash xxx")
- api.add("hello.txt")
- api.id() # 查看 id 信息等
- api.get("node1.txt hash xxx") # 下載文件
- api.swarm.peers() # 查看連接狀態