def test_runCGIHandler(self): from StringIO import StringIO json=u'{"method":"echo","params":["foobar"], "id":""}' fin=StringIO(json) fout=StringIO() env = {"CONTENT_LENGTH":len(json)} jsonrpc.handleCGI(service=Service(), fin=fin, fout=fout, env=env) data = StringIO(fout.getvalue()) data.readline() data.readline() data = data.read() self.assertEquals(jsonrpc.loads(data), {"result":"foobar", "error":None, "id":""})
def test_handleCGI(self): json = b'{"method":"echo","params":["foobar"], "id":""}' fin = BytesIO(json) fout = BytesIO() env = {'CONTENT_LENGTH': len(json)} jsonrpc.handleCGI(service=Service(), fin=fin, fout=fout, env=env) data = BytesIO(fout.getvalue()) data.readline() data.readline() data = data.read() expect = {'result': 'foobar', 'jsonrpc': '2.0', 'id': ''} actual = jsonrpc.loads(data) self.assertEqual(expect, actual)
def test_handleCGI(self): from StringIO import StringIO json = '{"method":"echo","params":["foobar"], "id":""}' fin = StringIO(json) fout = StringIO() env = {'CONTENT_LENGTH': len(json)} jsonrpc.handleCGI(service=Service(), fin=fin, fout=fout, env=env) data = StringIO(fout.getvalue()) data.readline() data.readline() data = data.read() self.assertEquals(jsonrpc.loads(data), {'result':'foobar', 'jsonrpc': '2.0', 'id':''})
def test_handleCGI(self): from StringIO import StringIO json=u'{"method":"echo","params":["foobar"], "id":""}' fin=StringIO(json) fout=StringIO() env = {'CONTENT_LENGTH': len(json)} jsonrpc.handleCGI(service=Service(), fin=fin, fout=fout, env=env) data = StringIO(fout.getvalue()) data.readline() data.readline() data = data.read() self.assertEquals(jsonrpc.loads(data), {'result':'foobar', 'jsonrpc': '2.0', 'id':''})
def test_runCGIHandler(self): from StringIO import StringIO json = u'{"method":"echo","params":["foobar"], "id":""}' fin = StringIO(json) fout = StringIO() env = {"CONTENT_LENGTH": len(json)} jsonrpc.handleCGI(service=Service(), fin=fin, fout=fout, env=env) data = StringIO(fout.getvalue()) data.readline() data.readline() data = data.read() self.assertEquals(jsonrpc.loads(data), { "result": "foobar", "error": None, "id": "" })
if q.count() == 0: raise Exception("User not found") cu = q.one() cu.name = name cu.email = email if pwd: cu.password = pwd if u.admin: cu.admin = admin session.commit() return cu.id @ServiceMethod def deleteUser(cookie, uid): session = Session() u = usermanagment.getUser(cookie) if not u.admin: raise Exception("Invalid access %s %s" % (uid, u.id)) q = session.query(User).filter_by(id=uid) if q.count() == 0: raise Exception("User not found") cu = q.one() session.delete(cu) session.commit() if __name__ == "__main__": handleCGI()
import servicemodule_example # The json-rpc mod_python handler automatically makes a web service # out of a class called Service. class Echo(object): @servicemethod def echo(self, *args, **kwargs): """Return what is passed in""" return args or kwargs or None class Add(object): @servicemethod def add(self, a, b): return a + b def get_service(): add = Add() echo = Echo() module = servicemodule(servicemodule_example) # Expose functions from multiple sources as a single service return servicechain(add, echo, module) if __name__ == '__main__': service = get_service() handleCGI(service)
for arg in process.command) try: process.start() # we start a bogus process that waits for data on stdin # to detect if the connection is lost sentry = Process(target=sentry_run, args=(process.process.pid, sys.stdin.fileno())) sentry.start() r = process.result() sentry.terminate() if not r: if process.process.returncode == 0: raise LatBuilderError("Aborted") else: raise LatBuilderError("<strong>Command</strong>: " + cmd + "<br/><strong>Ouput:</strong> " + process.output) else: return (cmd, r.lattice.size.points, r.lattice.gen, r.lattice.merit, r.seconds) except OSError: e = sys.exc_info()[1] raise LatBuilderError("Cannot execute the `latbuilder' program (" + e.strerror + ")") @ServiceMethod def array_from_expr(expr, index, dimension): return [parsemath.eval(expr, {index: i + 1}) for i in range(dimension)] if __name__ == "__main__": handleCGI()
from jsonrpc import handleCGI, ServiceMethod class BasicService: @ServiceMethod def echo(self,msg): return msg @ServiceMethod def add (self,a,b): return a + b def setup_logging(): import logging import logging.handlers logger = logging.getLogger() logger.setLevel(logging.DEBUG) syslog = logging.handlers.SysLogHandler(('localhost', 514),'local6') formatter = logging.Formatter('%(name)s: %(levelname)s %(message)s') syslog.setFormatter(formatter) logger.addHandler(syslog) return logger if __name__ == '__main__': logger = setup_logging() service = BasicService() if(service): try: handleCGI(service) except Exception as ex: logger.error(ex)