def main(): conn = socket.socket(socket.AF_UNIX) conn.connect(str(SOCKET_ADDR)) table = JSONRpc(conn).get_peer_proxy() response = table.connect(where='salary>5000 OR name="Mike"') fields = response['fields'] version = response['version'] while True: print('#', *fields, sep='\t') response = table.query(withdata=True) rows = set(response['rows']) data = response['data'] for row in rows: print(row, *data[row], sep='\t') print() method = input('c/u/d > ') if method == 'c': data = { 'id': int(input(' id > ')), 'name': input(' name > '), 'salary': int(input(' salary > ')) } table.modify(data=data, version=time()) elif method == 'u': row = input(' # > ') data = { 'id': int(input(' id > ')), 'name': input(' name > '), 'salary': int(input(' salary > ')) } table.modify(row=row, data=data, version=time()) elif method == 'd': row = input(' # > ') table.modify(row=row, version=time()) else: print('error') print()
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
def run_left(self, address, left, right, on): address = Address(address) self.socket = socket.socket(address.family) self.socket.bind(address.address) self.socket.listen() address = Address(left) self.left = socket.socket(address.family) self.left.connect(address.address) address = Address(right) self.right = socket.socket(address.family) self.right.connect(address.address) self.on = on while True: JSONRpc(self.socket.accept()[0], LeftJoinServer(self), threading_model=ThreadingModel.GEVENT, concurrent_request_handling=ThreadingModel.GEVENT)
# 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
except socket.error as msg: print(" error: %s" % msg) s.close() s = None continue break return s progname = "fileClient" paramMap = params.parseParams(switchesVarDefaults) server, usage, debug = paramMap["server"], paramMap["usage"], paramMap["debug"] s = get_socket(server) rpc = JSONRpc(s) rpc_server = rpc.get_peer_proxy() # create a tree leaf1 = node("leaf1") leaf2 = node("leaf2") root = node("root", [leaf1, leaf1, leaf2]) root.show() # Execute in rpc_server: g, n = encodeGraph(root) res_g, res_n = rpc_server.increment_graph(g, n) result = decodeGraph(res_g, res_n) result.show()
def server(): while True: JSONRpc(s.accept()[0], LogServer(self), threading_model=ThreadingModel.GEVENT, concurrent_request_handling=ThreadingModel.GEVENT)
# minimalistic server example from # https://github.com/seprich/py-bson-rpc/blob/master/README.md#quickstart import socket from bsonrpc import JSONRpc from bsonrpc import request, service_class # Class providing functions for the client to use: @service_class class ServerServices(object): @request def swapper(self, txt): return ''.join(reversed(list(txt))) # Quick-and-dirty TCP Server: ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ss.bind(('localhost', 50001)) ss.listen(10) while True: s, _ = ss.accept() # JSONRpc object spawns internal thread to serve the connection. JSONRpc(s, ServerServices()) Client import socket from bsonrpc import JSONRpc
# class providing functions for the client to use @service_class class ServerServices(object): @request def increment(self, graph_flat): graph = recreate(graph_flat) root = graph['root'] self.increment_rec(root) graph_flat = flatten(root, {}) return graph_flat def increment_rec(self, graph): graph.val += 1 for child in graph.children: self.increment_rec(child) return graph # quick-and-dirty TCP Server ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ss.bind(('localhost', 50001)) ss.listen(10) while True: s, _ = ss.accept() # JSONRpc object spawns internal thread to serve the connection JSONRpc(s, ServerServices(), framing_cls=JSONFramingNone)
if Node_Dic[i]['children'] == []: Root_List.append(node(Node_Dic[i]['name'])) Root_List[len(Root_List) - 1].val = Node_Dic[i]['val'] else: #If it does have a child it will look for its child in the Root list dictionary and append them in a new node Root_List.append( node(Node_Dic[i]['name'], FatherChildren(Root_List, Node_Dic[i]['children']))) Root_List[len(Root_List) - 1].val = Node_Dic[i]['val'] #Will return the last node in the List which is the root of the tree return Root_List[len(Root_List) - 1] s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('localhost', 50001)) rpc = JSONRpc(s, framing_cls=JSONFramingNone) server = rpc.get_peer_proxy() leaf1 = node("leaf1") leaf2 = node("leaf2") root = node("root", [leaf1, leaf1, leaf2]) print("graph before increment") root.show() Dict_Root = [] Dict_Root.append(({ 'name': root.name, 'val': root.val, 'children': ChildrenList(root) }))
print("%s%s val=%d:" % (level*" ", self.name, self.val)) for c in self.children: c.show(level + 1) def serialize_json(instance=None, path=None): dt = {} dt.update(vars(instance)) with open(path, "w") as file: json.dump(dt, file) # Cut-the-corners TCP Client: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('localhost', 6000)) rpc = JSONRpc(s) server = rpc.get_peer_proxy() leaf1 = node("leaf1") leaf2 = node("leaf2") root = node("root", [leaf1, leaf1, leaf2]) print("graph before increment") root.show() # open a file, where you ant to store the data file = open('request.json', 'wb') encode_root = json.dumps(root) #encode_root = json.dumps(root, default=lambda o: o.__dict__)
def startConnection(s): rpc = JSONRpc(s, framing_cls=JSONFramingNone) server = rpc.get_peer_proxy() return server, rpc
# Class providing functions for the client to use: @service_class class Node(object): @request def increment(self, graph): graph = nodeEncoder().from_json(graph) obj_dict = defaultdict(int) deserialized_increment(graph, obj_dict) #graph = nodeEncoder().to_json(graph) with open('request.json', 'w+') as outfile: json.dump(graph, outfile, default=lambda x: x.__dict__) return nodeEncoder().to_json(graph) @request def show(self, graph): graph = nodeEncoder().from_json(graph) show_g(graph) graph = nodeEncoder().to_json(graph) # Quick-and-dirty TCP Server: ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ss.bind(('localhost', 50001)) ss.listen(10) while True: s, _ = ss.accept() # JSONRpc object spawns internal thread to serve the connection. JSONRpc(s, Node(), framing_cls=JSONFramingNone)
def acceptConnection(serverSocket): while True: s, _ = serverSocket.accept() # JSONRpc object spawns internal thread to serve the connection. JSONRpc(s, ServerServices(),framing_cls=JSONFramingNone) sys.exit()