0

I'm running a Bitcoin testnet node on a Ubuntu 22.04 VM using UTM for Macbooks M1.

I'm trying to connect via json-rpc using https://github.com/jgarzik/python-bitcoinrpc.

1 - I installed the package etc..

2 - I edited my .conf file in /home/kk/snap/bitcoin-core/common/.bitcoin/bitcoin.conf:

server=1
testnet=1
daemon=1
prune=1000
fallbackfee=0.00001
rpcuser=kk
rpcpassword=testpass
rpcallowip=127.0.0.1
[test]
rpcbind=127.0.0.1
rpcport=18332

3 - I created a file called "btcrpc.py":

from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
from print import pprint

# rpc_user and rpc_password are set in the bitcoin. conf file
rpc_user = "kk"
rpc_pass = "testpass"
rpc_host = "127.0.0.1"
rpc_client = AuthServiceProxy(f'http://frpc_user}:{rpc_pass}@{rpc_host}:18332", timeout=120)

block_count = rpc_client.getblockcount()
print (block_count)

When I run btcrpc.py that's the error:

Why is it unauthorized?

What am I doing wrong?

Fawkes
  • 3
  • 3

1 Answers1

0
from bitcoinrpc.authproxy import AuthServiceProxy

rpc_user ="kk"
rpc_password="testpass"
rpc_port = "18332"

rpc_client = AuthServiceProxy("http://%s:%s@127.0.0.1:%s"%(rpc_user, rpc_password, rpc_port))

block_count = rpc_client.getblockcount()
print(block_count)

This code works and result was 2377824 for testnet. Although I would suggest not using python-bitcoinrpc for anything on mainnet. Instead you can use python requests and Bitcoin Core JSON-RPC.

  • Not working for me, maybe there is something wrong with my .config file or with its location. Any idea? – Fawkes Oct 23 '22 at 10:32
  • I managed to solve it by using your code and changing the .conf file: ``` testnet=1 daemon=1 prune=1000 fallbackfee=0.00001 server=1 [test] rpcport=18332 rpcallowip=127.0.0.1 rpcbind=127.0.0.1:18332 rpcuser=kk rpcpassword=testpass ``` – Fawkes Oct 23 '22 at 11:36