原始文献 / 论坛帖 / Command Line and JSON-RPC

Command Line and JSON-RPC

Bitcointalk · 2010-02-23 22:15:41 UTC

AuthorSatoshi Nakamoto
TitleCommand Line and JSON-RPC
Date2010-02-23 22:15:41 UTC
ForumBitcointalk · 原文

SVN 上的 0.2.6 版本现在可以作为守护进程运行,并通过命令行或 JSON-RPC 来控制。

在 Linux 上需要安装 libgtk2.0-0,但不需要运行 GUI。希望 gtk 可以在不安装窗口系统的情况下装上。

以守护进程方式启动的命令是:
bitcoin -daemon [switches...]

或者,在正常运行 UI 的同时也想通过命令行或 JSON-RPC 控制它,请使用 "-server" 开关。
bitcoin -server [switches...]

使用任一开关,它都会运行一个 HTTP JSON-RPC 服务器,接受 127.0.0.1:8332 上的本地套接字连接。端口绑定在 loopback 上,只能从本机访问,但可以是本机的任意账户,不限于运行它的那个用户。

要从命令行控制它,接口是:一个不带任何开关的命令名,后面跟参数(如果有)。
bitcoin <command> [params...]

例如:
bitcoin getinfo
bitcoin getdifficulty
bitcoin setgenerate true
bitcoin stop

它是一个简单的 JSON-RPC 客户端,会打印出 JSON 结果。命令列表请看 rpc.cpp。

Web 应用或任何自动化程序通常会直接使用 JSON-RPC,而不是命令行。各主流语言都有 JSON-RPC 库。在 PHP、Python 这样的脚本语言里,语法跟调用本地函数一样自然。

英文原文

Version 0.2.6 on SVN can now run as a daemon and be controlled by command line or JSON-RPC.

On Linux it needs libgtk2.0-0 installed, but does not need a GUI running.  Hopefully gtk can be installed without having a windowing system installed.

The command to start as a daemon is:
bitcoin -daemon [switches...]

Or, to run the UI normally and also be able to control it from command line or JSON-RPC, use the "-server" switch.
bitcoin -server [switches...]

With either switch, it runs an HTTP JSON-RPC server that accepts local socket connections on 127.0.0.1:8332.  The port is bound to loopback and can only be accessed from the local machine, but from any account, not just the user it's running under.

To control it from the command line, the interface is a command name without any switches, followed by parameters if any.
bitcoin <command> [params...]

For example:
bitcoin getinfo
bitcoin getdifficulty
bitcoin setgenerate true
bitcoin stop

It's a simple JSON-RPC client and prints the JSON result.  Look at rpc.cpp for the list of commands.

Web apps or anything automated will normally use JSON-RPC directly, not command line.  There are JSON-RPC libraries for all the major languages.  In script languages like PHP and Python the syntax is as natural as calling a local function.