使用gevent-socketio和Socket.IO的Python瓶子微框架的最小示例.js



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

背景:我开发了一个简单的Web应用程序,它为服务器上的远程自定义shell(cli(提供了一个基于Web的终端。 浏览器(客户端(从表单输入字段中收集 shell 命令,通过 web-socket 将命令传递给通过 geventwebsocket.WebSocketHandler 处理程序处理请求的gevent.pywsgi.WSGIServer,处理程序向 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()

客户端.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的例子,甚至还有一个瓶子的例子,但所有这些例子都与上面简单的例子太不同了,我无法在应用中实现飞跃。 (gevent-socketio示例看起来与瓶子应用程序完全不同,我很熟悉。 而且,瓶子的例子实际上并没有展示如何与客户沟通。

谢谢! :)

马戏团! 在 ZMQ 之上构建的进程运行器和观察者,使用 Bottle 和 Socketio 作为 Web 界面:

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

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

否则,我建议你转向sockjs!这是一个更通用的实现,对不同的后端有更好的支持。

这个其他线程可以帮助您:袜子JS还是 Socket.IO?值得重新编码基于 ajax 的页面吗?

最新更新