class KeyValueServer(object): _rpc_methods = ['get', 'set', 'delete', 'exists', 'keys'] def __init__(self, address): self.data = {} self._serv = SimpleXMLRPCServer(address, allow_none=True) for name in self._rpc_methods: self._serv.register_function(getattr(self, name)) def get(self, name): return self._data[name] def set(self, naem, value): self._data[naem] = value def delete(self, name): del self._data[name] def exists(self, name): return name in self._data def keys(self): return list(self._data) def server_forever(self): self._serv.server_forever()
# 単純な文字列を返す def is_alive(): return 'alive' # 引数を利用する def hello(message): return 'Hello {}.'.format(message) # 現在時刻を返す def nowtime(): return DateTime(datetime.now()) server = SimpleXMLRPCServer(('localhost', 8000)) print('Start Server') # 関数を登録 server.register_function(is_alive, 'is_alive') server.register_function(hello, 'hello') server.register_function(nowtime, 'nowtime') # system.listMethods で参照できるように登録 server.register_introspection_functions() # XML-RPCサーバーの起動 server.server_forever()
import cv2 import time from xmlrpc.server import SimpleXMLRPCServer import numpy as np DEBUG_PORT = 8000 IP_SERVER = '192.168.8.172' serverDebug = SimpleXMLRPCServer((IP_SERVER, DEBUG_PORT)) serverDebug.logRequests = False def debugFrame(frame): frameName = frame[0] imgArray = np.frombuffer(frame[1].data, dtype=np.uint8) img = cv2.imdecode(imgArray, cv2.IMREAD_COLOR) cv2.imshow(frameName, img) cv2.waitKey(1) return 0 serverDebug.register_function(debug_frame) try: serverDebug.server_forever() except (KeyboardInterrupt, SystemExit): print('Ctrl+C pressed') serverDebug.server_close() cv2.destroyAllWindows()