Interacting with the Ethereum blockchain programmatically is a powerful skill for developers building decentralized applications (dApps), conducting blockchain analysis, or managing smart contracts. One of the most effective ways to do this in Python is by using an Ethereum RPC client through the command line. This guide walks you through setting up and testing an Ethereum RPC client in Python, from environment configuration to sending transactions and verifying results—all via the command line interface.
Whether you're a blockchain developer, a data analyst, or just exploring Web3, understanding how to communicate directly with Ethereum nodes using Python opens up a world of automation and control.
Setting Up the Development Environment
Before diving into RPC interactions, ensure your system has the necessary tools installed.
First, install the ethereum-rpc-client package using pip:
pip install ethereum-rpc-clientThis lightweight library allows Python scripts to send JSON-RPC requests directly to an Ethereum node such as Geth.
Next, launch a local Ethereum node with RPC enabled. For testing purposes, you can run a private network instance:
geth --networkid 23 --nodiscover --maxpeers 0 --port 30333 --rpcThis starts Geth with RPC listening on port 8545 by default, allowing external clients to connect.
👉 Discover how to securely connect to Ethereum nodes using advanced API tools
Connecting to the Ethereum Node
Once Geth is running, verify that your account is available:
!geth account listOutput:
Account #0: {8cf9deda0712f2291fb16739f8759e4a0a575854} keystore:///home/jitsejan/.ethereum/keystore/UTC--2017-05-01T14-58-43.532247863Z--8cf9deda0712f2291fb16739f8759e4a0a575854Now, initialize the RPC client in Python:
from eth_rpc_client import Client
client = Client(host="127.0.0.1", port="8545")You’re now connected to your local node and ready to query blockchain data.
Exploring Available Client Methods
To understand what operations are supported, inspect the client object:
import pdir
pdir(client)The output reveals a comprehensive list of methods aligned with the Ethereum JSON-RPC specification, including:
get_balance: Retrieve account balanceget_block_by_number: Fetch block detailssend_transaction: Submit signed transactionsget_transaction_by_hash: Check transaction statusget_gas_price: Get current gas rate
These functions allow full interaction with the Ethereum network—reading state, sending value, and monitoring activity.
Querying Blockchain Data
Retrieve Coinbase Address
The coinbase is the address where mining rewards are sent:
address = client.get_coinbase()
print(address)Output:
0x8cf9deda0712f2291fb16739f8759e4a0a575854Check Account Balances
Query the balance of your main account:
client.get_balance(address)Output:
135000419895999999940Balances are returned in wei (1 ether = 10¹⁸ wei).
You can also check balances of other addresses:
address_vps_one = "0xc257beaea430afb3a09640ce7f020c906331f805"
address_vps_two = "0xe86ee31b7d32b743907fa7438c422a1803717deb"
client.get_balance(address_vps_one)
client.get_balance(address_vps_two)Output:
6999160060000000000
83000420044000000060Sending Transactions via Command Line
Now let’s perform a real transaction—sending 12 Ether from one account to another.
Prepare Transaction Parameters
amount = 12 # in Ether
sending_address = address
receiving_address = address_vps_oneSince Ether values are handled in wei, convert accordingly:
value_in_wei = amount * 10**18Unlock the Sending Account
To sign a transaction, unlock the sender’s account using a password:
from getpass import getpass
pw = getpass(prompt='Enter the password for the sender: ')Then unlock via Geth console:
command = r'geth --exec "personal.unlockAccount(\"%s\", \"%s\");" attach ' % (sending_address, pw)
result = !$command
if result[0] != 'true':
print('Fail: %s' % result[0])
else:
print('Account is unlocked!')Output:
Account is unlocked!Send the Transaction
Use the RPC client to send Ether:
tx_hash = client.send_transaction(to=receiving_address, _from=sending_address, value=value_in_wei)
print("Transaction hash:", tx_hash)Verify Transaction Details
Retrieve the transaction info:
client.get_transaction_by_hash(tx_hash)Sample output:
{
"from": "0x8cf9...",
"to": "0xc257...",
"value": "0x2cb417800",
"gasPrice": "0x4a817c800",
"hash": "0x3d1a193ccfccc4e9ab2a411044069deeec2feef31a594bbf73726b463e8e90b4"
}Note: The value field is in hex-encoded wei (12 * 10^18 wei ≈ 0x2cb417800).
Mining and Confirming Transactions
Transactions require mining to be confirmed on-chain.
Start Mining
Trigger a single mining cycle:
result = !geth --exec "miner.start();admin.sleepBlocks(1);miner.stop();" attach
if result[0] != 'true':
print('Mining failed')
else:
print("Mining finished!")Output:
Mining finished!Verify Balance Changes
Check if the recipient received funds:
print("Received %d wei" % (client.get_balance(receiving_address) - prev_balance_rec))Output:
Received 12000000000000000000 weiSender balance will decrease not only by the sent amount but also due to gas costs and potential mining rewards (if mining locally).
To isolate the actual transfer amount, subtract mining bonuses:
mining_bonus = 5_000_000_000_000_000_000 # 5 ETH in wei
net_change = client.get_balance(sending_address) - prev_balance_sen - mining_bonus
print("Net sent amount:", abs(net_change))Output:
Net sent amount: 12000000000000000000Frequently Asked Questions
Q: What is an Ethereum RPC client?
A: It's a software interface that lets applications communicate with an Ethereum node using JSON-RPC calls, enabling actions like reading balances or sending transactions.
Q: Why use Python for Ethereum development?
A: Python offers simplicity and rich libraries like web3.py and ethereum-rpc-client, making it ideal for scripting, automation, and rapid prototyping in blockchain projects.
Q: Is it safe to unlock accounts via command line?
A: Only do this in secure environments. Avoid logging passwords or running scripts in shared systems. Consider using signed transactions offline for production use.
Q: How does gas work in Ethereum transactions?
A: Gas covers computational effort. Each transaction consumes gas, priced in Gwei. The total cost is gas_used × gas_price.
Q: Can I interact with testnets or mainnet?
A: Yes. Just point your client to a node connected to Rinkeby, Goerli, or mainnet instead of a local private chain.
Q: What are common use cases for command-line Ethereum scripts?
A: Automated balance checks, batch transactions, smart contract interactions, monitoring dApps, and educational experimentation.
👉 Learn how to build scalable blockchain applications using secure infrastructure
Core Keywords
- Ethereum RPC client
- Python blockchain development
- JSON-RPC Ethereum
- Test Ethereum node
- Send Ether via Python
- Geth command line
- Web3 automation
- Blockchain scripting
By mastering these techniques, you gain direct control over Ethereum interactions—no GUI required. Whether automating financial operations or analyzing chain data, command-line tools provide precision and flexibility essential for modern Web3 development.
👉 Access powerful blockchain tools and APIs for your next project