我正在使用ws4py/CherryPy支持websockets,并且希望在其上实现WAMP。WAMP支持ws4py/CherryPy
我想过使用autobahn,但它似乎只支持Twisted和asyncio开箱即用。
是否可以使用高速公路功能来扩展ws4py,或者有其他方法吗?
我正在使用ws4py/CherryPy支持websockets,并且希望在其上实现WAMP。WAMP支持ws4py/CherryPy
我想过使用autobahn,但它似乎只支持Twisted和asyncio开箱即用。
是否可以使用高速公路功能来扩展ws4py,或者有其他方法吗?
正如您已经注意到的,Autobahn|Python支持在Twisted或asyncio下运行。它包含一个全功能的WebSocket实现,以及WAMP。因此,不需要ws4py,我们也没有计划将高速公路| Python包含的WAMP层移植到ws4py。
Twisted还支持运行任何WSGI兼容的应用程序。所以原则上,你应该能够在Twisted下运行CherryPy。我没有测试过 - 我只测试过(并经常使用)Flask on Twisted。
@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)
假设你保存这个片段到一个名为模块:“cptw.py”,现在你可以为你的应用程序的CherryPy如下:
twistd -n web --wsgi cptw.wsgiapp
这适用于扭曲的13.2和CherryPy的3.2.6+
我把这个答案投给了它,因为它对一些人来说肯定有用。但是对于我们来说,CherryPy被设置为基本服务器(我们在其上运行其他WSGI应用程序),所以我最终在CherryPy上重新实现了一个类WAMP协议。 – mar10
谢谢。如果我的回复来得太晚,我很抱歉。我有一种感觉,WSGI是为了实现跨框架场景。 –
我绑CherryPy的,所以我希望的一种方式来运行的高速公路| Python作为内CherryPy的WSGI应用(如ws4py一样),或者利用一些高速公路WPS代码在ws4py之上。无论如何,谢谢你。 – mar10
你可能会研究CherryPy(如果我正确的话,它是一个WSGI兼容的应用程序本身),它可以作为WSGI容器在Twisted下运行。下面是Flask的工作原理:https://github.com/tavendo/AutobahnPython/tree/master/examples/twisted/websocket/echo_wsgi – oberstet