Chapter 18 ■ rpC
344
An RPyC Example
The RPyC project can be found at http://rpyc.readthedocs.org/en/latest/. This project takes a much more
sophisticated approach toward objects. Indeed, it is more like the approach available in CORBA, where what actually
gets passed across the network is a reference to an object that can be used to call back and invoke more of its methods
later if the receiver needs to. The most recent version also seems to have put more thought into security, which is
important if you are letting other organizations use your RPC mechanism. After all, if you let someone give you some
data to unpickle, you are essentially letting them run arbitrary code on your computer!
You can see an example client and server in Listings 18-7 and 18-8, respectively. If you want an example of the
incredible kinds of things that a system like RPyC makes possible, you should study these listings closely.
Listing 18-7. An RPyC Client
#!/usr/bin/env python3
Foundations of Python Network Programming, Third Edition
https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter18/rpyc_client.py
RPyC client
import rpyc
def main():
config = {'allow_public_attrs': True}
proxy = rpyc.connect('localhost', 18861, config=config)
fileobj = open('testfile.txt')
linecount = proxy.root.line_counter(fileobj, noisy)
print('The number of lines in the file was', linecount)
def noisy(string):
print('Noisy:', repr(string))
if name == 'main':
main()
Listing 18-8. An RPyC Server
#!/usr/bin/env python3
Foundations of Python Network Programming, Third Edition
https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter18/rpyc_server.py
RPyC server
import rpyc
def main():
from rpyc.utils.server import ThreadedServer
t = ThreadedServer(MyService, port = 18861)
t.start()
class MyService(rpyc.Service):
def exposed_line_counter(self, fileobj, function):
print('Client has invoked exposed_line_counter()')