仅用于开发模式,给非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

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