使用私有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() # 查看连接状态