moved Authorization header handling out of util.jsonrpc
authorForrest Voight <forrest@forre.st>
Thu, 15 Mar 2012 21:37:55 +0000 (17:37 -0400)
committerForrest Voight <forrest@forre.st>
Thu, 15 Mar 2012 21:38:50 +0000 (17:38 -0400)
p2pool/main.py
p2pool/util/jsonrpc.py

index bccfdc5..ac0b297 100644 (file)
@@ -3,6 +3,7 @@ from __future__ import division
 import ConfigParser
 import StringIO
 import argparse
+import base64
 import os
 import random
 import sys
@@ -53,7 +54,7 @@ def main(args, net, datadir_path, merged_urls, worker_endpoint):
         # connect to bitcoind over JSON-RPC and do initial getmemorypool
         url = 'http://%s:%i/' % (args.bitcoind_address, args.bitcoind_rpc_port)
         print '''Testing bitcoind RPC connection to '%s' with username '%s'...''' % (url, args.bitcoind_rpc_username)
-        bitcoind = jsonrpc.Proxy(url, (args.bitcoind_rpc_username, args.bitcoind_rpc_password))
+        bitcoind = jsonrpc.Proxy(url, dict(Authorization='Basic ' + base64.b64encode(args.bitcoind_rpc_username + ':' + args.bitcoind_rpc_password)))
         good = yield deferral.retry('Error while checking bitcoind identity:', 1)(net.PARENT.RPC_CHECK)(bitcoind)
         if not good:
             print >>sys.stderr, "    Check failed! Make sure that you're connected to the right bitcoind with --bitcoind-rpc-port!"
@@ -212,7 +213,7 @@ def main(args, net, datadir_path, merged_urls, worker_endpoint):
         
         @defer.inlineCallbacks
         def set_merged_work(merged_url, merged_userpass):
-            merged_proxy = jsonrpc.Proxy(merged_url, (merged_userpass,))
+            merged_proxy = jsonrpc.Proxy(merged_url, dict(Authorization='Basic ' + base64.b64encode(merged_userpass)))
             while True:
                 auxblock = yield deferral.retry('Error while calling merged getauxblock:', 1)(merged_proxy.rpc_getauxblock)()
                 pre_merged_work.set(dict(pre_merged_work.value, **{auxblock['chainid']: dict(
index d29cdf0..d6d34de 100644 (file)
@@ -1,6 +1,5 @@
 from __future__ import division
 
-import base64
 import json
 
 from twisted.internet import defer
@@ -26,25 +25,20 @@ class Error(Exception):
         }
 
 class Proxy(object):
-    def __init__(self, url, auth=None, timeout=5):
+    def __init__(self, url, headers={}, timeout=5):
         self._url = url
-        self._auth = auth
+        self._headers = headers
         self._timeout = timeout
     
     @defer.inlineCallbacks
     def callRemote(self, method, *params):
         id_ = 0
         
-        headers = {
-            'Content-Type': 'application/json',
-        }
-        if self._auth is not None:
-            headers['Authorization'] = 'Basic ' + base64.b64encode(':'.join(self._auth))
         try:
             data = yield client.getPage(
                 url=self._url,
                 method='POST',
-                headers=headers,
+                headers=dict(self._headers, **{'Content-Type': 'application/json'}),
                 postdata=json.dumps({
                     'jsonrpc': '2.0',
                     'method': method,