The common entry point for interacting with the Web3.py library is the Web3 object. Web3 objects provide apis for Python-developed applications to interact with the Ethereum blockchain, typically by connecting to a JSON-RPC server.

Will the provider

Providers connect Web3 to the blockchain. The web3.py library comes with the following built-in providers, which can be used in most use cases.

  • web3.HTTPProvider: Used to connect to JSON-RPC servers based on HTTP and HTTPS.
  • web3.IPCProvider: Used to connect to jSON-RPC servers based on IPC sockets.
  • web3.WebsocketProviderJson-rpc server for connecting to WS – and WSS – based Websockets

HTTPProvider: Used to get the full URI where the server can be found. For local development, this is similar to http://localhost:8545.

IPCProvider: Used to get the file system path where the IPC socket can be found. If no arguments are provided, it uses the default path of the operating system.

WebsocketProvider: Used to get the full URI where the server can be found. For local development, this is similar to ws://127.0.0.1:8546.

Example code is as follows:

>>> from web3 import Web3, HTTPProvider, IPCProvider, WebsocketProvider # Note that you should create only one RPCProvider per # process, as it recycles underlying TCP/IP network connections between # your process and Ethereum node >>> web3 = Web3(HTTPProvider('http://localhost:8545')) # or for an IPC based connection >>> web3 = Web3(IPCProvider()) # or for Websocket Based Connection >>> web3 = web3 (WebsocketProvider('ws://127.0.0.1:8546'))Copy the code

Basic API

The Web3 class provides the following handy basic apis:

Type conversion

web3.toHex

Web3.toHex(primitive=None, hexstr=None, text=None)
Copy the code

Takes various input and returns it in hexadecimal representation. It follows the rules for converting to hexadecimal in the JSON-RPC specification.

>>> Web3.toHex(0) '0x0' >>> Web3.toHex(1) '0x1' >>> Web3.toHex(0x0) '0x0' >>> Web3.toHex(0x000F) '0xf' >>> Web3.toHex(b'') '0x' >>> Web3.toHex(b'\x00\x0F') '0x000f' >>> Web3.toHex(False) '0x0' >>> Web3.toHex(True) '0x1' >>> Web3.toHex(hexstr='0x000F') '0x000f' >>> Web3.toHex(hexstr='000F') '0x000f' >>> Web3.toHex(text='') '0x' >>> = 'cowmo' Web3. ToHex (text) '0 x636f776dc3b6'Copy the code

web3.toText

Web3.toText(primitive=None, hexstr=None, text=None)
Copy the code

Takes various inputs and returns their string equivalents. The text is decoded to UTF-8.

>>> web3.totext (0x636F776dc3B6) 'cowmo' >>> web3.totext (b'cowm\xc3\ XB6 ') 'cowmo' >>> Web3.totext (hexstr='0x636f776dc3b6') 'cowmo' >>> web3.totext (hexstr='636f776dc3b6') 'cowmo' >>> = 'cowmo' Web3. ToText (text) 'cowmo'Copy the code

Web3.toBytes

Web3.toBytes(primitive=None, hexstr=None, text=None)
Copy the code

Takes various inputs and returns the equivalent bytes. The text is encoded as UTF-8.

