(lachesisの文脈投稿)
JSON-RPCパスワードスレッドでサトシがほのめかした、BitcoinのJSON-RPCにおけるいわゆる「Multiple Invocation」サポートに気づいた。
大量のアドレスへの支払いを1分に2回ポーリングするサイトを運営しているので、興味を引かれた。まず、これはJSON-RPC 2.0の「Batch」サポートとは違う。Batchではリクエストを配列で送信し、レスポンスも同様に受信する: Code:request = [ {“jsonrpc”: “2.0”, “method”: “sum”, “params”: [1,2,4], “id”: “1”}, {“jsonrpc”: “2.0”, “method”: “subtract”, “params”: [42,23], “id”: “2”}, {“jsonrpc”: “2.0”, “method”: “get_data”, “id”: “9”} ] response = [ {“jsonrpc”: “2.0”, “result”: 7, “id”: “1”}, {“jsonrpc”: “2.0”, “result”: 19, “id”: “2”}, {“jsonrpc”: “2.0”, “result”: [“hello”, 5], “id”: “9”} ]
代わりに別の方式で、Pythonでレスポンスをパースする方法が分からない。Bitcoin RPCサーバーへのtelnetセッションの画面キャプチャを示す: Code:$ telnet localhost 8332 Trying 127.0.0.1… Connected to localhost. Escape character is ’^]’. POST / HTTP/1.1 Content-Type: text/plain Content-Length: 97
{“params”:[],“id”:1,“method”:“getconnectioncount”} {“params”:[],“id”:2,“method”:“getdifficulty”} HTTP/1.1 200 OK Connection: close Content-Length: 33 Content-Type: application/json Date: Sat, 08 Jul 2006 12:04:08 GMT Server: json-rpc/1.0
{“result”:8,“error”:null,“id”:1} HTTP/1.1 200 OK Connection: close Content-Length: 49 Content-Type: application/json Date: Sat, 08 Jul 2006 12:04:08 GMT Server: json-rpc/1.0
{“result”:181.5432893640505,“error”:null,“id”:2} Connection closed by foreign host. 見ての通り、サーバーは(予想されるような)2行を連結した1つのレスポンスではなく、2つの完全なHTTP 200レスポンスを返す。
Pythonで半自動的にこれをパースする方法が分からない。urllib2もhttplibも最初のレスポンスの後に戻ってきて、2つ目を捨ててしまう。
この問題に遭遇した人はいるだろうか?この奇妙なマルチリクエスト動作を処理できるPythonライブラリを知っている人は?
さて、前回の投稿から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}]
ヘッダーが繰り返されるのは明らかにバグだ。
1.0仕様に従おうとしていた: http://json-rpc.org/wiki/specification 複数呼び出しが規定されていた。
このようなことを意味していると思うが、確信はない:
Post:
{"method": "postMessage", "params": ["Hello all!"], "id": 99}
{"method": "postMessage", "params": ["I have a question:"], "id": 101}
Reply:
{"result": 1, "error": null, "id": 99}
{"result": 1, "error": null, "id": 101}
エラー応答にHTTPステータス500を返すべきだとどこかで見た気がするが、思い出せない。複数のレスポンスを含み、そのうちの1つがエラーの場合、全体のステータスが500になるのだろうか。おそらくそうだろう。常に200を返すべきかもしれない。500が問題を引き起こしているような指摘があったと思う。
これはおそらく0.3.3の後に修正される。それまでは単一呼び出しを使用してくれ。JSON-RPCパッケージで複数呼び出しをサポートしているものがあるかどうか疑問だが、おそらくないだろう。
修正を試みる前に、複数呼び出しがどのように機能すべきか(そもそも機能すべきなのか)、またエラーレスポンスにHTTPステータス500を返すのが正しいかどうかを、もう少し明確にできると良いのだが。