Guest User

Untitled

a guest
Jun 20th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. # Copyright (c) 2010 Sabin Iacob <iacobs@gmail.com>
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. # THE SOFTWARE.
  20.  
  21. import zmq
  22.  
  23.  
  24. class Application(object):
  25. def __init__(self, zmq_context, collectd_address):
  26. self.ctx = zmq_context
  27. self.collectd_address = collectd_address
  28.  
  29. def dispatch(self, env):
  30. """ very simple URL dispatch, a la Cake: /zelink maps to handle_zelink """
  31.  
  32. path = filter(None, env['PATH_INFO'].split('/'))
  33.  
  34. handler = getattr(self, 'handle_%s' % path[0], None)
  35. if not handler:
  36. return '404 Not Found', '%(PATH_INFO)s not found' % env
  37.  
  38. return handler(env)
  39.  
  40. def handle__status(self, env):
  41. comm = self.ctx.socket(zmq.PAIR)
  42. comm.connect(self.collectd_address)
  43.  
  44. comm.send('GET')
  45. ret = comm.recv()
  46.  
  47. comm.close()
  48.  
  49. return '200 OK', [ret]
  50.  
  51. def __call__(self, env, start_response):
  52. if env['REMOTE_ADDR'] != '127.0.0.1':
  53. start_response('403 Forbidden', [])
  54. return ['You are not allowed to see this!']
  55.  
  56. status, ret = self.dispatch(env)
  57. start_response(status, [])
  58. return ret
  59.  
  60.  
  61. def context_factory():
  62. context_store = []
  63. def inner():
  64. if not context_store:
  65. context_store.append(zmq.Context())
  66. return context_store[0]
  67.  
  68. return inner
  69.  
  70. get_context = context_factory()
  71.  
  72. app = Application(get_context(), 'tcp://127.0.0.1:2345')
Add Comment
Please, Sign In to add comment