2014-01-24 51 views

回答

2

正如您已經注意到的,Autobahn|Python支持在Twistedasyncio下運行。它包含一個全功能的WebSocket實現,以及WAMP。因此,不需要ws4py,我們也沒有計劃將高速公路| Python包含的WAMP層移植到ws4py。

Twisted還支持運行任何WSGI兼容的應用程序。所以原則上,你應該能夠在Twisted下運行CherryPy。我沒有測試過 - 我只測試過(並經常使用)Flask on Twisted。

+0

我綁CherryPy的,所以我希望的一種方式來運行的高速公路| Python作爲內CherryPy的WSGI應用(如ws4py一樣),或者利用一些高速公路WPS代碼在ws4py之上。無論如何,謝謝你。 – mar10

+1

你可能會研究CherryPy(如果我正確的話,它是一個WSGI兼容的應用程序本身),它可以作爲WSGI容器在Twisted下運行。下面是Flask的工作原理:https://github.com/tavendo/AutobahnPython/tree/master/examples/twisted/websocket/echo_wsgi – oberstet

4

@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+

+0

我把這個答案投給了它,因爲它對一些人來說肯定有用。但是對於我們來說,CherryPy被設置爲基本服務器(我們在其上運行其他WSGI應用程序),所以我最終在CherryPy上重新實現了一個類WAMP協議。 – mar10

+0

謝謝。如果我的回覆來得太晚,我很抱歉。我有一種感覺,WSGI是爲了實現跨框架場景。 –