PageRenderTime 36ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/jsonrpc/public/services/jsonrpc/apacheServiceHandler.py

http://pyjamas.googlecode.com/
Python | 62 lines | 31 code | 12 blank | 19 comment | 6 complexity | f76d03186395d52bb489074ee02796d0 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. """
  2. Copyright (c) 2006 Jan-Klaas Kollhof
  3. This file is part of jsonrpc.
  4. jsonrpc is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This software is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this software; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. """
  16. from mod_python import apache
  17. from jsonrpc import SimpleServiceHandler
  18. class ModPyHandler(SimpleServiceHandler):
  19. def send(self, data):
  20. self.req.write(data)
  21. self.req.flush()
  22. def handle(self, req):
  23. self.req = req
  24. req.content_type = "text/plain"
  25. self.handlePartialData(req.read())
  26. self.close()
  27. from mod_python import apache
  28. import os, sys
  29. def handler(req):
  30. (modulePath, fileName) = os.path.split(req.filename)
  31. (moduleName, ext) = os.path.splitext(fileName)
  32. if not os.path.exists(os.path.join(modulePath, moduleName + ".py")):
  33. return apache.HTTP_NOT_FOUND
  34. if not modulePath in sys.path:
  35. sys.path.insert(0, modulePath)
  36. module = apache.import_module(moduleName, log=1)
  37. if hasattr(module, "getService"):
  38. service = module.getService()
  39. elif hasattr(module, "service"):
  40. service = module.service
  41. elif hasattr(module, "Service"):
  42. service = module.Service()
  43. else:
  44. return apache.HTTP_SERVICE_UNAVAILABLE
  45. ModPyHandler(service, messageDelimiter="\n").handle(req)
  46. return apache.OK