This content originally appeared on Level Up Coding - Medium and was authored by Henrique Centieiro
Ethereum — Playing with Python web3 library and eth serialization Ethereum
Ethereum Node Series III — How to connect to the Ethereum network using Python and web3.py
In this article, we will learn how to install Python on your Ubuntu machine, install the web3 library and connect to the Ethereum network. This article assumes that you have already the following:
- An Ethereum node running on Ubuntu. Check this article where I explain how to deploy an Ethereum node on Ubuntu AWS
- You already know a few commands to play around with the Etehreum network. Check this article where I talk about some commands to explore Ethereum blocks
- You know what an ? is! ?
Python is a great programming language to play around with blockchain and other technologies such as Machine Learning, AI and Data Analytics. It’s great to have it in your toolkit as a general-purpose programming language that is quite easy to read and understand. Additionally, there are many Fintech applications in Python, making it very important if you are in this industry! And no, contrary to the other pythons, Python code doesn’t bite! It’s a very human-readable language and, again, straightforward to develop blockchain applications and leverage its web3 library.

What are Web2 and Web3?
Web2 is basically the internet that most of us use nowadays. The centralised internet is usually hosted and controlled by big data centres and big cloud providers. Web2 is typically composed of servers, webservers, centralized HTTP APIs and databases.
Web3, on the other hand, is the decentralized internet. More and more developers chose to develop their applications on Web3 (Ethereum is one of the biggest Web3 enablers) because it’s permissionless, no one can block or deny access to Web3, it has built-in payments (like ETH), and because Ethereum is turning complete, you can pretty much develop anything! The web3 is the world where dApps, DeFi and NFTs live! So much wow! ?
Install Python and the Python web3 library
We now need to install Python in our Ubuntu instance and install the eth-rlp library to access a few additional functionalities.
From your Ubuntu command line, type
sudo apt install python3-pip
Once Python is installed, let’s install the library that allows us to do the serialization.
pip3 install eth-rlp
We will use this library to serialize block headers data and create a block hash. This will help us to process the data to fit it into the hashing algorithm.
Next, we need to install a Python hashing backend and cryptography library, the Cryptodome pysha3. This library will give you things like AES, elliptic curves, SHA hashing, ECDSA, password-protected containers (PKCS#8), RSA, DSA and other cool cryptography stuff in case you need it:
pip3 install pycryptodome pysha3
Then we install the Web3 library. Web3 is a JavaScript library that interacts with the Etherem blockchain. It’s also called the next generation worldwide web, and it provides a decentralized web that allows us to interact with other Ethereum nodes using HTTP and IPC connections. It also enables to send transactions, retrieve user accounts and interact with smart contracts. Let’s install it.
pip3 install web3
sudo pip3 install pyethash
We will also need pyethash for the ethash algorithm. This will allow us to generate proof of work
Now we have all that we need to be installed, let’s enter our Ethereum node path .ethereum/ and run python. Here I assume that you have already an Ethereum node. If you don’t have, jump to the beginning of the article. Type the commands as per below:
cd .ethereum/
and then, type
python3
Welcome to Python3 console! Let’s import some tools that we will need, starting with the IPC Provider from Web3. Every time you exit Python3, you will have to repeat the next 2 commands. NOTE: To make this work, you need to run in parallel the Ethereum node with the geth command you saw before.
from web3 import IPCProvider, Web3
The IPC provider will allow your node to connect via an IPC connection. Ethereum Geth also allows HTTP and WebSocket connection, but IPC should be more secure.
Once this is done, we need to make sure that it works and that we can fetch the data. Let’s call an object w3 (you can call it anything) with the following command to make calls. Type:
w3 = Web3(IPCProvider())
and now, if you type, for example, w3.eth. and press the tab key twice, you will get the list of commands. Note that these 2 last commands may take a few seconds to take effect.

After a few seconds (sometimes you may need to wait for a minute to let it connect), you should also be able to type any command like
w3.eth.getBlock(987654)
And get the same block we saw before but in raw format:

This is the exact same block we saw before but a bit less human-readable. It’s raw dog data here. Cool right? ??☠️?
Now the reality is that this raw data is not very easy to read, but if we are looking for a specific piece of data, we can ask for it:
Here you can see some examples where if you are looking for the difficulty, the nonce or the miner of a block, you can type the following commands respectively:
w3.eth.getBlock(987654)[“difficulty”]
w3.eth.getBlock(987654)[“nonce”]
w3.eth.getBlock(987654)[“miner”]
Another thing you can do to improve the raw block’s readability is to separate each value in a different line.
We can achieve this with the following:
myBlock = w3.eth.getBlock(987654)
print(*myBlock.items(), sep=’\n’)
Now it’s more intelligible! ???

I’m just showing a portion of the block here because you have already seen the rest of the block before. ?
I hope you have enjoyed it! Next article, we will continue to play around with Python and calculate the Ethereum difficulty with python scripts! Keep tuned!
? Follow me, and please also check my ? blockchain courses:
? The First-Ever Dogecoin Course
?? Fintech, Cloud and Cybersecurity Course
?? Unblockchain Course — The Brain-Friendly Blockchain Course
Ethereum — Playing with Python web3 library and eth serialization Ethereum was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Henrique Centieiro

Henrique Centieiro | Sciencx (2021-07-14T21:50:45+00:00) Ethereum — Playing with Python web3 library and eth serialization Ethereum. Retrieved from https://www.scien.cx/2021/07/14/ethereum%e2%80%8a-%e2%80%8aplaying-with-python-web3-library-and-eth-serialization-ethereum/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.