さて、前回の投稿から1時間も経っていないのは分かっているが、この問題を自分で解決することにした。
もっと良い解決策があれば教えてほしいが、これが私のバージョン0.1の試みだ: http://www.alloscomp.com/bitcoin/btcjsonrpc.pys
気に入ったか、嫌いか、明らかな欠陥があるか教えてほしい。ソースに導入ドキュメントがあるが、テストスイートのコードを示す: Code: from btcjsonrpc import Service s = Service() print ‘preparing getbalance; id:‘,s.getbalance() # Each call returns its ID so you can find it later in the results print ‘preparing getdifficulty; id:‘,s.getdifficulty() print ‘preparing listreceivedbyaddress; id:‘,s.listreceivedbyaddress(10000) # Call with a parameter print ‘preparing getbalance; id:‘,s.getbalance(id=‘getbalance 2’) # You can also specify your own ID print ‘\nexecuting call\n\nresults:’ results = s() # Get the results by calling the Service object for id,value in results.iteritems(): print id,value # If you’d prefer to work directly with the JSON responses instead of a dict of IDs, then access the list Service.responses. print ‘\njson responses’ print s.responses
出力は以下の通り(docstrを含む): Code:$ ./btcjsonrpc.py Socket-based, Bitcoin-compatible JSON-RPC v1.0 client. By: Eric Swanson (http://www.alloscomp.com/bitcoin) Version 0.1, July 21, 2010 Don’t use this for one-off request->response pairs. Use something like the reference python-jsonrpc library, or urllib2 + json. This client is hackish, but it works for me (and has sped up my JSON-RPC accesses tremendously).
For details of WHY exactly I felt the need to redo python-jsonrpc using a raw socket, check out the follow forum post: topic 528
Usage is fairly straightforward, and a code sample can be found below the library code (in the if name==‘main’ clause).
preparing getbalance; id: jss-1 preparing getdifficulty; id: jss-2 preparing listreceivedbyaddress; id: jss-3 preparing getbalance; id: getbalance 2
executing call
results: jss-2 181.543289364 jss-3 ] getbalance 2 2345.94 jss-1 2345.94
json responses [{u’id’: u’jss-2’, u’result’: 181.54328936405051, u’error’: None}, {u’id’: u’jss-3’, u’result’: ], u’error’: None}, {u’id’: u’getbalance 2’, u’result’: 2345.9400000000001, u’error’: None}, {u’id’: u’jss-1’, u’result’: 2345.9400000000001, u’error’: None}]