Bitcoind

比特幣 RPC 連接

  • January 8, 2021

我正在嘗試使用 python3 將比特幣交易插入 MongoDB。下面是我的程式碼:

import pymongo
import sys
import json
import time
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException

rpc_connection = AuthServiceProxy("http://xxx:xxx@ipaddress:port")

def getTransaction():
   addresses = []
   txa = []
   commands = [ [ "getblockhash", height] for height in range(400000,550000) ]
   #print(commands)

   block_hashes = rpc_connection.batch_(commands)
   blocks = rpc_connection.batch_([ [ "getblock", h ] for h in block_hashes ])
   print(blocks)
   for txpre in blocks:
       #print(txpre)
       for txs in txpre["tx"]:
           txa.append(txs)
   trans = conTransaction(txa)
   return trans

我收到以下錯誤:

Traceback (most recent call last):
 File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
   self.run()
 File "/usr/lib/python3.6/threading.py", line 864, in run
   self._target(*self._args, **self._kwargs)
 File "Test06.py", line 252, in getBTCTransaction
   block_hashes = rpc_connection.batch_(commands)
 File "/home/administrator/.local/lib/python3.6/site-packages/bitcoinrpc/authproxy.py", line 165, in batch_
   'Content-type': 'application/json'})
 File "/usr/lib/python3.6/http/client.py", line 1239, in request
   self._send_request(method, url, body, headers, encode_chunked)
 File "/usr/lib/python3.6/http/client.py", line 1285, in _send_request
   self.endheaders(body, encode_chunked=encode_chunked)
 File "/usr/lib/python3.6/http/client.py", line 1234, in endheaders
   self._send_output(message_body, encode_chunked=encode_chunked)
 File "/usr/lib/python3.6/http/client.py", line 1065, in _send_output
   self.send(chunk)
 File "/usr/lib/python3.6/http/client.py", line 986, in send
   self.sock.sendall(data)
BrokenPipeError: [Errno 32] Broken pipe

我檢查了 bitcoind rpc 連接,它已連接。

block_hashes = rpc_connection.batch_(commands)此程式碼行給出錯誤。有人可以告訴我有什麼問題嗎?在遠端伺服器上工作。

我找到了解決方案。在對比特幣配置文件進行更改後,我只是減少了要插入的記錄範圍。RPC 伺服器無法一次處理大量數據。否則會出現伺服器宕機問題,連接失敗。一次最多插入 100 條記錄或更少。還有一件事,通過下面給出的停止和啟動命令繼續重啟 bitcoind RPC 伺服器:

sudo killall bitcoind
bitcoind -daemon -rpcuser=xxx -rpcpassword=xxx -txindex -rpcallowip=youripaddress

引用自:https://bitcoin.stackexchange.com/questions/82998