Web3.py learning (1)

1. What is Web3.py

       Web3.py is a python library for interacting with Ethereum. Its API is derived from the Web3.js Javascript API and should be familiar to anyone who has used web3.js.

       Web3.py is a python library that connects to Ethereum, and its API is derived from web3.js. If you have used web3.js, you will be familiar with its API. But ashamed, as a developer of Dapp on Ethereum, I have hardly used web3.js directly, nor have I seen its API.

       Although I have used web3.py when developing Dapp in a local environment (such as Ganache), it is just a very simple application, and I have not studied it systematically (part of the reason is that I have not learned python systematically, but just read about 10 hours documentation). Open the official document of web3.py today and prepare to study the system.

       The author's learning environment is Mac OS + python 3.7.3.

Second, install and test web3.py

       I have installed web3.py before, so I directly tested it according to the application in the document. I did not expect to encounter some problems, which are recorded as follows for the reference of readers who have the same problem.

2.1 Set environment variables

       Open the terminal, it is in the ~directory by default , run vim .bash_profile, press iinsert, and add in the last line:

export WEB3_INFURA_PROJECT_ID=your_infura_key

       Among them your_infura_keyis the key needed to connect to the infura node. If not, go to https://infura.io/ and create a new project to get it.

       Press esc, then yes :, then wqsave and exit. Run again source .bash_profileto make the environment variable settings effective.

2.2 Resolve various errors

       Execute in the terminal python3, enter the interpreter, or run an IDLEapplication. Execute the following code:

>>> from web3.auto.infura import w3
>>> w3.eth.blockNumber

However, after a few seconds of waiting, the following error was reported:

certificate verify failed: unable to get local issuer certificate

       Google took a look, because python3.6 and 3.7 no longer depend on openSSL of Mac OS. It uses its own openSSL. There are two solutions:
       1. Run the installer that comes with python

cd /Applications/Python\ 3.7/
./Install\ Certificates.command

       2. Installation certifipackage

pip install certifi

       In fact, these two methods are equivalent. Well, this is how this problem is solved.

       Re-execute the script to get the current block height, but an error is reported:

websockets.exceptions.InvalidStatusCode: Status code not 101: 401

       Continuing with Google, I'm talking about websocketsthe difference between this library 6.0 and 7.0. It seems that my library is 6.0, this is easy to handle, upgrade it:

pip install --upgrade websockets

       The success of the websocketsupgrade from 6.0 to 8.0. However, during the upgrade process it prompts:

ERROR: web3 4.8.1 has requirement websockets<7.0.0,>=6.0.0, 
but you'll have websockets 8.1 which is incompatible.

       I am speechless, it seems that I have to continue to upgrade my web3.py. run:

pip install --upgrade web3

       Wait for the upgrade to complete. Okay, there is no problem now.

2.3 Connect to Ethereum nodes

       Re-run in the terminal phthon3, run the interpreter, and then execute:

>>> from web3.auto.infura import w3
>>> w3.eth.blockNumber

       After a short time of waiting, the current block height will be output. During the test, it is 9462097.

       View the latest block details:

>>> w3.eth.getBlock('latest')

       There will be output soon, showing some information about the current block, and the test of connecting to the Ethereum node passes.

Third, connect to local Ethereum

       When we usually use the local development environment (Ganache), how do we connect to the local Ethereum? A method is given in the document, using HTTPProvider:

>>> w3 = Web3(HTTPProvider('http://localhost:8545'))

       But we don’t need to use this method, it is recommended to use another:

>>> from web3.auto import w3
>>> w3.isConnected()
True
>>> w3.eth.blockNumber
46
>>> 

Four, familiar with the basic API

       When you are familiar with the basic API, I suggest that you do not execute in the terminal python3to enter the interpreter, but run the IDLEprogram. Because IDLEthere is a prompt in the API, as shown in the following figure:
Insert picture description here
       here is a prompt to enter a number and a string, and an integer is returned. Note that this string refers to the unit on Ethereum, such as ether, Gweietc.

       In addition to checking whether the basic API listed in the document can be coded, it is often used in normal development.

       The first chapter overview of Web3.py is over. There is not much learning content this time, the focus is to master the installation of web3.py and the method of connecting to the Ethereum node. At the same time, we must have some understanding of the basic API, after all, we must use it frequently.

       Looking forward to the next learning.

Guess you like

Origin blog.csdn.net/weixin_39430411/article/details/104270968