Bottle-WebSocket
raw JSON → 0.2.9 verified Fri May 01 auth: no python maintenance
WebSocket support for the Bottle micro web-framework. Version 0.2.9 is the latest release; the library wraps gevent and GeventWebSocketHandler to provide real-time bidirectional communication. Development appears stalled since 2013.
pip install bottle-websocket Common errors
error ImportError: No module named 'bottle.ext' ↓
cause Bottle-WebSocket is a plugin installed as a separate package; bottle.ext.websocket is only available after installing bottle-websocket.
fix
Run
pip install bottle-websocket. error AttributeError: module 'gevent' has no attribute 'websocket' ↓
cause The GeventWebSocketHandler requires gevent-websocket package to be installed separately.
fix
Install gevent-websocket:
pip install gevent-websocket. error ImportError: No module named 'gevent_websocket' ↓
cause Missing the gevent-websocket dependency.
fix
Install gevent-websocket:
pip install gevent-websocket. Warnings
gotcha The package has not been updated since 2013 and relies on outdated dependencies (gevent < 1.1, gevent-websocket). Modern versions of gevent may break compatibility. ↓
fix Pin gevent to 1.0.2 and gevent-websocket to 0.9.5, or consider an alternative like Flask-SocketIO or aiohttp for new projects.
gotcha The server must be GeventWebSocketServer; using Bottle's default server will not handle WebSocket upgrades. ↓
fix Pass `server=GeventWebSocketServer` to `run()` or use `app.run(server=GeventWebSocketServer)`.
deprecated Python 2 is no longer supported by the Python ecosystem; this library has not been updated for Python 3 compatibility in many areas. ↓
fix Test thoroughly with Python 3; consider migrating to a maintained alternative.
Imports
- WebSocket
from bottle.ext.websocket import WebSocket - gevent_websocket wrong
import gevent-websocketcorrectimport gevent_websocket
Quickstart
from bottle import Bottle, request
from bottle.ext.websocket import GeventWebSocketServer
from bottle.ext.websocket import websocket
app = Bottle()
@app.route('/echo')
def handle_websocket(ws):
while True:
msg = ws.receive()
if msg is None:
break
ws.send('echo: ' + msg)
if __name__ == '__main__':
from bottle import run
run(app, server=GeventWebSocketServer, host='localhost', port=8080)