Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a parameter to keep or not the connection. #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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