Last active
December 20, 2015 10:31
-
-
Save whiteinge/6116107 to your computer and use it in GitHub Desktop.
Bare WSGI app for testing gevent streaming
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| ''' | |
| ''' | |
| # from gevent import monkey | |
| # monkey.patch_all() | |
| import os | |
| import itertools | |
| from time import sleep | |
| # from gevent import sleep | |
| def application(environ, start_response): | |
| import cherrypy | |
| class Resource(object): | |
| exposed = True | |
| def GET(self): | |
| import salt.auth | |
| import salt.config | |
| import saltapi | |
| __opts__ = salt.config.client_config( | |
| os.environ.get('SALT_MASTER_CONFIG', '/etc/salt/master')) | |
| auth = salt.auth.LoadAuth(__opts__) | |
| token = auth.mk_token({'username': 'shouse', 'password': 'saltdev', 'eauth': 'pam'}).get('token') | |
| api = saltapi.APIClient(__opts__) | |
| ret = api.run({'client': 'local', 'tgt': '*', 'fun': 'test.ping', 'token': token}) | |
| return ['Return: {0}\n'.format(ret)] | |
| class Events(object): | |
| exposed = True | |
| _cp_config = { | |
| 'response.stream': True, | |
| 'tools.encode.encoding': 'utf-8', | |
| } | |
| def GET(self): | |
| import salt.config | |
| import salt.utils.event | |
| cherrypy.response.headers['Content-Type'] = 'text/event-stream' | |
| cherrypy.response.headers['Cache-Control'] = 'no-cache' | |
| cherrypy.response.headers['Connection'] = 'keep-alive' | |
| def listen(): | |
| __opts__ = salt.config.client_config( | |
| os.environ.get('SALT_MASTER_CONFIG', '/etc/salt/master')) | |
| event = salt.utils.event.SaltEvent('master', __opts__['sock_dir']) | |
| while True: | |
| sleep(1) | |
| yield '{0}\n'.format(event.get_event(full=True)) | |
| return listen() | |
| class Root(object): | |
| pass | |
| root = Root() | |
| root.salt = Resource() | |
| root.events = Events() | |
| conf = { | |
| 'global': { | |
| 'server.socket_host': '0.0.0.0', | |
| 'server.socket_port': 8000, | |
| }, | |
| '/': { | |
| 'request.dispatch': cherrypy.dispatch.MethodDispatcher(), | |
| } | |
| } | |
| cherrypy.tree.mount(root, '/', conf) | |
| return cherrypy.tree(environ, start_response) | |
| # from saltapi.netapi.rest_cherrypy.wsgi import application | |
| def cpy_serve(): | |
| print "XXX: starting cherrypy multithreaded server" | |
| from cherrypy import wsgiserver | |
| server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 8000), application) | |
| server.start() | |
| def gevent_serve(): | |
| print "XXX: starting gevent evented server" | |
| from gevent import pywsgi | |
| server = pywsgi.WSGIServer(('0.0.0.0', 8000), application, log='default') | |
| server.serve_forever() | |
| if __name__ == '__main__': | |
| # gevent_serve() | |
| cpy_serve() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment