# Fix paths # rootdir = os.path.dirname(os.path.abspath(__file__)) # sys.path.append(rootdir) # os.chdir(rootdir) import bottle import bottle_jsonrpc @bottle.route('/') def index(): return bottle.static_file('example.html', os.getcwd()) def add(a, b): return a + b def sort(lst): return sorted(lst) jsonrpc = bottle_jsonrpc.register('/rpc') jsonrpc.methods = {'add': add, 'sort': sort} bottle.debug(True) if __name__ == '__main__': # Standalone web server bottle.run(reloader=True) else: # Running under WSGI (probably apache) application = bottle.default_app()
import bottle import gevent import gevent.monkey gevent.monkey.patch_all() # Should be import after "gevent.monkey.patch_all()" import bottle_jsonrpc @bottle.route("/") def index(): return bottle.static_file("example.html", os.getcwd()) jsonrpc = bottle_jsonrpc.register("/rpc") @jsonrpc def add(a, b): return a + b @jsonrpc def sort(lst): return sorted(lst) @jsonrpc def power(base, power): print "receive: {}".format(base, power)
except Settings.DoesNotExist: message = "Cannot assign setting {0}: it does not exist".format( sname) log.info(message) return message except Exception, e: message = "Error querying Settings {0}: {1}".format(sname, str(e)) log.error(message) return message log.info(message) notify('set', message) return message bottle_jsonrpc.register('/rpc', Methods()) def agentinfo(): device = 'unknown' imei = '000000000000000' user_agent = request.environ.get('HTTP_USER_AGENT') try: # "TC65i/123456789012345 Profile/IMP-NG Configuration/CLDC-1.1" user_agent = user_agent.split(' ')[0] parts = user_agent.split('/') if len(parts) == 2: device = parts[0] imei = parts[1]
# Fix paths # rootdir = os.path.dirname(os.path.abspath(__file__)) # sys.path.append(rootdir) # os.chdir(rootdir) import bottle import bottle_jsonrpc @bottle.route('/') def index(): return bottle.static_file('example.html', os.getcwd()) class Methods(object): def add(self, a, b): return a + b def sort(self, lst): return sorted(lst) bottle_jsonrpc.register('/rpc', Methods()) bottle.debug(True) if __name__ == '__main__': # Standalone web server bottle.run(reloader=True) else: # Running under WSGI (probably apache) application = bottle.default_app()
help='Python module with control functions. Default controlFunctions') parser.add_argument( '--noreload', action='store_true', help='Do not automatically reload server after script changes') parser.add_argument('-d', '--debug', action='store_true', help='Enable debug mode with debug output') args = parser.parse_args() # instead of 'import controlFunctions', load dynamically using arguments importlib.import_module(args.functions) jsonrpc = bottle_jsonrpc.register(args.path) # curl -X POST -i -H "Content-type: application/json" -X POST http://localhost:2102/control -d '{ "jsonrpc": "2.0", "method": "ping", "params": [], "id": 1}' @jsonrpc def ping(): """Returns "pong".""" return "pong" # curl -X POST -i -H "Content-type: application/json" -X POST http://localhost:2102/control -d '{ "jsonrpc": "2.0", "method": "print", "params": ["Wow"], "id": 1}' def print_msg(msg): """Prints a message on the server.""" print(msg)