LXD - 如何通过nodejs下的unix socket域连接到LXD API?

时间:2015-10-15 15:06:50

标签: node.js api unix-socket linux-containers lxd

如何通过nodejs下的unix socket域连接到LXD API? 我尝试以下代码但失败了。我有这个答案: HTTP / 1.1 400错误请求

var net = require('net');

var lxdUSD='/var/lib/lxd/unix.socket';



var net = require('net');
var client = net.connect({path: lxdUSD},
            function() { //'connect' listener
                console.log('connected to socket!: '+lxdUSD);

});

client.on('data', function(data) {
    console.log(data);
    console.log(data.toString());
    client.end();
});

client.on('end', function() {
    console.log('disconnected from socket!: '+lxdUSD);
});

client.on('error', function() {
    console.log('Error');
    process.exit(-1);
});
setInterval(function(){
            var msg="GET /1.0/containers HTTP/1.1\r\n";
            console.log("write to socket: "+msg);
            client.write(msg);
            console.log("........");

}, 15*1000);

从命令行中尝试以下操作:

echo -e "GET /1.0/containers HTTP/1.1\r\n" | socat unix-connect:/var/lib/lxd/unix.socket STDIO

输出:

HTTP/1.1 200 OK
Content-Type: application/json
Date: Thu, 15 Oct 2015 15:02:59 GMT
Content-Length: 135

{"type":"sync","status":"Success","status_code":200,"metadata":["/1.0/containers/pc01","/1.0/containers/pc02","/1.0/containers/pc03"]}

1 个答案:

答案 0 :(得分:1)

修正:

而不是

str.strip()

必须:

>>> np.array([[x.strip('()') for x in i] for i in l])
array([['a', 'b', 'c'],
       ['d', 'e', 'f'],
       ['g', 'h', 'i']], 
      dtype='|S1')