【问题标题】:WAMP support for ws4py / CherryPyWAMP 支持 ws4py / CherryPy
【发布时间】:2014-01-24 16:03:55
【问题描述】:

我正在使用 ws4py / CherryPy 来支持 websockets,并希望在它之上实现 WAMP

我曾想过使用autobahn,但它似乎只支持开箱即用的 Twisted 和 asyncio。
是否可以使用高速公路功能来扩展 ws4py,或者有其他方法吗?

【问题讨论】:

    标签: websocket cherrypy autobahn ws4py wamp-protocol


    【解决方案1】:

    @oberstet 是对的。这是一个关于如何运行 CherryPy 应用程序的快速展示:

    import cherrypy
    from twisted.web.wsgi import WSGIResource
    from twisted.internet import reactor
    
    # Our CherryPy application
    class Root(object):
        @cherrypy.expose
        def index(self):
            return "hello world"
    
    # Create our WSGI app from the CherryPy application
    # it will respond to the /blog path
    wsgiapp = cherrypy.tree.mount(Root(), '/blog', {'/': {'tools.etags.on': True}})
    
    # Configure the CherryPy's app server
    # Disable the autoreload which won't play well 
    cherrypy.config.update({'engine.autoreload.on': False})
    
    # We will be using Twisted HTTP server so let's
    # disable the CherryPy's HTTP server entirely
    cherrypy.server.unsubscribe()
    
    # If you'd rather use CherryPy's signal handler
    # Uncomment the next line. I don't know how well this
    # will play with Twisted however
    #cherrypy.engine.signals.subscribe()
    
    # Tie our app to Twisted
    reactor.addSystemEventTrigger('after', 'startup', cherrypy.engine.start)
    reactor.addSystemEventTrigger('before', 'shutdown', cherrypy.engine.exit)
    resource = WSGIResource(reactor, reactor.getThreadPool(), wsgiapp)
    

    假设您将此 sn-p 保存到名为“cptw.py”的模块中,您现在可以按如下方式为您的 CherryPy 应用程序提供服务:

    twistd -n web --wsgi  cptw.wsgiapp
    

    这适用于 Twisted 13.2 和 CherryPy 3.2.6+

    【讨论】:

    • 我投票赞成这个答案,因为它对某些人肯定有用。但对我们来说,CherryPy 被设置为基础服务器(我们改为在其上运行其他 WSGI 应用程序),所以我最终在 CherryPy 之上重新实现了一个类似 WAMP 的协议。
    • 谢谢。如果我的回复来得太晚了,我很抱歉。我感觉 WSGI 是为了支持跨框架场景。
    【解决方案2】:

    正如您已经注意到的,Autobahn|Python 支持在Twistedasyncio 下运行。它包括一个功能齐全的 WebSocket 实现,以及最重要的 WAMP。所以不需要 ws4py,我们也没有将 Autobahn|Python 包含的 WAMP 层移植到 ws4py 的计划。

    Twisted 还支持运行任何符合 WSGI 的应用程序。所以原则上,你应该能够在 Twisted 下运行 CherryPy。我没有测试过——我只在 Twisted 上测试过(并且经常使用)Flask。

    【讨论】:

    • 我依赖于 CherryPy,所以我希望有一种方法可以在 CherryPy 中将 autobahn|python 作为 WSGI 应用程序运行(就像 ws4py 一样),或者在 ws4py 之上使用一些高速公路 WAMP 代码。还是谢谢你。
    • 你可能会研究 CherryPy(它本身就是一个 WSGI 兼容的应用程序,如果我没弄错的话)是否可以在 Twisted 下作为 WSGI 容器运行。以下是 Flask 的工作原理:github.com/tavendo/AutobahnPython/tree/master/examples/twisted/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 2021-02-02
    • 2017-03-14
    • 2010-12-23
    • 2020-12-08
    • 2019-12-10
    相关资源
    最近更新 更多