0

openshift.com サーバー (サポートする必要があります) に websocket を実装しようとしています。

openshift.com は WSGI を提供してくれるので、そこに my を埋め込み、cherrypyスクリプトがオブジェクトwsgi.pyを定義するようにしapplicationます。cherrypyで定義されているように、websocket ツールもありますws4py

これは、OpenShift の WSGI で動作する最小限のチェリーピー アプリケーションであり、websocket も使用する必要があります。

import cherrypy
from ws4py.server.cherrypyserver import WebSocketPlugin, WebSocketTool
from ws4py.websocket import EchoWebSocket
import atexit
import logging

# see http://tools.cherrypy.org/wiki/ModWSGI
cherrypy.config.update({'environment': 'embedded'}) 
if cherrypy.__version__.startswith('3.0') and cherrypy.engine.state == 0:
    cherrypy.engine.start(blocking=False)
    atexit.register(cherrypy.engine.stop)

class Root(object):
    def index(self): return 'I work!'
    def ws(self): print('THIS IS NEVER PRINTED :(')
    index.exposed=True
    ws.exposed=True

# registering the websocket
conf={'/ws':{'tools.websocket.on': True,'tools.websocket.handler_cls': EchoWebSocket}}
WebSocketPlugin(cherrypy.engine).subscribe()
cherrypy.tools.websocket = WebSocketTool()  

#show stacktraces in console (for some reason this is not default in cherrypy+WSGI)
logger = logging.getLogger()
logger.setLevel(logging.INFO)
stream = logging.StreamHandler()
stream.setLevel(logging.INFO)
logger.addHandler(stream)

application = cherrypy.Application(Root(), script_name='', config=conf)

Websocketを作成する場合 ( に接続する場合ws://myserver:8000/ws) を除いて、すべてがうまく機能します。これは、取得したスタック トレースです。

 cherrypy/_cplogging.py, 214, HTTP Traceback (most recent call last):
   File "cherrypy/_cprequest.py", line 661, in respond
     self.hooks.run('before_request_body')
   File "cherrypy/_cprequest.py", line 114, in run
     raise exc
   File "cherrypy/_cprequest.py", line 104, in run
     hook()
   File "cherrypy/_cprequest.py", line 63, in __call__
     return self.callback(**self.kwargs)
   File "ws4py/server/cherrypyserver.py", line 200, in upgrade
     ws_conn = get_connection(request.rfile.rfile)
AttributeError: 'mod_wsgi.Input' object has no attribute 'rfile'

(ファイル名から絶対パスを手動で削除しました) PS: python3.3, cherrypy==3.5.0,を使用しws4py==0.3.4ます。

それは私には明らかではありません:

  • これが、WSGI 環境での cherrypy と ws4py の間の互換性の欠如である場合。
  • WSGI環境でws4pyの問題なら
  • Openshift Websocket のポートが http とは異なるためである場合

PPS: これは完全な OpenShift プロジェクトであり、自分で実行して試すことができます: https://github.com/spocchio/wsgi-cherrypy-ws4py

4

1 に答える 1

1

まったくありえないと思います。WSGI は同期プロトコル ( 12 ) であり、WebSocket プロトコルは非同期です。Wiki は、Python アプリケーション インターフェイスの場合、OpenShift は WSGI ( 3 ) を使用すると述べています。ああ。

しかし、私は最近、pub/sub シナリオでws4pyを試してみましたが、CherryPy 標準の HTTP サーバー展開の上で非常にうまく機能します。したがって、アプリケーション インターフェイスの制約がない一般的な仮想サーバーでは問題になりません。

于 2014-09-15T18:18:10.483 に答える