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) ]
#print(commands)

block_hashes = rpc_connection.batch_(commands)
#print(block_hashes)
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)

# 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(t)
    try:
        print(t)
        mycol.insert(t)

    except:
        print("not unique")

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

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

            print("Block:",num+n*1)
            getBTCTransaction(num+n*1,num+(n+1)*1)

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>
    getBTCTransaction(num+n*1,num+(n+1)*1)
  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
    response.begin()
  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?

Article First Published here