1

My problem i that I'm trying to communicate my python code with bitcoin-cli (Bitcoin ABC) and I'm suppose to set rpcuser, and rpcpassword on bitcoin.conf file on .bitcoin folder. Right? Well, when I do so I still can call bitcoin-cli without prompting any password, and, worst, the code does not work:

from __future__ import print_function
import requests, json

rpcPort = 8332
rpcUser = 'xxxxx'

rpcPassword = 'xxxxx'
serverURL = 'http://' + rpcUser + ':' + rpcPassword + '@localhost:' + str(rpcPort)

headers = {'content-type': 'application/json'}
payload = json.dumps({"method": 'getblock', "params": ["0000000000005e5fd51f764d230441092f1b69d1a1eeab334c5bb32412e8dc51"], "jsonrpc": "2.0"})
response = requests.get(serverURL, headers=headers, data=payload)
print(response.json()['result'])

My results:

Traceback (most recent call last):
  File "/home/phglz/Desktop/pruebapython.py", line 17, in <module>
    print(response.json()['result'])
  File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 892, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "/home/phglz/Desktop/pruebapython.py"]
[dir: /home/phglz/Desktop]
[path: /home/phglz/bin:/home/phglz/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin]

I'm quite lost now as you can see I'm not an expert on this. My bitcoin.conf looks like:

rpcuser=xxxx
rpcpassword=xxxx
daemon=1
keypool=10000
maxuploadtarget=20
maxconnections=16

Thanks in advance for your valued help.

1 Answer 1

1

I'm suppose to set rpcuser, and rpcpassword on bitcoin.conf file on .bitcoin folder. Right?

Kind of. rpcuser and rpcpassword have been deprecated in favor of cookie authentication (temporary username and password placed in the .cookie file) and rpcauth.

Well, when I do so I still can call bitcoin-cli without prompting any password,

It's not supposed to. bitcoin-cli reads the data directory to get the information it needs. It will read your bitcoin.conf file for the username and password or it will use the .cookie file.

the code does not work

Your code is completely wrong.

serverURL = 'http://' + rpcUser + ':' + rpcPassword + '@localhost:' + str(rpcPort)

This is incorrect; that's not how authentication works for this.

Bitcoin Core uses HTTP basic auth; the username and password are not part of the URL like that. Since you are using Python Requests, here is their documentation on using HTTP Basic Authentication: http://docs.python-requests.org/en/master/user/authentication/#basic-authentication

1
  • Thank you master. I just jumped to bitcoinrpc library, working good so far. I'll be back to this topic when the time comes. It seems it will be a long way. Thanks again. Feb 20, 2018 at 0:10

Your Answer

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

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