Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

minimal example of Python's bottle microframework using gevent-socketio and Socket.IO.js

Question: What would be a comparable solution to the example at this link, except implemented using gevent-socketio and Socket.io.js with bottle? I'm looking for the minimal solution that will simply pass some traffic in a loop from the client to the server and back to the client using gevent-socketio, Socket.io.js, and bottle.

Background: I have developed a simple web-app that provides a web-based terminal for a remote custom shell (cli) on the server. The browser (client) collects shell commands from a form input field, passes the command over a web-socket to a gevent.pywsgi.WSGIServer handling the requests via the geventwebsocket.WebSocketHandler handler, which supplies the command to the shell, while asynchronously returning output via the socket to a textarea field in a form in the client's browser. This is based on a great, little example provided by the bottle team:

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

Provided here for redundancy:

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>

Motivation: My existing app works great in the latest version of Firefox and Chrome. IE support is non-existent, and Safari compatibility is middlin'. I'm ultimately looking for a cross-browswer solution to communicate shell commands and output between the client and server. If I had a simple example for bottle, I think I could move forward more quickly.

Incidentally, I looked at the gevent-socketio examples and even a bottle example, but all of these examples are too different from the above simple example for me to make the leap in application. (The gevent-socketio examples look nothing like the bottle apps, which which I'm familiar. And, the bottle example doesn't actually show how to communicate with the client.)

Thanks! :)

like image 801
Trevor Avatar asked Aug 04 '12 03:08

Trevor


1 Answers

Circus! the process runner and watcher built on top of zmq, use bottle and socketio for the web interfaces:

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

The source code is simple enough for helping you to get started to build a bigger app with bottle and socketio.

Otherwise, I advice you to move to sockjs! which a more generic implementation with better support for different backends.

This other thread can help you : SockJS or Socket.IO? Worth to recode ajax-based page?

like image 176
Rach Avatar answered Nov 03 '22 06:11

Rach