比特币RPC连接

时间:2018-12-18 10:19:24

标签: python-3.x rpc bitcoind

我正在尝试使用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)此代码行给出了错误。有人可以告诉我怎么了吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。在更改了比特币配置文件后,我只是减小了要插入的记录范围。 RPC服务器无法一次处理数据负载。否则,将发生服务器关闭问题,并且连接失败。一次最多插入100条记录,或少于一次。还有一件事,请通过下面给出的停止和启动命令来继续重启bitcoind RPC服务器:

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