IPFS 服務的Python訪問

IPFS 服務的Python訪問

py-ipfs-api提供python api對IPFS服務經過http gateway進行訪問,須要運行一個本地的IPFS daemon。https://github.com/ipfs/py-ipfs 是IPFS的純Python語言實現,自己就具備IPFS Daemon功能。html

Python IPFS HTTP Client Library

查看 the client API reference,獲取完整的命令參考。python

重要: The py-ipfs-api PIP package 和 Python module 已經都改名爲 ipfsapi (沒有橫線, 小寫的 a)。
原來的 ipfs-api/ipfsApi package/module 只用於 IPFS 0.3.x 和 Python 2,已通過時。請更新-Please upgrade!git

Note: 該庫儘量保持 IPFS HTTP API的兼容性。目前,通過測試 go-ipfs v0.4.10。若是與 go-ipfs的其它版本遇到兼容性問題,能夠到https://github.com/ipfs/py-ipfs-api 提交issue報告。github

安裝

經過 pip 進行安裝:web

pip install ipfsapi

用法

基本用法 (須要已經有一個運行的 IPFS 服務實例):json

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

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'},
          …

添加目錄,指定文件名類型匹配:tcp

api.add('photos', match='*.jpg')

[{'Hash': 'QmcqBstfu5AWpXUqbucwimmWdJbu89qqYmE3WXVktvaXhX',
  'Name': 'photos/photo1.jpg'},
 {'Hash': 'QmSbmgg7kYwkSNzGLvWELnw1KthvTAMszN5TNg3XQ799Fu',
  'Name': 'photos/photo2.jpg'},
 {'Hash': 'Qma6K85PJ8dN3qWjxgsDNaMjWjTNy8ygUWXH2kfoq9bVxH',
  'Name': 'photos/photo3.jpg'}]

遞歸添加目錄:ide

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 給開發者!

相關文章
相關標籤/搜索