首页 > 解决方案 > Apache2.4 + WSGI + Bottle + Websocket + 烧杯

问题描述

如标题所示,我在Ubuntu20服务器上使用Apache2.4、WSGI和Bottle。

我已使用此文件成功设置了端点:

"""file: app.py"""
import os, sys
from bottle import app, get, post, request, route, template
from beaker.middleware import SessionMiddleware

os.chdir(os.path.dirname(__file__))
sys.path.append(os.path.dirname(__file__))

application = SessionMiddleware(app())

@route('/<query>')
def index(query):
    return f"Hello, you entered: {query}"

Apache conf 文件包含以下条目:

WSGIScriptAlias /test /path/to/my/project/app.py

这样当我调用该站点时,http://<ip_addr>/test我可以在页面上看到响应。

现在我想在同一台服务器上设置一个 websocket 端点。

谁能告诉我应该怎么做才能使用此代码(基于前面的示例):

"""file: ws.py"""
# taken from https://github.com/zeekay/bottle-websocket

import os, sys
from bottle import app, get, post, request, route, template
from beaker.middleware import SessionMiddleware
from bottle.ext.websocket import GeventWebSocketServer
from bottle.ext.websocket import websocket

#os.chdir(os.path.dirname(__file__))
sys.path.append(os.path.dirname(__file__))

application = SessionMiddleware(GeventWebSocketServer) # is tis correct???

@get('/websocket', apply=[websocket])
def echo(ws):
    while True:
        msg = ws.receive()
        if msg is not None:
            ws.send(msg)
        else: break

我需要 Apache conf 文件中的相应条目吗?喜欢:

WSGIScriptAlias /ws /path/to/my/project/ws.py

谁能告诉我我应该怎么做才能使 websocket 以与以前的端点类似的方式工作?

标签: apachewebsocketwsgibottlebeaker

解决方案


推荐阅读