Search code examples
pythonpython-3.xbitcoinbitcoindbitcoin-testnet

Bitcoin core on osx and bitcoinlib socket error


I have some problem about my study..

When I make python file about bitcoinlib like it

from bitcoin.rpc import RawProxy

p = RawProxy()

info = p.getblockchaininfo()

print(info['blocks'])

But it shows me some error like

>> python3 rpc_example.py
Traceback (most recent call last):
  File "rpc_example.py", line 5, in <module>
    info = p.getblockchaininfo()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/bitcoin/rpc.py", line 315, in <lambda>
    f = lambda *args: self._call(name, *args)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/bitcoin/rpc.py", line 231, in _call
    self.__conn.request('POST', self.__url.path, postdata, headers)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
from bitcoin.rpc import RawProxy
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 936, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 724, in create_connection
    raise err
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 713, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused

ConnectionRefusedError... this is my bitcoin.conf file

alertnotify=myemailscript.sh "Alert: %s"
server=1
rpcuser=bitcoinrpc
rpcpassword=any_long_random_password
txindex=1
testnet=1
daemon=1

When I start -testnet, I can execute some API like getblockchaininfo or getblockhash like

bitcoin-cli getblockchaininfo
{
  "chain": "test",
  "blocks": 1863439,
  "headers": 1863439,
  "bestblockhash": "00000000133f751916630063f86f5b27cd6b5871ce70a5fcba7112a111941706",
  "difficulty": 1,
  "mediantime": 1602990284,
  "verificationprogress": 0.999998381271931,
  "initialblockdownload": false,
  "chainwork": "0000000000000000000000000000000000000000000001d9736dcfa64f3ea8bf",
  "size_on_disk": 28355982038,
  "pruned": false,
  "softforks": {
    "bip34": {
      "type": "buried",
      "active": true,
      "height": 21111
    },
    "bip66": {
      "type": "buried",
      "active": true,
      "height": 330776
    },
    "bip65": {
      "type": "buried",
      "active": true,
      "height": 581885
    },
    "csv": {
      "type": "buried",
      "active": true,
      "height": 770112
    },
    "segwit": {
      "type": "buried",
      "active": true,
      "height": 834624
    }
  },
  "warnings": "Warning: unknown new rules activated (versionbit 28)"
}

But I can't execute on python... Please help me TT


Solution

  • I solved it !!

    I did not check my bitcoin.conf about testnet When I add some comments in my bitcoin.conf

    alertnotify=myemailscript.sh "Alert: %s"
    server=1
    rpcuser=bitcoinrpc
    rpcpassword=any_long_random_password
    txindex=1
    testnet=1
    daemon=1
    
    [testnet]
    rpcallowip=*
    rpcport=18332
    

    below the [testnet] two line was required.. when you like me Do add this line!! thanks