websockify
Websockify is a Python library and tool that functions as a WebSocket to TCP proxy/bridge. It translates WebSocket traffic to normal socket traffic, enabling browsers to connect to any application or server. Originally part of the noVNC project, it is actively maintained, with releases typically occurring every few months, incorporating new features and compatibility updates.
Common errors
-
OSError: [Errno 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted / Address already in use
cause The specified `source_port` for websockify is already in use by another process, or websockify was not properly shut down from a previous run.fixChoose a different `source_port` that is not in use, or ensure that any previous websockify instances or other applications are fully terminated before restarting. -
Exception: rebind.so not found, perhaps you need to run make
cause This error occurs when using `websockify` in `--wrap-mode` on Linux, indicating that the `rebind.so` library (used for intercepting `bind()` system calls) was not found or not built.fixNavigate to the `websockify` source directory and run `make` to compile `rebind.so`. If it's compiled but still not found, check if `rebind.so` is in the expected `websockify/websockify/` directory, and move it there if necessary. -
code 400, message Client must support 'binary' or 'base64' protocol
cause An older WebSocket client is attempting to connect to `websockify` using a protocol that requires explicit 'binary' or 'base64' support, or expects Base64 encoding for binary data, which is no longer the default/supported method in recent `websockify` versions.fixEnsure the WebSocket client is updated to a modern version that explicitly supports the 'binary' WebSocket sub-protocol as per HyBi/IETF 6455. Avoid older clients that implicitly rely on Base64 encoding. -
WebSocket connection failed: Error during WebSocket handshake
cause This generic error can be caused by various issues during the WebSocket handshake, including invalid or missing upgrade headers, authentication/authorization failures, TLS certificate problems (for `wss://`), or network intermediaries (firewalls, proxies) blocking the handshake.fixCheck server logs for specific HTTP status codes (e.g., 400, 401, 403, 404) that might indicate the root cause. Verify the WebSocket URL (ws:// vs wss://), firewall rules, authentication credentials, and ensure correct SSL certificate configuration.
Warnings
- breaking Python 2.x and older Python 3 versions are no longer supported. Version 0.13.0 dropped support for Python < 3.6. Earlier versions (0.10.0) required Python 3.4+.
- breaking Older WebSocket protocol versions (Hixie) and Base64 encoding for binary data are no longer supported. Only the HyBi / IETF 6455 WebSocket protocol with binary message frames is supported.
- breaking The internal API for subclassing `WebsocketProxy` was significantly refactored. Custom implementations extending this class will likely break.
- gotcha When using `wss://` (encrypted WebSockets) with self-signed SSL certificates, browsers typically do not prompt to accept the certificate directly from a WebSocket connection. This can lead to connection failures.
Install
-
pip install websockify
Imports
- WebSocketProxy
from websockify.websocketproxy import WebSocketProxy
Quickstart
# Start websockify to proxy WebSocket connections on port 8080 to a VNC server on localhost:5901 websockify 8080 localhost:5901 # Or, to wrap a program (e.g., a simple telnet server) and proxy to it # This example assumes 'nc' (netcat) is available and listening on port 2000 # In one terminal: nc -lk 2000 # (for Linux) # In another terminal, start websockify wrapping nc: websockify 2023 --wrap-mode=respawn -- nc -lk 2000 # Access via a WebSocket client (e.g., noVNC) pointing to ws://your_server_ip:8080 or ws://your_server_ip:2023