Reader small image

You're reading from  Hands-On Blockchain for Python Developers

Product typeBook
Published inFeb 2019
Reading LevelExpert
PublisherPackt
ISBN-139781788627856
Edition1st Edition
Languages
Concepts
Right arrow
Author (1)
Arjuna Sky Kok
Arjuna Sky Kok
author image
Arjuna Sky Kok

Arjuna Sky Kok has experience more than 10 years in expressing himself as a software engineer. He has developed web applications using Symfony, Laravel, Ruby on Rails, and Django. He also has built mobile applications on top of Android and iOS platforms. Currently, he is researching Ethereum technology. Other than that, he teaches Android and iOS programming to students. He graduated from Bina Nusantara University with majors in Computer Science and Applied Mathematics. He always strives to become a holistic person by enjoying leisure activities, such as dancing Salsa, learning French, and playing StarCraft 2. He lives quietly in the bustling city of Jakarta. In loving memory of my late brother, Hengdra Santoso (1979-2011).
Read more about Arjuna Sky Kok

Right arrow

Using ipfsapi to Interact with IPFS

In this chapter, we are going to learn how to interact with IPFS programmatically with Python. There are a couple of interactions that we can do here, such as adding files, retrieving files, hosting mutable files, subscribing to topics, publishing topics, and copying files to the Mutable File System (MFS). First, we have to install the IPFS software and launch it. Then, we will learn how to install the IPFS Python library and learn about most of its API.

In this chapter, we are going to cover the following topics:

  • Installing the IPFS software and it's library
  • Content hashing
  • The ipfsapi API

Installing the IPFS software and its library

At the time of writing, there are only two IPFS implementations: go-ipfs (written in the Go language) and js-ipfs (written in JavaScript). There is no IPFS implementation written in Python as of yet. The Go implementation is the more popular one, so we will use that.

Go to, https://dist.ipfs.io/#go-ipfs, and download the software for your platform. For Ubuntu Linux, the file is named go-ipfs_v0.4.18_linux-amd64.tar.gz.

Extract this using the following command line:

$ tar xvfz go-ipfs_v0.4.18_linux-amd64.tar.gz

Then, install the binary using the following command:

$ cd go-ipfs
$ sudo ./install.sh

This step is optional. Here, we export the IPFS_PATH environment variable to our shell:

$ export IPFS_PATH=/path/to/ipfsrepo

This is where the ipfs stores the files. You can store this statement in ~/.bashrc. By default (without this environment...

Content hashing

In the IPFS quick start documentation (https://docs.ipfs.io/introduction/usage), the first thing that they teach you is to download the cute cat picture. Use the following code to do this:

$ ipfs cat /ipfs/QmW2WQi7j6c7UgJTarActp7tDNikE4B2qXtFCfLPdsgaTQ/cat.jpg >cat.jpg
$ eog cat.jpg

When you run the preceding code, the cat picture will be downloaded and you will get the following as output:

eog is an image viewer in Ubuntu.

To respect the tradition, let's create a Python script to download the preceding image programmatically with Python and name the script download_cute_cat_picture.py:

import ipfsapi


c = ipfsapi.connect()
cute_cat_picture = 'QmW2WQi7j6c7UgJTarActp7tDNikE4B2qXtFCfLPdsgaTQ/cat.jpg'
c.get(cute_cat_picture)

After executing this script, the image will be named cat.jpg in your directory.

As you may have noticed, there is a cat.jpg filename...

The ipfsapi API

Let's go back to the API of ipfsapi. We have added a file with IPFS API and received the hash that we use to refer to the content of the file. But if we add a big file, this will be split into many chunks. This is for efficiency purposes.

Let's download quite a big image file from Unsplash. Go to, https://unsplash.com/photos/UBtUB4Qc-_4 to download an image file. The name of the downloaded file is milada-vigerova-1284157-unsplash.jpg. Put this in the same directory as your IPFS Python script files. You could use any image file for this, but make sure its size is at least 1 MB. However, if you use another image file, you should get a different hash.

Create a script named add_image_file.py using the following code block:

import ipfsapi


c = ipfsapi.connect()
result = c.add('dose-juice-1184429-unsplash.jpg')
print(result)

Run it. You should get the...

Summary

In this chapter, you have learned about interacting with the IPFS through the HTTP API using Python. First of all, you installed IPFS software and ran the daemon. You started this by adding a file to the IPFS and studied how to get the hash of the content of the file, which is based on protobuf, multihash, and base58. Then, you saw that a big file would be divided into many chunks if added to the IPFS. You could also add a directory of files into the IPFS. Based on this ability, you could host a static website on the IPFS. Then, you learned about publishing IPFS files in the IPNS on which you could have dynamic content. After this, you learned about the MFS, where you could copy a large file from the IPFS without incurring any significant costs in your local storage.

In the next chapter, you will combine the IPFS and smart contracts to build a decentralized application...

Further reading

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Blockchain for Python Developers
Published in: Feb 2019Publisher: PacktISBN-13: 9781788627856
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Arjuna Sky Kok

Arjuna Sky Kok has experience more than 10 years in expressing himself as a software engineer. He has developed web applications using Symfony, Laravel, Ruby on Rails, and Django. He also has built mobile applications on top of Android and iOS platforms. Currently, he is researching Ethereum technology. Other than that, he teaches Android and iOS programming to students. He graduated from Bina Nusantara University with majors in Computer Science and Applied Mathematics. He always strives to become a holistic person by enjoying leisure activities, such as dancing Salsa, learning French, and playing StarCraft 2. He lives quietly in the bustling city of Jakarta. In loving memory of my late brother, Hengdra Santoso (1979-2011).
Read more about Arjuna Sky Kok