Skip to content

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

License

Notifications You must be signed in to change notification settings

riga/rpyc-stream

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Examples using SSH

Local Code

Python (local.py):

# -*- coding: utf-8 -*-
from rpycstream import RPC
from subprocess import Popen, PIPE, STDOUT

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(err, msg) {
    if(err) throw err;
    console.log("msg:", msg);
});

Remote Code

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({
    hello: function(name, cb) {
        cb(null, 'Hello, ' + name);
    }
});

server.pipe(process.stdout);
process.stdin.pipe(server);

Development

Authors

Marcel R. (riga)

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages