revy revy - 1 year ago 116
Node.js Question

zerorpc: how to convert string data from python to node

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())

This is the node zerorpc client:

// nodejs zerorpc client

var zerorpc = require("zerorpc")

var client = new zerorpc.Client();

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?

Answer Source

Node JS Buffer class has the toString method

strings[i] = response[i].toString("utf8")

See the method:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download