一个Python脚本,用于给geth循环解锁账户,方便合约调试

仅用于开发模式,给非0号账户自动解锁使用。为了资金安全考虑,切勿用于真实环境。

#!/usr/bin/python
# coding: utf-8
import subprocess, threading, sys, time
geth=subprocess.Popen('./rungeth.sh',stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)

def blockData(chan):
    '''
    :param subprocess.Popen chan:
    :return:
    '''
    while chan.poll() is None:
        r=chan.stdout.read(1)
        sys.stdout.write(r)
        sys.stdout.flush()

def autoType(chan):
    '''
    :param subprocess.Popen chan:
    :return:
    '''
    x=5
    while chan.poll() is None:
        time.sleep(x)
        print 'unlocking account 1'
        chan.stdin.write('personal.unlockAccount(eth.accounts[1])\n')
        chan.stdin.flush()
        time.sleep(2)
        chan.stdin.write('\n')
        time.sleep(x)
        print 'unlocking account 2'
        chan.stdin.write('personal.unlockAccount(eth.accounts[2])\n')
        chan.stdin.flush()
        time.sleep(2)
        chan.stdin.write('\n')
        x=13

t=threading.Thread(target=blockData,args=(geth,))
u=threading.Thread(target=autoType,args=(geth,))
t.setDaemon(True)
u.setDaemon(True)
t.start()
u.start()

while True:
    uinput=raw_input('')
    geth.stdin.write(uinput+'\n')
    geth.stdin.flush()

我们假设rungeth.sh是同目录下geth开启开发模式的脚本,例如如下内容

#!/bin/bash
# 使用开发者模式,请不加参数,否则请加任何一个参数
if [ $# == 1 ]; then
  geth --datadir ./data --networkid 18 --port 30303 --rpc  --rpcport 8546 --rpcapi 'db,net,eth,web3,personal' --rpccorsdomain '*' --gasprice 0  console 2> 1.log
else
  geth --datadir ./devdata --networkid 18 --port 30303 --rpc --rpcaddr 0.0.0.0 --rpcvhosts "*"  --rpcport 8546 --rpcapi 'db,net,eth,web3,personal' --rpccorsdomain '*' --ws --wsaddr 0.0.0.0 --wsport 8545 --wsorigins '*' --wsapi 'db,net,eth,web3,personal' --gasprice 0 --dev --dev.period 1 console 2> 1.log
fi

将开启8546的httpProvider和8545的webSocketProvider

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值