python如何用以太坊web3客户端

以太坊Web3

类似于RPC客户端,我将使用Web3 API在私有区块链上进行交易处理。作为参考推荐你看web3pygithub的官方文档资料。

1.输入:

1
2
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

我使用Python 2.7环境,现在已经支持Python 3.5以上了。

2.看看我的系统开发环境:

1
2
import sys
sys.version

输出结果:

1
'2.7.13 |Anaconda 4.3.1 (64-bit)| (default, Dec 20 2016, 23:09:15) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]'

用pip安装一下web3:

1
2
3
4
5
6
7
8
9
10
(python2) jitsejan@jjvps:~$ pip install web3
Collecting web3
Requirement already satisfied: requests>=2.12.4 in /home/jitsejan/anaconda3/envs/python2/lib/python2.7/site-packages (from web3)
Collecting ethereum-utils>=0.2.0 (from web3)
Requirement already satisfied: pysha3>=0.3 in /home/jitsejan/anaconda3/envs/python2/lib/python2.7/site-packages (from web3)
Collecting ethereum-abi-utils>=0.4.0 (from web3)
Requirement already satisfied: rlp>=0.4.7 in /home/jitsejan/anaconda3/envs/python2/lib/python2.7/site-packages (from web3)
Collecting pylru>=1.0.9 (from web3)
Installing collected packages: ethereum-utils, ethereum-abi-utils, pylru, web3
Successfully installed ethereum-abi-utils-0.4.0 ethereum-utils-0.2.0 pylru-1.0.9 web3-3.8.1

注意:要确保geth开启时使用了这些参数--rpc--rpcapi="db,eth,net,web3,personal,web3

1
jitsejan@jjvps:~$ geth --networkid 23 --nodiscover --maxpeers 0  --port 30333 --rpc --rpcapi="db,eth,net,web3,personal,miner"

接着输入:

1
2
3
import web3
from web3 import Web3, KeepAliveRPCProvider, IPCProvider
web3.__version__

输出结果:

1
'3.8.1'

设置RPC链接

1
web3 = Web3(KeepAliveRPCProvider(host='localhost', port='8545'))

查询一下账号

输入:

1
web3.eth.accounts

输出结果:

1
[u'0x8cf9deda0712f2291fb16739f8759e4a0a575854']

继续输入:

1
address = web3.eth.accounts[0]

设置另外两台机器:

1
2
address_vps_one = "0xc257beaea430afb3a09640ce7f020c906331f805"
address_vps_two = "0xe86ee31b7d32b743907fa7438c422a1803717deb"

显示他们的账户余额

1
2
3
4
print "Host has %d Ether"% web3.fromWei(web3.eth.getBalance(address), 'ether')
print "VPS 1 has %d Ether" % web3.fromWei(web3.eth.getBalance(address_vps_one), 'ether')
print "VPS 2 has %d Ether" % web3.fromWei(web3.eth.getBalance(address_vps_two), 'ether')
prev_host_amount = float(web3.fromWei(web3.eth.getBalance(address), 'ether'))

输出结果:

1
2
3
Host has 411 Ether
VPS 1 has 150 Ether
VPS 2 has 83 Ether

交易

从主机发送12个ether到VPS1上。

1
2
3
amount = 12 # Ether
sending_address = address
receiving_address = address_vps_one

获取密码解锁发送帐户

1
2
from getpass import getpass  
pw = getpass(prompt='Enter the password for the sender: ')

输出结果:

1
Enter the password for the sender: ········

解锁帐户

1
web3.personal.unlockAccount(address, pw)

输出结果:

1
True

建一个交易

1
2
3
4
5
params = {}
params['to'] = receiving_address
params['from'] = sending_address
params['value'] = web3.toWei(amount, "ether")
tx_hash = web3.eth.sendTransaction(params)

校验一下

1
web3.eth.getTransaction(tx_hash)

