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

Tre*_*vor 10 python websocket gevent bottle socket.io

问: 什么是可比的解决方案的例子,在这个环节,除了使用实施GEVENT-socketioSocket.io.js?我正在寻找最小的解决方案,它将简单地将一些流量从客户端传递到服务器并使用gevent-socketio,Socket.io.js和bottle返回客户端.

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

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()
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

顺便说一下,我查看了gevent-socketio示例甚至是一个瓶子示例,但所有这些示例与上面的简单示例有太大不同,以便我在应用程序中实现飞跃.(gevent-socketio示例看起来与我熟悉的瓶子应用程序完全不同.而且,瓶子示例实际上并没有显示如何与客户端进行通信.)

谢谢!:)

Rac*_*ach 1

马戏团!进程运行器和观察器构建在 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 还是 Socket.IO?值得重新编码基于 ajax 的页面吗?