您的位置:澳门402永利com > 编程应用 > 透过命令行在Python中测验以太坊RPC客户端,py开垦

透过命令行在Python中测验以太坊RPC客户端,py开垦

发布时间:2019-11-01 18:21编辑:编程应用浏览(188)

    python利用web3.py开荒以太坊应用dapp的实战教程:

    在这里个笔记中,小编将应用Python命令行测验以太坊的RPC客商端,计划好狂敲键盘吧。进度中有关JSON RPC的更加多新闻,能够参照他事他说加以考察JSON RPC。

    python以太坊

    输入:

    本学科详细讲明怎样运用Python开辟以太坊应用,课程内容即涉及以太坊中的宗旨概念,例如账户管理、状态与交易、智能合约开垦与互动、过滤器和事件等,同期也详细表明什么使用Python与以太坊举办交互,是Python技术员学习以太坊应用开辟的不二选项 。

    from IPython.core.interactiveshell import InteractiveShellInteractiveShell.ast_node_interactivity = "all"
    
    import syssys.version
    

    输出:

    '3.6.0 |Anaconda 4.3.0 | (default, Dec 23 2016, 12:22:00) n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]'
    

    粮草先行未雨筹算条件

    为以太坊设置Python RPC客户端,输入:

    jitsejan@jjvps:~$ pip install ethereum-rpc-client
    

    输入:

    jitsejan@jjvps:~$ geth --networkid 23 --nodiscover --maxpeers 0 --port 30333 --rpc
    

    输入:

    !geth account list
    

    输出:

    Account #0: {8cf9deda0712f2291fb16739f8759e4a0a575854} keystore:///home/jitsejan/.ethereum/keystore/UTC--2017-05-01T14-58-43.532247863Z--8cf9deda0712f2291fb16739f8759e4a0a575854
    

    输入:

    from eth_rpc_client import Clientclient = Client(host="127.0.0.1", port="8545")
    

    输入:

    import pdirpdir
    

    输出:

    abstract class: __subclasshook__attribute access: __delattr__, __dir__, __getattribute__, __setattr__class customization: __init_subclass__object customization: __format__, __hash__, __init__, __new__, __repr__, __sizeof__, __str__other: _coinbase_cache, _coinbase_cache_til, _nonce, async_timeout, host, is_async, port, request_queue, request_thread, results, sessionpickle: __reduce__, __reduce_ex__rich comparison: __eq__, __ge__, __gt__, __le__, __lt__, __ne__special attribute: __class__, __dict__, __doc__, __module__, __weakref__descriptor: default_from_address: @property with getter, Cache the coinbase address so that we don't make two requests for everyfunction: _make_request: call: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_call construct_json_request: get_accounts: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_accounts get_balance: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getbalance get_block_by_hash: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getblockbyhash get_block_by_number: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getblockbynumber get_block_number: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_blocknumber<F37> get_code: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getcode get_coinbase: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_coinbase get_filter_changes: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getfilterchanges get_filter_logs: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getfilterlogs get_gas_price: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_gasprice get_logs: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getlogs get_max_gas: get_nonce: get_transaction_by_hash: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_gettransactionbyhash get_transaction_receipt: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_gettransactionreceipt make_request: new_block_filter: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_newblockfilter new_filter: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_newfilter new_pending_transaction_filter: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_newpendingtransactionfilter process_requests: Loop that runs in a thread to process requests synchronously. send_transaction: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sendtransaction uninstall_filter: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_uninstallfilter wait_for_block: wait_for_transaction: 
    

    输入:

    address = client.get_coinbase()address
    

    输出:

    0x8cf9deda0712f2291fb16739f8759e4a0a575854

    输入:

    client.get_balance
    

    输出:

    135000419895999999940

    输入:

    address_vps_one = "0xc257beaea430afb3a09640ce7f020c906331f805"address_vps_two = "0xe86ee31b7d32b743907fa7438c422a1803717deb"client.get_balance(address_vps_one)client.get_balance(address_vps_two)
    

    输出:

    699916006000000000083000420044000000060
    

    交易

    让我们从主机发送十一个以太网到VPS,1 GWEI=0.000000001 Ether。

    输入:

    amount = 12 # Ethersending_address = addressreceiving_address = address_vps_one
    

    输入:

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

    输入:

    command = r'geth --exec "personal.unlockAccount("%s", "%s");" attach ' % (sending_address, pw)result = !$commandif result[0] != 'true': print('Fail: %s' % result[0])else: print('Account is unlocked!')
    

    输出:

    Account is unlocked!
    
    tx_hash = client.send_transaction(to=receiving_address, _from=sending_address, value=amount * 10**9)
    
    client.get_transaction_by_hash
    

    输出:

    {'blockHash': '0x0000000000000000000000000000000000000000000000000000000000000000', 'blockNumber': None, 'from': '0x8cf9deda0712f2291fb16739f8759e4a0a575854', 'gas': '0x15f90', 'gasPrice': '0x4a817c800', 'hash': '0x3d1a193ccfccc4e9ab2a411044069deeec2feef31a594bbf73726b463e8e90b4', 'input': '0x', 'nonce': '0xb', 'r': '0xe8698846a461938e800698fcc34570e0c4e9a3425f0bc441bf3e0716dab7b3e0', 's': '0x4fcd9bda8a1e98a7b0e8d953dec0cc733238c383d97393aa15c43963551f8daf', 'to': '0xc257beaea430afb3a09640ce7f020c906331f805', 'transactionIndex': '0x0', 'v': '0x42', 'value': '0x2cb417800'}
    

    实行四个挖沙步骤

    实施矿工以证实交易。

    输入:

    prev_balance_sen = client.get_balance(sending_address)prev_balance_rec = client.get_balance(receiving_address)
    
    result = !geth --exec "miner.start();admin.sleepBlocks;miner.stop();" attachif result[0] != 'true': print('Fail: %s' % result[0])else: print("Mining finished!")
    

    出口挖矿实现:

    Mining finished!

    输入:

    print("Received %d"% (client.get_balance(receiving_address)-prev_balance_rec))
    

    出口收到金额:

    Received 12000000000

    先是检查余额的反差,输入:

    print("Difference of the sender %d"% (client.get_balance(sending_address)-prev_balance_sen))
    

    输出:

    Difference of the sender 4999999988000000000

    对于采矿来讲,矿工将获取采矿奖金。

    输入:

    mining_bonus = 5000000000000000000
    

    要赢得以太网发送的多少,我们需求减去采矿奖金。

    输入:

    print("Amount difference: %d" % int(client.get_balance(sending_address)-prev_balance_sen - mining_bonus))
    

    出口金额差异:

    Amount difference: -12000000000

    ======================================================================

    享用部分以太坊、EOS、比特币等区块链相关的交互式在线编制程序实战教程:

    • python以太坊,重借使指向python程序员使用web3.py进行区块链以太坊支出的详明。
    • java以太坊支付教程,首要是指向java和android程序员实行区块链以太坊支出的web3j详细解释。
    • php以太坊,首如果介绍使用php实行智能合约开拓交互,举行账号制造、交易、转账、代币开垦以至过滤器和贸易等内容。
    • 以太坊入门教程,首要介绍智能合约与dapp应用开垦,符合入门。
    • 以太坊开荒进级教程,首倘诺介绍使用node.js、mongodb、区块链、ipfs完毕去大旨化电商DApp实战,相符进级。
    • C#以太坊,首要讲明如何使用C#开采基于.Net的以太坊应用,富含账户处理、状态与贸易、智能合约开垦与互相、过滤器和交易等。
    • EOS教程,本学科支持你急迅入门EOS区块链去中央化应用的开支,内容富含EOS工具链、账户与钱包、发行代币、智能合约开辟与布局、使用代码与智能合约交互等骨干知识点,最终综合运用各知识点实现多个便签DApp的开辟。
    • java比特币开辟教程,本学科面向初读书人,内容即含有比特币的为主概念,例如区块链存款和储蓄、去中央化共鸣机制、密钥与剧本、交易与UTXO等,同不时候也详细批注怎么着在Java代码中集成比特币协理成效,举个例子成立地址、管理钱袋、构造裸交易等,是Java程序猿八斗之才的比特币开荒学习课程。
    • php比特币开垦教程,本课程面向初读书人,内容即含有比特币的宗旨概念,比如区块链存款和储蓄、去大旨化共鸣机制、密钥与剧本、交易与UTXO等,同期也详细疏解如何在Php代码中集成比特币协理功用,比方成立地址、管理钱袋、构造裸交易等,是Php程序猿名列前茅的比特币开采学习课程。
    • tendermint区块链开垦精解,本学科切合希望选拔tendermint进行区块链开采的技术员,课程内容即包罗tendermint应用开垦模型中的核心概念,比方ABCI接口、默克尔(Merkel)树、多版本状态库等,也富含代币发行等丰裕的实际操作代码,是go语言程序员急忙入门区块链开荒的一流选用。

    汇智网原创翻译,转发请标记出处。这里是原作在Python中选用以太坊RPC顾客端

    本文由澳门402永利com发布于编程应用,转载请注明出处:透过命令行在Python中测验以太坊RPC客户端,py开垦

    关键词:

上一篇:本人建了个Python的上学探究群

下一篇:没有了