The Chinese (Simplified) wiki is no longer maintained and is in read-only mode. Information contained within may be inaccurate or outdated. Please go to the English wiki for more up to date information.
从英文自动翻译:中文(简体)维基不再维护,处于只读模式。其中包含的信息可能不准确或过时。请前往英文维基获取更多最新信息。
API reference (JSON-RPC):修订间差异
以内容“== 操控 bitcoin 软件 == 运行 ''bitcoind'' 或 ''bitcoin -server''. 你可以通过命令行或[http://json-rpc.org/wiki/specification| HTTP JSON-RPC] 命令来...”创建新页面 |
无编辑摘要 |
||
第28行: | 第28行: | ||
Bitcoin supports SSL (https) JSON-RPC connections beginning with version 0.3.14. See the [[Enabling SSL on original client daemon|rpcssl wiki page]] for setup instructions and a list of all bitcoin.conf configuration options. --> | Bitcoin supports SSL (https) JSON-RPC connections beginning with version 0.3.14. See the [[Enabling SSL on original client daemon|rpcssl wiki page]] for setup instructions and a list of all bitcoin.conf configuration options. --> | ||
你需要和你使用语言对应的[http://json-rpc.org/wiki/implementations|库]来访问服务器 | |||
<!-- | <!-- | ||
To access the server you should find a [http://json-rpc.org/wiki/implementations|suitable library] for your language. --> | To access the server you should find a [http://json-rpc.org/wiki/implementations|suitable library] for your language. --> | ||
<!-- | |||
== Proper money handling == | == Proper money handling == | ||
See the [[Proper Money Handling (JSON-RPC)|proper money handling page]] for notes on avoiding rounding errors when handling bitcoin values. | See the [[Proper Money Handling (JSON-RPC)|proper money handling page]] for notes on avoiding rounding errors when handling bitcoin values. --> | ||
== Python == | == Python == | ||
[http://json-rpc.org/wiki/python-json-rpc python-jsonrpc] | [http://json-rpc.org/wiki/python-json-rpc python-jsonrpc] 是JSON-RPC在Python上的官方实现。它自动生成RPC调用的Python方法。但为了支持旧版本的Python,它效率地下。 | ||
It automatically generates Python methods for RPC calls. | <!-- It automatically generates Python methods for RPC calls. | ||
However, due to its design for supporting old versions of Python, it is also rather inefficient. | However, due to its design for supporting old versions of Python, it is also rather inefficient. --> | ||
[[User:jgarzik|jgarzik]]创建了[https://github.com/jgarzik/python-bitcoinrpc Python-BitcoinRPC] 分支,它对新版本的Python进行了优化(最低需要 Python 2.6+, 而非3.x). 推荐这个版本。 | |||
<!-- | |||
[[User:jgarzik|jgarzik]] has forked it as [https://github.com/jgarzik/python-bitcoinrpc Python-BitcoinRPC] and optimized it for current versions (at least Python 2.6+, though not 3.x). | [[User:jgarzik|jgarzik]] has forked it as [https://github.com/jgarzik/python-bitcoinrpc Python-BitcoinRPC] and optimized it for current versions (at least Python 2.6+, though not 3.x). | ||
Generally, this version is recommended. | Generally, this version is recommended. --> | ||
While BitcoinRPC lacks a few obscure features from jsonrpc, software using only the ServiceProxy class can be written the same to work with either version the user might choose to install: | 尽管BitcoinRPC缺少jsonrpc的一些特性,在编写仅使用ServiceProxy类的软件时,两个版本可以使用相同的代码 | ||
<!-- | |||
While BitcoinRPC lacks a few obscure features from jsonrpc, software using only the ServiceProxy class can be written the same to work with either version the user might choose to install: --> | |||
<source lang="python"> | <source lang="python"> | ||
第106行: | 第112行: | ||
== PHP == | == PHP == | ||
The [http://jsonrpcphp.org/ JSON-RPC PHP] library also makes it very easy to connect to Bitcoin. For example: | 用[http://jsonrpcphp.org/ JSON-RPC PHP]库连接Bitcoin非常简单,例如: | ||
<!-- | |||
The [http://jsonrpcphp.org/ JSON-RPC PHP] library also makes it very easy to connect to Bitcoin. For example: --> | |||
<source lang="php"> | <source lang="php"> | ||
第121行: | 第129行: | ||
== Java == | == Java == | ||
The easiest way to tell Java to use HTTP Basic authentication is to set a default Authenticator: | 让Java使用HTTP基本认证的最简单方法是创建default Authenticator | ||
<!-- | |||
The easiest way to tell Java to use HTTP Basic authentication is to set a default Authenticator: --> | |||
<source lang="java"> | <source lang="java"> | ||
第134行: | 第144行: | ||
</source> | </source> | ||
Once that is done, any JSON-RPC library for Java (or ordinary URL POSTs) may be used to communicate with the Bitcoin server. | 这样一来,任何JSON-RPC Java库(或 URL POSTs)都可以和Bitcoin 服务器通信了。 | ||
<!-- | |||
Once that is done, any JSON-RPC library for Java (or ordinary URL POSTs) may be used to communicate with the Bitcoin server. --> | |||
== Perl == | == Perl == | ||
The JSON::RPC package from CPAN can be used to communicate with Bitcoin. You must set the client's credentials; for example: | 用CPAN 的 JSON::RPC 包可以和Bitcoin通信。首先你必须设置客户端证书,例如: | ||
<!-- | |||
The JSON::RPC package from CPAN can be used to communicate with Bitcoin. You must set the client's credentials; for example: --> | |||
<source lang="perl"> | <source lang="perl"> | ||
第167行: | 第181行: | ||
== .NET (C#) == | == .NET (C#) == | ||
<!-- | |||
The communication with rpc service can be achieved using the standard httprequest/response objects. | The communication with rpc service can be achieved using the standard httprequest/response objects. | ||
A library for serialising and deserialising Json will make your life a lot easier: | A library for serialising and deserialising Json will make your life a lot easier: --> | ||
使用标准的httprequest/response 对象可与rpc服务通信。 | |||
用序列化/反序列化Json库可以轻松许多: | |||
* JayRock for .NET 4.0 | * JayRock for .NET 4.0 | ||
* Json.Net for .NET 2.0 and above | * Json.Net for .NET 2.0 and above | ||
下面的例子使用Json.Net: | |||
<source lang="csharp"> | <source lang="csharp"> | ||
第216行: | 第233行: | ||
</source> | </source> | ||
== | == 命令行 (cURL) == | ||
<!-- You can also send commands and see results using [http://curl.haxx.se/ cURL] or some other command-line HTTP-fetching utility; for example: --> | |||
用[http://curl.haxx.se/ cURL]或其他命令行HTTP-fetching可以发送命令并查看结果,例如: | |||
<source lang="bash"> | <source lang="bash"> | ||
第225行: | 第244行: | ||
</source> | </source> | ||
You will be prompted for your rpcpassword, and then will see something like: | 你将获得你的rpcpassword,然后你会看到类似下面的信息 | ||
<!-- You will be prompted for your rpcpassword, and then will see something like: --> | |||
<source lang="javascript"> | <source lang="javascript"> | ||
第232行: | 第252行: | ||
</source> | </source> | ||
== | == 参看 == | ||
* [[Original_Bitcoin_client/API_Calls_list|API | * [[Original_Bitcoin_client/API_Calls_list|API 调用列表]] | ||
* [[ | * [[运行 Bitcoin]] | ||
* [[Lazy API]] | * [[Lazy API]] | ||
[[Category:Articles using content from the old wiki]] | [[Category:Articles using content from the old wiki]] | ||
[[Category:技术]] | [[Category:技术]] | ||
[[Category: | [[Category:开发文档]] |
2011年5月28日 (六) 15:00的版本
操控 bitcoin 软件
运行 bitcoind 或 bitcoin -server. 你可以通过命令行或HTTP JSON-RPC 命令来操控它.
首先你需要创建bitcoin.conf配置文件并设置rpcuser和rpcpassword;详情请查看运行Bitcoin
示例:
$ ./bitcoind bitcoin server starting $ ./bitcoind help # shows the help text
RPC调用列表将显示
$ ./bitcoind getbalance 2000.00000
JSON-RPC
运行'bitcoin -server'或'bitcoind' 将使bitcoin以HTTP JSON-RPC服务器模式运行,但与其通信时必须使用[1],安全起见,默认情况下服务器只接受同一机器上其他进程的连接。如果你的HTTP或JSON库需要你指定已认证的'realm',请使用'jsonrpc'。
0.3.14版之后版本的Bitcoin支持SSL (https) JSON-RPC连接。配置详情请参考本页面。
你需要和你使用语言对应的[2]来访问服务器
Python
python-jsonrpc 是JSON-RPC在Python上的官方实现。它自动生成RPC调用的Python方法。但为了支持旧版本的Python,它效率地下。
jgarzik创建了Python-BitcoinRPC 分支,它对新版本的Python进行了优化(最低需要 Python 2.6+, 而非3.x). 推荐这个版本。
尽管BitcoinRPC缺少jsonrpc的一些特性,在编写仅使用ServiceProxy类的软件时,两个版本可以使用相同的代码
from jsonrpc import ServiceProxy
access = ServiceProxy("http://user:password@127.0.0.1:8332")
access.getinfo()
access.listreceivedbyaddress(6)
#access.sendtoaddress("11yEmxiMso2RsFVfBcCa616npBvGgxiBX", 10)
Ruby
=begin
Make sure to do:
gem install rest-client
Usage:
h = ServiceProxy.new('http://user:password@127.0.0.1:8332')
puts h.getinfo.call
puts h.getbalance.call 'accname'
=end
require 'json'
require 'rest_client'
class JSONRPCException < RuntimeError
def initialize()
super()
end
end
class ServiceProxy
def initialize(service_url, service_name=nil)
@service_url = service_url
@service_name = service_name
end
def method_missing(name, *args, &block)
if @service_name != nil
name = "%s.%s" % [@service_name, name]
end
return ServiceProxy.new(@service_url, name)
end
def respond_to?(sym)
end
def call(*args)
postdata = {"method" => @service_name, "params" => args, "id" => "jsonrpc"}.to_json
respdata = RestClient.post @service_url, postdata
resp = JSON.parse respdata
if resp["error"] != nil
raise JSONRPCException.new, resp['error']
end
return resp['result']
end
end
PHP
用JSON-RPC PHP库连接Bitcoin非常简单,例如:
require_once 'jsonRPCClient.php';
$bitcoin = new jsonRPCClient('http://user:password@127.0.0.1:8332/');
echo "<pre>\n";
print_r($bitcoin->getinfo()); echo "\n";
echo "Received: ".$bitcoin->getreceivedbylabel("Your Address")."\n";
echo "</pre>";
Java
让Java使用HTTP基本认证的最简单方法是创建default Authenticator
final String rpcuser ="...";
final String rpcpassword ="...";
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication (rpcuser, rpcpassword.toCharArray());
}
});
这样一来,任何JSON-RPC Java库(或 URL POSTs)都可以和Bitcoin 服务器通信了。
Perl
用CPAN 的 JSON::RPC 包可以和Bitcoin通信。首先你必须设置客户端证书,例如:
use JSON::RPC::Client;
use Data::Dumper;
my $client = new JSON::RPC::Client;
$client->ua->credentials(
'localhost:8332', 'jsonrpc', 'user' => 'password' # REPLACE WITH YOUR bitcoin.conf rpcuser/rpcpassword
);
my $uri = 'http://localhost:8332/';
my $obj = {
method => 'getinfo',
params => [],
};
my $res = $client->call( $uri, $obj );
if ($res){
if ($res->is_error) { print "Error : ", $res->error_message; }
else { print Dumper($res->result); }
} else {
print $client->status_line;
}
.NET (C#)
使用标准的httprequest/response 对象可与rpc服务通信。 用序列化/反序列化Json库可以轻松许多:
- JayRock for .NET 4.0
- Json.Net for .NET 2.0 and above
下面的例子使用Json.Net:
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://localhost.:8332");
webRequest.Credentials = new NetworkCredential("user", "pwd");
/// important, otherwise the service can't desirialse your request properly
webRequest.ContentType = "application/json-rpc";
webRequest.Method = "POST";
JObject joe = new JObject();
joe.Add(new JProperty("jsonrpc", "1.0"));
joe.Add(new JProperty("id", "1"));
joe.Add(new JProperty("method", Method));
// params is a collection values which the method requires..
if (Params.Keys.Count == 0)
{
joe.Add(new JProperty("params", new JArray()));
}
else
{
JArray props = new JArray();
// add the props in the reverse order!
for (int i = Params.Keys.Count - 1; i >= 0; i--)
{
.... // add the params
}
joe.Add(new JProperty("params", props));
}
// serialize json for the request
string s = JsonConvert.SerializeObject(joe);
byte[] byteArray = Encoding.UTF8.GetBytes(s);
webRequest.ContentLength = byteArray.Length;
Stream dataStream = webRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse webResponse = webRequest.GetResponse();
... // deserialze the response
命令行 (cURL)
用cURL或其他命令行HTTP-fetching可以发送命令并查看结果,例如:
curl --user user --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getinfo", "params": [] }'
-H 'content-type: text/plain;' http://127.0.0.1:8332/
你将获得你的rpcpassword,然后你会看到类似下面的信息
{"result":{"balance":0.000000000000000,"blocks":59952,"connections":48,"proxy":"","generate":false,
"genproclimit":-1,"difficulty":16.61907875185736,"error":null,"id":"curltest"}