Skip to content

Commit

Permalink
Merge pull request jgarzik#73 from jgarzik/python-bitcoinrpc
Browse files Browse the repository at this point in the history
Add a parameter to keep or not the connection.
  • Loading branch information
bjarnemagnussen authored Feb 5, 2019
2 parents 95bb5c0 + 2fa3d69 commit 4abf833
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions bitcoinrpc/authproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def EncodeDecimal(o):
class AuthServiceProxy(object):
__id_count = 0

def __init__(self, service_url, service_name=None, timeout=HTTP_TIMEOUT, connection=None):
def __init__(self, service_url, service_name=None, timeout=HTTP_TIMEOUT, connection=None, keep_connection=True):
self.__service_url = service_url
self.__service_name = service_name
self.__url = urlparse.urlparse(service_url)
Expand All @@ -101,16 +101,20 @@ def __init__(self, service_url, service_name=None, timeout=HTTP_TIMEOUT, connect
self.__auth_header = b'Basic ' + base64.b64encode(authpair)

self.__timeout = timeout

if connection:
# Callables re-use the connection of the original proxy
self.__conn = connection
elif self.__url.scheme == 'https':
self.__conn = httplib.HTTPSConnection(self.__url.hostname, port,
timeout=timeout)
self.__keep_connection = keep_connection

if self.__keep_connection:
if connection:
# Callables re-use the connection of the original proxy
self.__conn = connection
elif self.__url.scheme == 'https':
self.__conn = httplib.HTTPSConnection(self.__url.hostname, port,
timeout=timeout)
else:
self.__conn = httplib.HTTPConnection(self.__url.hostname, port,
timeout=timeout)
else:
self.__conn = httplib.HTTPConnection(self.__url.hostname, port,
timeout=timeout)
self.__conn = None

def __getattr__(self, name):
if name.startswith('__') and name.endswith('__'):
Expand All @@ -129,20 +133,34 @@ def __call__(self, *args):
'method': self.__service_name,
'params': args,
'id': AuthServiceProxy.__id_count}, default=EncodeDecimal)
self.__conn.request('POST', self.__url.path, postdata,

if not self.__keep_connection:
if self.__url.scheme == 'https':
__conn = httplib.HTTPSConnection(self.__url.hostname, self.__url.port,
timeout=self.__timeout)
else:
__conn = httplib.HTTPConnection(self.__url.hostname, self.__url.port,
timeout=self.__timeout)
else:
__conn = self.__conn

__conn.request('POST', self.__url.path, postdata,
{'Host': self.__url.hostname,
'User-Agent': USER_AGENT,
'Authorization': self.__auth_header,
'Content-type': 'application/json'})
self.__conn.sock.settimeout(self.__timeout)
__conn.sock.settimeout(self.__timeout)

response = self._get_response()
if response.get('error') is not None:
raise JSONRPCException(response['error'])
elif 'result' not in response:
raise JSONRPCException({
'code': -343, 'message': 'missing JSON-RPC result'})


if not self.__keep_connection:
__conn.close()

return response['result']

def batch_(self, rpc_calls):
Expand Down

0 comments on commit 4abf833

Please sign in to comment.