Adding custom module via RPyC

818 Views Asked by At

I'm trying to add a new module to a connection.

I have the following files: main.py UpdateDB.py

In UpdateDB:

def UpdateDB():
    ...

In main.py:

import UpdateDB
import rpyc

conn = rpyc.classic.connect(...)
rpyc.utils.classic.upload_package(conn, UpdateDB)
conn.modules.UpdateDB.UpdateDB()

And I can figure out how to invoke the UpdateDB() function. I get:

AttributeArror: 'module' object has no attribute 'UpdateDB'

Perhaps I'm trying to do it wrong. So let me explain what I'm trying to do: I want to create a connection to the server and run on it a function from the UpdateDB.py file.

1

There are 1 best solutions below

0
On

Not sure how to do that in classic mode (not sure why you'd use it), but here is how to accomplish the task in the newer RPyC service mode.

Script ran as Server:

import rpyc
from rpyc.utils.server import ThreadedServer


class MyService(rpyc.Service):
    def exposed_printSomething(self, a):
        print a
        print "printed on server!"
        return 'printed on client!'

if __name__ == '__main__':
    server = ThreadedServer(MyService, port=18812)
    server.start()

Script ran as Client:

import rpyc

if __name__ == '__main__':
    conn = rpyc.connect("127.0.0.1", port=18812)
    print conn.root.printSomething("passed to server!")

Result on Server:

passed to server!
printed on server!

Result on Client:

printed on client!