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