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