【问题标题】:minimal example of Python's bottle microframework using gevent-socketio and Socket.IO.js使用 gevent-socketio 和 Socket.IO.js 的 Python 瓶子微框架的最小示例
【发布时间】:2012-08-02 02:16:03
【问题描述】:

问题: 除了使用gevent-socketioSocket.io.jsbottle 实现之外,与this link 的示例类似的解决方案是什么?我正在寻找最小的解决方案,它可以简单地将一些流量从客户端传递到服务器,然后使用 gevent-socketio、Socket.io.js 和瓶子返回到客户端。

背景:我开发了一个简单的网络应用程序,它为服务器上的远程自定义 shell (cli) 提供了一个基于网络的终端。浏览器(客户端)从表单输入字段收集 shell 命令,通过 web-socket 将命令传递给 gevent.pywsgi.WSGIServer 通过 geventwebsocket.WebSocketHandler 处理程序处理请求,该处理程序将命令提供给 shell,同时通过异步返回输出客户端浏览器中表单中文本区域字段的套接字。这是基于瓶子团队提供的一个很棒的小例子:

http://bottlepy.org/docs/dev/async.html#finally-websockets

此处提供冗余:

example_server.py

from bottle import request, Bottle, abort
app = Bottle()

@app.route('/websocket')
def handle_websocket():
    wsock = request.environ.get('wsgi.websocket')
    if not wsock:
        abort(400, 'Expected WebSocket request.')

    while True:
        try:
            message = wsock.receive()
            wsock.send("Your message was: %r" % message)
        except WebSocketError:
            break

from gevent.pywsgi import WSGIServer
from geventwebsocket import WebSocketHandler, WebSocketError
server = WSGIServer(("0.0.0.0", 8080), app,
                    handler_class=WebSocketHandler)
server.serve_forever()

client.html

<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript">
    var ws = new WebSocket("ws://example.com:8080/websocket");
    ws.onopen = function() {
        ws.send("Hello, world");
    };
    ws.onmessage = function (evt) {
        alert(evt.data);
    };
  </script>
</head>
</html>

动机:我现有的应用在最新版本的 Firefox 和 Chrome 中运行良好。不存在 IE 支持,Safari 兼容性中等。我最终正在寻找一种跨浏览器解决方案来在客户端和服务器之间通信 shell 命令和输出。如果我有一个简单的瓶子例子,我想我可以更快地前进。

顺便说一下,我查看了gevent-socketio examples 甚至a bottle example,但是所有这些示例都与上面的简单示例相差太大,以至于我无法在应用程序中实现飞跃。 (gevent-socketio 示例看起来与我熟悉的瓶子应用程序完全不同。而且,瓶子示例实际上并没有展示如何与客户端通信。)

谢谢! :)

【问题讨论】:

    标签: python websocket socket.io gevent bottle


    【解决方案1】:

    Circus!建立在 zmq 之上的进程运行器和观察器,使用 Bottle 和 socketio 作为 Web 界面:

    https://github.com/mozilla-services/circus/blob/master/circus/web/circushttpd.py https://github.com/mozilla-services/circus/blob/master/circus/web/server.py

    源代码非常简单,可以帮助您开始使用 Bottle 和 socketio 构建更大的应用程序。

    否则,我建议您转到sockjs!这是一个更通用的实现,可以更好地支持不同的后端。

    这个其他线程可以帮助你: SockJS or Socket.IO? Worth to recode ajax-based page?

    【讨论】:

    猜你喜欢
    • 2018-07-18
    • 1970-01-01
    • 2011-09-25
    • 2012-04-29
    • 2018-12-22
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 2012-09-26
    相关资源
    最近更新 更多