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

refactor BaseProxy __init__ method #145

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
60 changes: 32 additions & 28 deletions bitcoin/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def __init__(self,
# __conn being created __del__() can detect the condition and handle it
# correctly.
self.__conn = None


authpair = None
if service_url is None:
# Figure out the path to the bitcoin.conf file
if btc_conf_file is None:
Expand All @@ -140,27 +141,32 @@ def __init__(self,
else:
btc_conf_file = os.path.expanduser('~/.bitcoin')
btc_conf_file = os.path.join(btc_conf_file, 'bitcoin.conf')

# Extract contents of bitcoin.conf to build service_url
with open(btc_conf_file, 'r') as fd:
# Bitcoin Core accepts empty rpcuser, not specified in btc_conf_file
conf = {'rpcuser': ""}
for line in fd.readlines():
if '#' in line:
line = line[:line.index('#')]
if '=' not in line:
continue
k, v = line.split('=', 1)
conf[k.strip()] = v.strip()

if service_port is None:
service_port = bitcoin.params.RPC_PORT
conf['rpcport'] = int(conf.get('rpcport', service_port))
conf['rpchost'] = conf.get('rpcconnect', 'localhost')

service_url = ('%s://%s:%d' %
('http', conf['rpchost'], conf['rpcport']))

if service_port is None:
service_port = bitcoin.params.RPC_PORT

# Attempt to extract contents from bitcoin.conf or a cookie file
# Bitcoin Core accepts an empty rpcuser
conf = {'rpcuser': ""}
try:
with open(btc_conf_file, 'r') as fd:
for line in fd.readlines():
if '#' in line:
line = line[:line.index('#')]
if '=' not in line:
continue
k, v = line.split('=', 1)
conf[k.strip()] = v.strip()
except IOError:
pass

conf['rpcport'] = int(conf.get('rpcport', service_port))
conf['rpchost'] = conf.get('rpcconnect', 'localhost')
service_url = ('%s://%s:%d' %
('http', conf['rpchost'], conf['rpcport']))

if 'rpcpassword' in conf:
authpair = "%s:%s" % (conf['rpcuser'], conf['rpcpassword'])
else:
cookie_dir = os.path.dirname(btc_conf_file)
if bitcoin.params.NAME != "mainnet":
cookie_dir = os.path.join(cookie_dir, bitcoin.params.NAME)
Expand All @@ -169,12 +175,8 @@ def __init__(self,
with open(cookie_file, 'r') as fd:
authpair = fd.read()
except IOError as err:
if 'rpcpassword' in conf:
authpair = "%s:%s" % (conf['rpcuser'], conf['rpcpassword'])

else:
raise ValueError('Cookie file unusable (%s) and rpcpassword not specified in the configuration file: %r' % (err, btc_conf_file))

raise ValueError('Cookie file unusable (%s) and rpcpassword not specified in the configuration file: %r' % (err, btc_conf_file))

self.__service_url = service_url
self.__url = urlparse.urlparse(service_url)

Expand All @@ -186,6 +188,8 @@ def __init__(self,
else:
port = self.__url.port
self.__id_count = 0
if authpair is None:
authpair = "%s:%s" % (self.__url.username, self.__url.password)
authpair = authpair.encode('utf8')
self.__auth_header = b"Basic " + base64.b64encode(authpair)

Expand Down