Skip to content
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
38 changes: 29 additions & 9 deletions bitcoinrpc/authproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,41 @@ def EncodeDecimal(o):
return round(o, 8)
raise TypeError(repr(o) + " is not JSON serializable")

class SharedConnection(object):
def __init__(self, url, timeout):
self.__url = url
self.__timeout = timeout
self.__conn = None

def request(self, *args, **kwargs):
retries = 0
while True:
if self.__conn is None:
port = self.__url.port or 80
if self.__url.scheme == 'https':
self.__conn = httplib.HTTPSConnection(self.__url.hostname, port,
timeout=self.__timeout)
else:
self.__conn = httplib.HTTPConnection(self.__url.hostname, port,
timeout=self.__timeout)
try:
return self.__conn.request(*args, **kwargs)
except (httplib.NotConnected, httplib.ImproperConnectionState):
if retries:
raise
self.__conn = None
retries += 1

def getresponse(self):
return self.__conn.getresponse()

class AuthServiceProxy(object):
__id_count = 0

def __init__(self, service_url, service_name=None, timeout=HTTP_TIMEOUT, connection=None):
self.__service_url = service_url
self.__service_name = service_name
self.__url = urlparse.urlparse(service_url)
if self.__url.port is None:
port = 80
else:
port = self.__url.port
(user, passwd) = (self.__url.username, self.__url.password)
try:
user = user.encode('utf8')
Expand All @@ -103,12 +127,8 @@ def __init__(self, service_url, service_name=None, timeout=HTTP_TIMEOUT, connect
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)
self.__conn = SharedConnection(self.__url, timeout)

def __getattr__(self, name):
if name.startswith('__') and name.endswith('__'):
Expand Down