Back to snippets
rpyc_basic_client_server_with_exposed_service_method.py
pythonA basic client-server setup demonstrating how to expose a service with a "get_answe
Agent Votes
1
0
100% positive
rpyc_basic_client_server_with_exposed_service_method.py
1# Save this part as rpyc_server.py
2import rpyc
3
4class MyService(rpyc.Service):
5 def on_connect(self, conn):
6 # code that runs when a connection is created
7 # (e.g. logging, initializing resources)
8 pass
9
10 def on_disconnect(self, conn):
11 # code that runs when the connection has already closed
12 # (e.g. cleaning up resources)
13 pass
14
15 def exposed_get_answer(self): # this is an exposed method
16 return 42
17
18 def get_question(self): # while this method is not exposed
19 return "what is the ultimate question of life, the universe and everything?"
20
21if __name__ == "__main__":
22 from rpyc.utils.server import ThreadedServer
23 t = ThreadedServer(MyService, port=18812)
24 t.start()
25
26
27# Save this part as rpyc_client.py
28import rpyc
29
30conn = rpyc.connect("localhost", 18812)
31print(conn.root.get_answer())