Supports
- Hixie 76 (Safari and iPhone)
- RFC 6455 (All latest browsers)
- TLS/SSL
- Write the client code by extending WebSocket
from SimpleWebSocketServer import WebSocket, SimpleWebSocketServer
class SimpleEcho(WebSocket):
def handleMessage(self):
if self.data is None:
self.data = ''
# echo message back to client
self.sendMessage(str(self.data))
def handleConnected(self):
print self.address, 'connected'
def handleClose(self):
print self.address, 'closed'
server = SimpleWebSocketServer('', 8000, SimpleEcho)
server.serveforever()
-
Run your code
-
Open up websocket.html and connect to the server
There is an example which provides a simple echo and chat server
Echo Server
python SimpleExampleServer.py --example echo
Chat Server (open up multiple websocket.html files)
python SimpleExampleServer.py --example chat
-
Generate a certificate with key
openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem
-
Run the secure TSL/SSL server (in this case the cert.pem file is in the same directory)
python SimpleExampleServer.py --example chat --ssl 1 --cert ./cert.pem
-
Offer the certificate to the browser by serving websocket.html through https. The HTTPS server will look for cert.pem in the local directory. Ensure the websocket.html is also in the same directory to where the server is run.
sudo python SimpleHTTPSServer.py
-
Open a web browser to: https://localhost:443/websocket.html
-
Change ws://localhost:8000/ to wss://localhost:8000 and click connect.
Note: if you are having problems connecting, ensure that the certificate is added in your browser against the exception https://localhost:8000 or whatever host:port pair you want to connect to.
def handleConnected(): called when handskake is complete
def handleClose(): called when the endpoint is closed or there is an error
def handleMessage(): gets called when there is an incoming message from the client endpoint
- self.opcode: the WebSocket frame type (STREAM, TEXT, BINARY)
- self.data: bytearray payload or None if there was no payload
- self.address: TCP address port tuple of the endpoint
- self.request: HTTP details from the WebSocket handshake (refer to BaseHTTPRequestHandler)
- self.server.connections: map containing all the clients connected to the server
def sendMessage(buffer): send some text or binary data to the client endpoint
- sending a buffer as str() will send a text based WebSocket frame otherwise a binary frame
def sendClose() : send close frame to endpoint
simply run ./WebSockPortMapper.py for port mapping I hope it workes but if you want to map an individual port on a host just change host address and port in the following part of the code:
def SockFun(self):
try:
ms=mysocket()
ms.connect("127.0.0.1",7778)
which in my example is "127.0.0.1" and 7778.
The MIT License (MIT)
Copyright (c) 2013 Dave P.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.