I am trying to insert bitcoin transactions into MongoDB using python3. Below is my code :

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

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

def getBTCTransaction(rangeinit,rangefinish):

myclient = pymongo.MongoClient("mongodb://ipaddress:port")
mydb = myclient["ETHCollection"]
mycol = mydb["BTCtransaction"]

addresses = []
txa = []
commands = [ [ "getblockhash", height] for height in range(rangeinit,rangefinish) ]

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

# insert BTC transactions
for t in trans:
    i = 0
    for x in t["vout"]:
        t["vout"][i]["value"] = int(t["vout"][i]["value"]*100000000)
        i = i+1

        print("not unique")

def conTransaction(txa):
        trans = rpc_connection.batch_([ [ "getrawtransaction", tx ,1] for tx in txa ])
        return trans
        print("connect error")
        return conTransaction(txa)

if __name__ == '__main__':
  if sys.argv[1] == "4":
        for n in range(0,10000):


After inserting only one block's transaction instead of continuing for the next block it is giving an error:

Block: 459722
Traceback (most recent call last):
  File "Test06.py", line 342, in <module>
  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 167, in batch_
    responses = self._get_response()
  File "/home/administrator/.local/lib/python3.6/site-packages/bitcoinrpc/authproxy.py", line 179, in _get_response
    http_response = self.__conn.getresponse()
  File "/usr/lib/python3.6/http/client.py", line 1331, in getresponse
  File "/usr/lib/python3.6/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.6/http/client.py", line 266, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

Can somebody please help me with this error. Why connection is getting closed?

  • Likely your batch of RPC commands is excessively large and is causing an invalid request. – Anonymous Dec 27 '18 at 6:41
  • but its passing only one block transactions at one time then why? – Varsha Khandre Dec 27 '18 at 7:29
  • You’re requesting thousands of transactions with getrawtransaction in a single batch call. – Anonymous Dec 27 '18 at 7:31
  • Okay. Any solution for this problem? – Varsha Khandre Dec 27 '18 at 7:33
  • Break it into multiple small batches. – Anonymous Dec 27 '18 at 7:34

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.