source: python-simple-websocket-server/simple-websocket-server/dist/deb_dist/simplewebsocketserver-0.1.0/README.md @ 1309

Last change on this file since 1309 was 1309, checked in by joamuran, 3 years ago

initial upload

File size: 3.7 KB
Line 
1## A Simple Websocket Server written in Python
2
3- RFC 6455 (All latest browsers)
4- TLS/SSL out of the box
5- Passes Autobahns Websocket Testsuite
6- Support for Python 2 and 3
7
8#### Installation
9
10You can install SimpleWebSocketServer by running the following command...
11
12`sudo pip install git+https://github.com/dpallot/simple-websocket-server.git`
13
14Or by downloading the repository and running `sudo python setup.py install`. 
15Installation via pip is suggested.
16
17#### Echo Server Example
18`````python
19from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
20
21class SimpleEcho(WebSocket):
22
23    def handleMessage(self):
24        # echo message back to client
25        self.sendMessage(self.data)
26
27    def handleConnected(self):
28        print self.address, 'connected'
29
30    def handleClose(self):
31        print self.address, 'closed'
32
33server = SimpleWebSocketServer('', 8000, SimpleEcho)
34server.serveforever()
35`````
36
37Open *websocket.html* and connect to the server.
38
39#### Chat Server Example
40`````python
41from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
42
43clients = []
44class SimpleChat(WebSocket):
45
46    def handleMessage(self):
47       for client in clients:
48          if client != self:
49             client.sendMessage(self.address[0] + u' - ' + self.data)
50
51    def handleConnected(self):
52       print self.address, 'connected'
53       for client in clients:
54          client.sendMessage(self.address[0] + u' - connected')
55       clients.append(self)
56
57    def handleClose(self):
58       clients.remove(self)
59       print self.address, 'closed'
60       for client in clients:
61          client.sendMessage(self.address[0] + u' - disconnected')
62
63server = SimpleWebSocketServer('', 8000, SimpleChat)
64server.serveforever()
65`````
66Open multiple *websocket.html* and connect to the server.
67
68#### Want to get up and running faster?
69
70There is an example which provides a simple echo and chat server
71
72Echo Server
73
74    python SimpleExampleServer.py --example echo
75
76Chat Server (open up multiple *websocket.html* files)
77
78    python SimpleExampleServer.py --example chat
79
80
81#### TLS/SSL Example
82
831) Generate a certificate with key
84
85    openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem
86
872) Run the secure TSL/SSL server (in this case the cert.pem file is in the same directory)
88
89    python SimpleExampleServer.py --example chat --ssl 1 --cert ./cert.pem
90
913) Offer the certificate to the browser by serving *websocket.html* through https.
92The HTTPS server will look for cert.pem in the local directory.
93Ensure the *websocket.html* is also in the same directory to where the server is run.
94
95    sudo python SimpleHTTPSServer.py
96
974) Open a web browser to: *https://localhost:443/websocket.html*
98
995) Change *ws://localhost:8000/* to *wss://localhost:8000* and click connect.
100
101Note: 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.
102
103#### For the Programmers
104
105handleConnected: called when handshake is complete
106 - self.address: TCP address port tuple of the endpoint
107
108handleClose: called when the endpoint is closed or there is an error
109
110handleMessage: gets called when there is an incoming message from the client endpoint
111 - self.opcode: the WebSocket frame type (STREAM, TEXT, BINARY)
112 - self.data: bytearray (BINARY frame) or unicode string payload (TEXT frame) 
113 - self.request: HTTP details from the WebSocket handshake (refer to BaseHTTPRequestHandler)
114
115sendMessage: send some text or binary data to the client endpoint
116 - sending data as a unicode object will send a TEXT frame
117 - sending data as a bytearray object will send a BINARY frame
118
119sendClose: send close frame to endpoint
120
121
122---------------------
123The MIT License (MIT)
Note: See TracBrowser for help on using the repository browser.