Skip to content

Commit 4abf833

Browse files
Merge pull request jgarzik#73 from jgarzik/python-bitcoinrpc
Add a parameter to keep or not the connection.
2 parents 95bb5c0 + 2fa3d69 commit 4abf833

1 file changed

Lines changed: 31 additions & 13 deletions

File tree

bitcoinrpc/authproxy.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def EncodeDecimal(o):
8080
class AuthServiceProxy(object):
8181
__id_count = 0
8282

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

103103
self.__timeout = timeout
104-
105-
if connection:
106-
# Callables re-use the connection of the original proxy
107-
self.__conn = connection
108-
elif self.__url.scheme == 'https':
109-
self.__conn = httplib.HTTPSConnection(self.__url.hostname, port,
110-
timeout=timeout)
104+
self.__keep_connection = keep_connection
105+
106+
if self.__keep_connection:
107+
if connection:
108+
# Callables re-use the connection of the original proxy
109+
self.__conn = connection
110+
elif self.__url.scheme == 'https':
111+
self.__conn = httplib.HTTPSConnection(self.__url.hostname, port,
112+
timeout=timeout)
113+
else:
114+
self.__conn = httplib.HTTPConnection(self.__url.hostname, port,
115+
timeout=timeout)
111116
else:
112-
self.__conn = httplib.HTTPConnection(self.__url.hostname, port,
113-
timeout=timeout)
117+
self.__conn = None
114118

115119
def __getattr__(self, name):
116120
if name.startswith('__') and name.endswith('__'):
@@ -129,20 +133,34 @@ def __call__(self, *args):
129133
'method': self.__service_name,
130134
'params': args,
131135
'id': AuthServiceProxy.__id_count}, default=EncodeDecimal)
132-
self.__conn.request('POST', self.__url.path, postdata,
136+
137+
if not self.__keep_connection:
138+
if self.__url.scheme == 'https':
139+
__conn = httplib.HTTPSConnection(self.__url.hostname, self.__url.port,
140+
timeout=self.__timeout)
141+
else:
142+
__conn = httplib.HTTPConnection(self.__url.hostname, self.__url.port,
143+
timeout=self.__timeout)
144+
else:
145+
__conn = self.__conn
146+
147+
__conn.request('POST', self.__url.path, postdata,
133148
{'Host': self.__url.hostname,
134149
'User-Agent': USER_AGENT,
135150
'Authorization': self.__auth_header,
136151
'Content-type': 'application/json'})
137-
self.__conn.sock.settimeout(self.__timeout)
152+
__conn.sock.settimeout(self.__timeout)
138153

139154
response = self._get_response()
140155
if response.get('error') is not None:
141156
raise JSONRPCException(response['error'])
142157
elif 'result' not in response:
143158
raise JSONRPCException({
144159
'code': -343, 'message': 'missing JSON-RPC result'})
145-
160+
161+
if not self.__keep_connection:
162+
__conn.close()
163+
146164
return response['result']
147165

148166
def batch_(self, rpc_calls):

0 commit comments

Comments
 (0)