输出结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{u'blockHash': u'0x0000000000000000000000000000000000000000000000000000000000000000',
u'blockNumber': None,
u'from': u'0x8cf9deda0712f2291fb16739f8759e4a0a575854',
u'gas': 90000,
u'gasPrice': 20000000000,
u'hash': u'0x46e0a5a96dbea0391adbfd401b087ad1d1dfb120684462d8652df7d8b8d7f069',
u'input': u'0x',
u'nonce': 24,
u'r': u'0x63d0a6d39e9f57bf42e874bb268fc11c67cdb72d86cd21a65181b48063ddb531',
u's': u'0x44bb390959b28e0f14d4a7891b5ac44c84816a69c2434aeafdc5c94a42997b2b',
u'to': u'0xc257beaea430afb3a09640ce7f020c906331f805',
u'transactionIndex': 0,
u'v': u'0x42',
u'value': 12000000000000000000L}

查验收据

矿工没有运行工作,所以不返回任何收据。

1
web3.eth.getTransactionReceipt(tx_hash)

开启或者停止挖矿

1
2
3
4
5
import time
miner = web3.miner
miner.start(1)
time.sleep(5)
miner.stop()

输出结果:

1
True

再次查验收据

1
web3.eth.getTransactionReceipt(tx_hash)

输出结果:

1
2
3
4
5
6
7
8
9
10
11
12
{u'blockHash': u'0x635486b2100a4a9de155b57c1a9fad4dfafcf1db1d62f9aa55bd38d6a3864869',
u'blockNumber': 110,
u'contractAddress': None,
u'cumulativeGasUsed': 21000,
u'from': u'0x8cf9deda0712f2291fb16739f8759e4a0a575854',
u'gasUsed': 21000,
u'logs': [],
u'logsBloom': u'0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
u'root': u'0xe7ca6b8a99b6e69f52c2834ae84276dd9b0b89daf459c7391d29e2121e49e27e',
u'to': u'0xc257beaea430afb3a09640ce7f020c906331f805',
u'transactionHash': u'0x46e0a5a96dbea0391adbfd401b087ad1d1dfb120684462d8652df7d8b8d7f069',
u'transactionIndex': 0}

再次检查账户

主机应该减少了12个eth,但是矿工那里收到了5个eth,VPS1应该收到了12个eth。

1
2
3
print "Host has %d Ether"% web3.fromWei(web3.eth.getBalance(address), 'ether')
print "VPS 1 has %d Ether" % web3.fromWei(web3.eth.getBalance(address_vps_one), 'ether')
print "VPS 2 has %d Ether" % web3.fromWei(web3.eth.getBalance(address_vps_two), 'ether')

输出结果:

1
2
3
Host has 404 Ether
VPS 1 has 162 Ether
VPS 2 has 83 Ether
1
2
3
mine_bonus = 5 # Ether
num_cycles = (float(web3.fromWei(web3.eth.getBalance(address), 'ether')) - prev_host_amount + amount)/mine_bonus
print "%d mining cycle(s) are elapsed" % num_cycles

输出结果:

1
1个采矿周期已经过去。

python有专门的web3.py库,对于python开发以太坊来说非常的方便,有兴趣的用户可以关注我们的python以太坊教程,主要是针对python工程师使用web3.py进行区块链以太坊开发的详解。

另外其他语言可以学习的以太坊教程如下:

  • web3j教程,主要是针对java和android程序员进行区块链以太坊开发的web3j详解。
  • 以太坊教程,主要介绍智能合约与dapp应用开发,适合入门。
  • 以太坊开发,主要是介绍使用node.js、mongodb、区块链、ipfs实现去中心化电商DApp实战,适合进阶。
  • php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和事件等内容。
  • C#以太坊,主要讲解如何使用C#开发基于.Net的以太坊应用,包括账户管理、状态与交易、智能合约开发与交互、过滤器和事件等。

汇智网原创翻译,转载请标明出处。这里是原文