rpyc-stream

A simple, one-file Python RPC System that is based on Streams allowing for cross-language/SSH usage.

rpyc-stream

A simple (130 loc), one-file Python RPC System that is based on Streams allowing for cross-language/SSH usage.

This is a port of the nodejs RPC system by @dominictarr.

Python (local.py):

# -*- coding: utf-8 -*- 
from rpycstream import RPC
from subprocess import PopenPIPESTDOUT
 
def main():
    # python 
    worker = Popen(['ssh', 're.mo.te.ip', 'python', 'remote.py']stdout=PIPE, stdin=PIPE, stderr=STDOUT)
    # nodejs 
    # worker = Popen(['ssh', 're.mo.te.ip', 'node', 'remote.js'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) 
    # pipe into each other 
    rpc = RPC(stdin=worker.stdout, stdout=worker.stdin)
    # remote call 
    remote = rpc.wrap(['hello'])
    remote.hello('Carlo'cb)
    # which is equivalent to 
    rpc.rpc('hello'['John']cb)
 
def cb(err, msg=None):
    if err:
        raise err
    print "msg:"msg
 
if __name__ == '__main__':
    main()
 

NodeJs (local.js):

var cp  = require('child_process');
var rpc = require('rpc-stream');
 
// nodejs 
var worker = cp.exec('ssh re.mo.te.ip node remote.js');
// python 
// var worker = cp.exec('ssh re.mo.te.ip python remote.py'); 
 
var client = rpc();
client.pipe(worker.stdin);
worker.stdout.pipe(client);
 
client.rpc('hello', ['John'], function(errmsg) {
    if(err) throw err;
    console.log("msg:", msg);
});
 

Python (remote.py):

# -*- coding: utf-8 -*- 
from rpycstream import RPC
 
class MyClass:
    def hello(self, name):
        return '%s was bitten by a python' % name
 
if __name__ == '__main__':
    rpc = RPC(target=MyClass())
 

NodeJs (remote.js):

// see https://github.com/dominictarr/rpc-stream 
var rpc = require('rpc-stream');
 
var server = rpc({
    hellofunction(namecb) {
        cb(null, 'Hello, ' + name);
    }
});
 
server.pipe(process.stdout);
process.stdin.pipe(server);
 

Marcel R. (riga)