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

标签 python websocket socket.io gevent bottle

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

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

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 命令和输出。如果我有一个简单的 Bottle 示例,我想我可以更快地前进。

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

谢谢! :)

最佳答案

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?

关于python - 使用 gevent-socketio 和 Socket.IO.js 的 Python 瓶微框架的最小示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11805756/

相关文章:

python - python点击关闭按钮后程序的执行没有结束(使用wxpython-GUI)

java - WebSocket 消息代理的 Android 客户端

node.js - 使用redis与socket.io和NodeJs构建实时聊天

python - 合并按列中的值分组的 2d numpy 数组

python - 使用 Python 测试 JSON 中的值

python - 在索引地理数据框时维护地理结构

vue.js - 动态显示数组长度

c# - 在 MVC Controller 中打开一个 websocket channel

javascript - Webrtc 如何在收到回复后设置视频 Remote

node.js - 使用 Nginx 时 socket.io 出现 CORS 错误