IPFS 服务的Python访问
py-ipfs-api提供python api对IPFS服务通过http gateway进行访问,需要运行一个本地的IPFS daemon。https://github.com/ipfs/py-ipfs 是IPFS的纯Python语言实现,本身就具有IPFS Daemon功能。
- 项目源码:https://github.com/ipfs/py-ipfs-api
- IPFS Daemon部署参考:
查看 the client API reference,获取完整的命令参考。
重要: The py-ipfs-api PIP package 和 Python module 已经都更名为 ipfsapi (没有横线, 小写的 a)。
原来的 ipfs-api/ipfsApi package/module 只用于 IPFS 0.3.x 和 Python 2,已经过时。请更新-Please upgrade!
Note: 该库尽可能保持 IPFS HTTP API的兼容性。目前,经过测试 go-ipfs v0.4.10。如果与 go-ipfs的其它版本遇到兼容性问题,可以到https://github.com/ipfs/py-ipfs-api 提交issue报告。
安装
通过 pip 进行安装:
pip install ipfsapi
用法
基本用法 (需要已经有一个运行的 IPFS 服务实例):
import ipfsapi
api = ipfsapi.connect('127.0.0.1', 5001)
res = api.add('test.txt')
res
{'Hash': 'QmWxS5aNTFEc9XbMX1ASvLET1zrqEaTssqt33rVZQCQb22', 'Name': 'test.txt'}
api.cat(res['Hash'])
'fdsafkljdskafjaksdjf\n'
管理功能:
api.id()
{'Addresses': ['/ip4/127.0.0.1/tcp/4001/ipfs/QmS2C4MjZsv2iP1UDMMLCYqJ4WeJw8n3vXx1VKxW1UbqHS',
'/ip6/::1/tcp/4001/ipfs/QmS2C4MjZsv2iP1UDMMLCYqJ4WeJw8n3vXx1VKxW1UbqHS'],
'AgentVersion': 'go-ipfs/0.4.10',
'ID': 'QmS2C4MjZsv2iP1UDMMLCYqJ4WeJw8n3vXx1VKxW1UbqHS',
'ProtocolVersion': 'ipfs/0.1.0',
'PublicKey': 'CAASpgIwgg ... 3FcjAgMBAAE='}
传入API参数:
api.pin_ls(type='all')
{'Keys': {'QmNMELyizsfFdNZW3yKTi1SE2pErifwDTXx6vvQBfwcJbU': {'Count': 1,
'Type': 'indirect'},
'QmNQ1h6o1xJARvYzwmySPsuv9L5XfzS4WTvJSTAWwYRSd8': {'Count': 1,
'Type': 'indirect'},
…
添加目录,指定文件名类型匹配:
api.add('photos', match='*.jpg')
[{'Hash': 'QmcqBstfu5AWpXUqbucwimmWdJbu89qqYmE3WXVktvaXhX',
'Name': 'photos/photo1.jpg'},
{'Hash': 'QmSbmgg7kYwkSNzGLvWELnw1KthvTAMszN5TNg3XQ799Fu',
'Name': 'photos/photo2.jpg'},
{'Hash': 'Qma6K85PJ8dN3qWjxgsDNaMjWjTNy8ygUWXH2kfoq9bVxH',
'Name': 'photos/photo3.jpg'}]
递归添加目录:
api.add('fake_dir', recursive=True)
[{'Hash': 'QmQcCtMgLVwvMQGu6mvsRYLjwqrZJcYtH4mboM9urWW9vX',
'Name': 'fake_dir/fsdfgh'},
{'Hash': 'QmNuvmuFeeWWpxjCQwLkHshr8iqhGLWXFzSGzafBeawTTZ',
'Name': 'fake_dir/test2/llllg'},
{'Hash': 'QmX1dd5DtkgoiYRKaPQPTCtXArUu4jEZ62rJBUcd5WhxAZ',
'Name': 'fake_dir/test2'},
{'Hash': 'Qmenzb5J4fR9c69BbpbBhPTSp2Snjthu2hKPWGPPJUHb9M',
'Name': 'fake_dir'}]
辅助函数,添加字符串和JSON等资源:
lst = [1, 77, 'lol'] client.add_json(lst) 'QmQ4R5cCUYBWiJpNL7mFe4LDrwD6qBr5Re17BoRAY9VNpd' client.get_json(_) [1, 77, 'lol']
综合
import ipfsapi
# 连接IPFS,需要先启动节点服务器daemon
api = ipfsapi.connect('127.0.0.1', 5001)
# 查看节点ID
api.id()
# 上传文件
res = api.add('test.txt')
# 上传目录
res = api.add('pub_dir', recursive=True)
# 查看文件内容
res = api.cat('QmWxS5aNTFEc9XbMX1ASvLET1zrqEaTssqt33rVZQCQb23')
# 下载文件
res = api.get('QmWxS5aNTFEc9XbMX1ASvLET1zrqEaTssqt33rVZQCQb23')
文档
可用的IPFS文档 (目前的大部分 API 文档) :
https://ipfs.io/ipns/QmZ86ow1byeyhNRJEatWxGPJKcnQKG7s51MtbHdxxUddTH/Software/Python/ipfsapi/
这个客户端命令行文档(ipfs command-line Client documentation)也许有用。
重要变化,从ipfsApi 0.2.x开始
- The Python package has been renamed from
ipfsApitoipfsapi - The PIP module has been renamed from
ipfs-apitoipfsapi(please update your requirement files) - A lot of changes in the internal code
- Commands have been completely removed
- Usage of
requestsor other libraries is considered an implementation detail from now on
- Most parts of the library (except for
Client()) are now considered internal and may therefore break at any time (reference)- We will try to keep breakage for these modules at a minimum
- If you require stabilisation of some feature please open an issue with the feature in question and your preceived use-case
- Raised exceptions have been completely changed and are now documented with guaranteed backwards compatibility (reference)
- The new
ipfsapi.connect()function allows creating aClientinstance, while also checking whether a compatible IPFS daemon instance is actually available - Methods in
Client()now have parameters for options
其它项目
使用 py-ipfs-api的项目。如果你的项目希望添加,可以提交 PR 给开发者!
- git-remote-ipfs 允许push 和 pull git repositories,使用 IPFS network。
- InterPlanetary Wayback interfaces web archive (WARC) 文件,用于distributed indexing 和 replay,使用 IPFS。
来源:oschina
链接:https://my.oschina.net/u/2306127/blog/1934093