>>> Web3.toBytes(0) b'\x00' >>> Web3.toBytes(0x000F) b'\x0f' >>> Web3.toBytes(b'') b'' >>> Web3.toBytes(b'\x00\x0F') b'\x00\x0f' >>> Web3.toBytes(False) b'\x00' >>> Web3.toBytes(True) b'\x01' >>> Web3.toBytes(hexstr='0x000F') b'\x00\x0f' > > > Web3. ToBytes (hexstr = '000 f) b' \ x00 \ x0f '> > > Web3. ToBytes (text =' ') "> b > > Web3. ToBytes (text = 'cowmo') b'cowm\xc3\xb6'Copy the code

Web3.toInt

Web3.toInt(primitive=None, hexstr=None, text=None)
Copy the code

Takes various inputs and returns their integer equivalents.

>>> Web3.toInt(0)
0
>>> Web3.toInt(0x000F)
15
>>> Web3.toInt(b'\x00\x0F')
15
>>> Web3.toInt(False)
0
>>> Web3.toInt(True)
1
>>> Web3.toInt(hexstr='0x000F')
15
>>> Web3.toInt(hexstr='000F')
15
Copy the code

Currency conversion

Web3.toWei

Web3.toWei(value, currency)
Copy the code

Returns the value converted to wei in the denomination specified by the currency argument.

>>> Web3.toWei(1, 'ether')
1000000000000000000
Copy the code

Web3.fromWei

Web3.fromWei(value, currency)
Copy the code

Returns the value of wei converted to the given currency. The value is returned as Decimal to ensure accuracy down to WEI.

>>> web3.fromWei(1000000000000000000, 'ether')
Decimal('1')
Copy the code

address

Web3.isAddress

Web3.isAddress(value)
Copy the code

Returns true if the value is one of the recognized address formats. – Allows 0x prefixes and values without prefixes. – If the address contains mixed upper and lower case characters, the function also checks whether the address is EIP55 compliant.

>>> web3.isAddress('0xd3CdA913deB6f67967B99D67aCDFa1712C293601')
True
Copy the code

Web3.isChecksumAddress

Web3.isChecksumAddress(value)
Copy the code

Returns true if the value conforms to EIP55 and is a valid address

>>> web3.isChecksumAddress('0xd3CdA913deB6f67967B99D67aCDFa1712C293601')
True
>>> web3.isChecksumAddress('0xd3cda913deb6f67967b99d67acdfa1712c293601')
False
Copy the code

Web3.toChecksumAddress

Web3.toChecksumAddress(value)
Copy the code

Returns the given address with EIP55 checksum.

>>> Web3.toChecksumAddress('0xd3cda913deb6f67967b99d67acdfa1712c293601')
'0xd3CdA913deB6f67967B99D67aCDFa1712C293601'
Copy the code

Cryptographic hash

Web3.sha3

classmethod Web3.sha3(primitive=None, hexstr=None, text=None)
Copy the code

Returns Keccak SHA256 for the given value. Before calculating the hash, the text is encoded into UTF-8, just like solidity. Any of the following ways are valid and equal:

>>> Web3.sha3(0x747874)
>>> Web3.sha3(b'\x74\x78\x74')
>>> Web3.sha3(hexstr='0x747874')
>>> Web3.sha3(hexstr='747874')
>>> Web3.sha3(text='txt')
HexBytes('0xd7278090a36507640ea6b7a0034b69b0d240766fa3f98e3722be93c613b29d2e')
Copy the code

Web3.soliditySha3

classmethod Web3.soliditySha3(abi_types, value)

Returns Sha3, which is evaluated by the soliditySha3 function based on the supplied value and abi_types. Abi_types should be a list of solidity type strings for each value provided.

>>> Web3.soliditySha3(['bool'], True)
HexBytes("0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2")

>>> Web3.soliditySha3(['uint8', 'uint8', 'uint8'], [97, 98, 99])
HexBytes("0x4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")

>>> Web3.soliditySha3(['uint8[]'], [[97, 98, 99]])
HexBytes("0x233002c671295529bcc50b76a2ef2b0de2dac2d93945fca745255de1a9e4017e")

>>> Web3.soliditySha3(['address'], ["0x49eddd3769c0712032808d86597b84ac5c2f5614"])
HexBytes("0x2ff37b5607484cd4eecf6d13292e22bd6e5401eaffcc07e279583bc742c68882")

>>> Web3.soliditySha3(['address'], ["ethereumfoundation.eth"])
HexBytes("0x913c99ea930c78868f1535d34cd705ab85929b2eaaf70fcd09677ecd6e5d75e9")
Copy the code

Module Modules

Web3.py splits the JSON-RPC functionality over modules that loosely correspond to namespaces for jSON-RPC methods.


For those who want to get started quickly on developing Ethereum in Python, check out this: Python Ethereum, a detailed explanation of blockchain ethereum development for Python engineers using Web3.py.

Other Ethereum related tutorials:

  • Web3j tutorial, mainly for Java and Android programmers for blockchain Ethereum development web3J details.
  • Ethereum tutorial, mainly introduces smart contract and DAPP application development, suitable for getting started.
  • Ethereum development, mainly introduces the use of Node.js, mongodb, blockchain, IPFS to achieve decentralized e-commerce DApp combat, suitable for advanced.
  • PHP Ethereum mainly introduces the use of PHP for smart contract development interaction, account creation, transactions, transfers, token development, filters and events and other content.

Huizhi net original translation, reprint please indicate the source. Here is the original text