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'] print(*fields, sep='\t') response = table.query(withdata=True) rows = set(response['rows']) data = response['data'] for row in rows: print(*data[row], sep='\t') print() print('', *fields, sep='\t') while True: response = table.wait(withdata=True) insert = response['insert'] remove = response['remove'] data.update(response['data']) for row in insert: print('+', *data[row], sep='\t') rows.add(row) for row in remove: print('-', *data[row], sep='\t') rows.remove(row)
def main(): name = input('relation > ') query = input(name + ' > ') conn = socket.socket(socket.AF_UNIX) conn.connect(str(SOCKET_DIR / name)) table = JSONRpc(conn).get_peer_proxy() fields = table.connect(query=query, protocol=0)['fields'] while True: print('#', *fields, sep='\t| ') print('-' * 80) response = table.query() rows = response['add'] data = response['data'] for row in rows: print(row, *data[row], sep='\t| ') print() method = input('c/u/d > ') if method == 'c': data = input('c > ').split() table.commit(time(), [data], []) elif method == 'u': row = input('# > ') data = input('u > ').split() table.commit(time(), [data], [row]) elif method == 'd': row = input('# > ') table.commit(time(), [], [row]) else: print('error') print()
def main(): if SOCKET_ADDR.exists(): SOCKET_ADDR.unlink() s = socket.socket(socket.AF_UNIX) s.bind(str(SOCKET_ADDR)) s.listen() while True: JSONRpc(s.accept()[0], RelationServer(), threading_model=ThreadingModel.GEVENT, concurrent_request_handling=ThreadingModel.GEVENT)
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 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)
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
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)
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()