2

I'm running a testnet node. I can use bitcoin-cli just fine:

>bitcoin-cli.exe getinfo
{
    "version" : 100000,
    "protocolversion" : 70002,
    "walletversion" : 60000,
[... snipped]

But when I run this python code:

from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
import logging

rpc_user = "rpcuser"
rpc_password = "xxxxx"

logging.basicConfig()
logging.getLogger("BitcoinRPC").setLevel(logging.DEBUG)

rpc_connection = AuthServiceProxy("http://%s:%[email protected]:8332/" % (rpc_user, rpc_password))
print(rpc_connection.getinfo())

I get this error:

DEBUG:BitcoinRPC:-1-> getinfo []

Traceback (most recent call last):
  File "<path>\rawtransactiondemo\raw.py", line 11, in <module>
    print(rpc_connection.getinfo())
[... snipped]
  File "C:\Python27\lib\json\decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

This is my bitcoin.conf:

testnet=1
rpcuser=bitcoinrpc
rpcpassword=xxxxx
daemon=1
rpcport=8332
rpcallowip=192.168.1.39
bind=192.168.1.39

I'm using the python-bitcoinrpc library.

3 Answers 3

1

I added some code to debug what bitcoin was sending back - it turned out to be a 401 error.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML>
<HEAD>
<TITLE>Error</TITLE>
<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>
</HEAD>
<BODY><H1>401 Unauthorized.</H1></BODY>
</HTML>

Why was this triggered? If you look in my bitcoin.conf, and my python code, you can see that the username is different (bitcoinrpc vs. rpcuser). Changing that fixed it.

Anyways, if you get the error ValueError: No JSON object could be decoded, this might be your problem.

1

For anyone else finding this, and the problem was NOT a typo like in the accepted answer:

The issue for me was simply that I had my conf file in the wrong location.

I installed this on Ubuntu 20.04 and saved the conf file in /.bitcoin/bitcoin.conf, but it was looking in /home/user, which was actually ~.bitcoin/bitcoin.conf.

Saving the conf file in the correct location resolved everything. I could now use bitcoin-cli without sudo, and the python scripts using python-bitcoinrpc.

0

If you're using the testnet, rpcport should be 18332.

2
  • 3
    It's not required; you just need to use the same ports on both ends. In my bitcoin.conf, I set rpcport to 8332.
    – Nick ODell
    Mar 13, 2015 at 14:15
  • @NickODell That would explain my testnet connectivity issues; I didn't realize the conf rpcport=8332 flag set port 8332 for mainnet AND testnet Mar 15, 2015 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.