【发布时间】:2014-01-24 16:03:55
【问题描述】:
我正在使用 ws4py / CherryPy 来支持 websockets,并希望在它之上实现 WAMP。
我曾想过使用autobahn,但它似乎只支持开箱即用的 Twisted 和 asyncio。
是否可以使用高速公路功能来扩展 ws4py,或者有其他方法吗?
【问题讨论】:
标签: websocket cherrypy autobahn ws4py wamp-protocol
我正在使用 ws4py / CherryPy 来支持 websockets,并希望在它之上实现 WAMP。
我曾想过使用autobahn,但它似乎只支持开箱即用的 Twisted 和 asyncio。
是否可以使用高速公路功能来扩展 ws4py,或者有其他方法吗?
【问题讨论】:
标签: websocket cherrypy autobahn ws4py wamp-protocol
@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+
【讨论】:
正如您已经注意到的,Autobahn|Python 支持在Twisted 或asyncio 下运行。它包括一个功能齐全的 WebSocket 实现,以及最重要的 WAMP。所以不需要 ws4py,我们也没有将 Autobahn|Python 包含的 WAMP 层移植到 ws4py 的计划。
Twisted 还支持运行任何符合 WSGI 的应用程序。所以原则上,你应该能够在 Twisted 下运行 CherryPy。我没有测试过——我只在 Twisted 上测试过(并且经常使用)Flask。
【讨论】: