def increment_using_rpc(current_node): # create a server socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # connect to the server socket s.connect(('localhost', 50001)) # Connects via socket to RPC peer node rpc = JSONRpc(s) # Get a RPC peer proxy object server = rpc.get_peer_proxy() # Serialize the graph as a dictionary # Dictionary structure # {"root_object_id": {"name": "root", # "level": 0, # "value": 0, # "children":["leaf1_object_id", "leaf1_object_id", "leaf2_object_id"] # }, # "leaf1_object_id": {..... # } ser_graph = object_to_dictionary(current_node) # Remote procedure call using the serialized tree and the root node name result = server.server_increment(ser_graph) # Close thr RPC. # Closes the socket 's' also rpc.close() # construct the tree using objects # from the serialized output received from the RPC current_node = dictionary_to_object(result) # return the root node return current_node
# minimalistic client example from # https://github.com/seprich/py-bson-rpc/blob/master/README.md#quickstart import socket from bsonrpc import JSONRpc from bsonrpc.exceptions import FramingError from bsonrpc.framing import (JSONFramingNetstring, JSONFramingNone, JSONFramingRFC7464) # Cut-the-corners TCP Client: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('localhost', 50002)) rpc = JSONRpc(s, framing_cls=JSONFramingNone) server = rpc.get_peer_proxy() # Execute in server: result = server.swapper('Hello World!') # "!dlroW olleH" print(result) print(server.nop({1: [2, 3]})) rpc.close() # Closes the socket 's' also
def startConnection(s): rpc = JSONRpc(s, framing_cls=JSONFramingNone) server = rpc.get_peer_proxy() return server, rpc