IPFS 服务的Python访问

IPFS 服务的Python访问

py-ipfs-api提供python api对IPFS服务通过http gateway进行访问,需要运行一个本地的IPFS daemon。https://github.com/ipfs/py-ipfs 是IPFS的纯Python语言实现,本身就具有IPFS Daemon功能。

Python IPFS HTTP Client Library

查看 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 ipfsApi to ipfsapi
  • The PIP module has been renamed from ipfs-api to ipfsapi (please update your requirement files)
  • A lot of changes in the internal code
    • Commands have been completely removed
    • Usage of requests or 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 a Client instance, while also checking whether a compatible IPFS daemon instance is actually available
  • Methods in Client() now have parameters for options

其它项目

使用 py-ipfs-api的项目。如果你的项目希望添加,可以提交 PR 给开发者!

转载于:https://my.oschina.net/u/2306127/blog/1934093

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
IPFS (InterPlanetary File System) 是一个开源的分布式文件系统,它通过使用分布式哈希表来连接全球计算机网络,提供了一个去中心化的存储和共享文件的方式。你可以使用 Python 来与 IPFS 进行交互和操作。 在 Python 中,你可以使用 ipfshttpclient 库来连接并与 IPFS 节点进行通信。首先,你需要安装该库,可以使用以下命令来安装: ``` pip install ipfshttpclient ``` 安装完成后,你可以按照以下示例代码来使用 IPFS 的基本功能: ```python import ipfshttpclient # 连接到 IPFS 节点 client = ipfshttpclient.connect() # 添加文件到 IPFS res = client.add('path/to/file.txt') file_hash = res['Hash'] print(f'File uploaded, IPFS hash: {file_hash}') # 获取文件内容 content = client.cat(file_hash) print(f'File content: {content}') # 获取文件的信息 file_info = client.stat(file_hash) print(f'File size: {file_info["Size"]}') # 下载文件到本地 client.get(file_hash) # 关闭连接 client.close() ``` 以上代码演示了如何连接到 IPFS 节点、上传文件、获取文件内容和信息、下载文件等基本操作。你可以根据自己的需求进行扩展和调整。 请注意,在实际使用中,你需要确保 IPFS 节点正常运行并具备访问权限。另外,IPFS 是一个分布式系统,因此文件上传后并不能立即在全球范围内被访问到,需要等待网络中的节点完成传播和同步。 希望这能帮助到你!如果你有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值