I need to call a python script from nodejs and getting back the result. I found the zerorpc library which seems a good fit. The python script returns an array of strings but in node i got objects of binary data.
This is the python zerorpc server:
# python zerorpc server
import zerorpc
class HelloRPC(object):
def test(self):
return ["A", "B", "C"]
server = zerorpc.Server(HelloRPC())
serrver.bind("tcp://0.0.0.0:4242")
server.run()
This is the node zerorpc client:
// nodejs zerorpc client
var zerorpc = require("zerorpc")
var client = new zerorpc.Client();
client.connect("tcp://127.0.0.1:4242");
client.invoke("test", function(error, response, more) {
if (response) {
for (var i = 0; i < response.length; i++) {
console.log(typeof response[i], response[i])
}
}
}
Which gives this output:
object <Buffer 41>
object <Buffer 42>
object <Buffer 43>
Which is the best way to convert these objects in strings in nodejs?
Node JS Buffer class has the toString method
strings[i] = response[i].toString("utf8")
See the method: https://nodejs.org/api/buffer.html#buffer_buf_tostring_encoding_start_end