getNeighbors - 获取邻居节点信息
使用getNeighbors调用获取邻居节点及其活动状态。
调用参数
无
返回结果
getNeighbors调用返回一个JSON对象,结构如下:
neighbors:邻居节点对象数组,对象结构如下:address:地址
connectionType:连接类型
numberOfAllTransactions:交易数量
numberOfRandomTransactionRequests:随机交易请求数量
numberOfNewTransactions:新交易数量
numberOfInvalidTransactions:无效交易数量
numberOfStaleTransactions:不稳定交易数量
numberOfSentTransactions:已发送交易数量
duration:请求执行用时,单位:毫秒
调用成功的HTTP状态码为200,响应示例如下:
{
"neighbors": [{
"address": "/8.8.8.8:14265",
"numberOfAllTransactions": 158,
"numberOfRandomTransactionRequests": 271,
"numberOfNewTransactions": 956,
"numberOfInvalidTransactions": 539,
"numberOfStaleTransactions": 663,
"numberOfSentTransactions": 672,
"connectiontype": "TCP"
}],
"duration": 735
}
调用失败的HTTP状态码为400,响应示例如下:
{
"error": "'command' parameter has not been specified"
}
示例代码
Python
import urllib2
import json
command = {"command": "getNeighbors"}
stringified = json.dumps(command)
headers = {
'content-type': 'application/json',
'X-IOTA-API-Version': '1'
}
request = urllib2.Request(url="http://localhost:14265", data=stringified, headers=headers)
returnData = urllib2.urlopen(request).read()
jsonData = json.loads(returnData)
print jsonData
Node.js
var request = require('request');
var command = {"command": "getNeighbors"}
var options = {
url: 'http://localhost:14265',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-IOTA-API-Version': '1',
'Content-Length': Buffer.byteLength(JSON.stringify(command))
},
json: command
};
request(options, function (error, response, data) {
if (!error && response.statusCode == 200) {
console.log(data);
}
});
命令行
curl http://localhost:14265 \
-X POST \
-H 'Content-Type: application/json' \
-H 'X-IOTA-API-Version: 1' \
-d '{"command": "getNeighbors"}'