Skip to content

Commit 857d685

Browse files
author
Dave P
committed
autobahn test suite compliance
1 parent 5d5b2a1 commit 857d685

File tree

4 files changed

+435
-411
lines changed

4 files changed

+435
-411
lines changed

README.md

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
<h2>A very simple WebSocket Server written in Python</h2>
2-
<h3>No package installation, just one file, enjoy</h3>
32

43
Supports
5-
- Hixie 76 (Safari and iPhone)
64
- RFC 6455 (All latest browsers)
75
- TLS/SSL
86

9-
<h3><a href = http://opiate.github.io/SimpleWebSocketServer>Click for the fancy Website</a></h3>
7+
Passes Autobahn Websocket Testsuite
108

11-
<h4>A Simple Echo Server Example</h4>
12-
13-
1) Write the client code by extending WebSocket
9+
<h4>Simple Echo Server Example</h4>
1410
`````python
1511
from SimpleWebSocketServer import WebSocket, SimpleWebSocketServer
1612

1713
class SimpleEcho(WebSocket):
18-
14+
1915
def handleMessage(self):
20-
if self.data is None:
21-
self.data = ''
22-
2316
# echo message back to client
24-
self.sendMessage(str(self.data))
17+
self.sendMessage(self.data)
2518

2619
def handleConnected(self):
2720
print self.address, 'connected'
@@ -33,9 +26,7 @@ Supports
3326
server.serveforever()
3427
`````
3528

36-
2) Run your code
37-
38-
3) Open up <i>websocket.html</i> and connect to the server
29+
Open <i>websocket.html</i> and connect to the server.
3930

4031
<h4>Want to get up and running faster?</h4>
4132

@@ -70,46 +61,27 @@ Ensure the <i>websocket.html</i> is also in the same directory to where the serv
7061

7162
5) Change <i>ws://localhost:8000/</i> to <i>wss://localhost:8000</i> and click connect.
7263

73-
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.
64+
Note: if you are having problems connecting, ensure that the certificate is added in your browser against the exception <i>https://localhost:8000</i> or whatever host:port pair you want to connect to.
7465

7566
<h4>For the Programmers</h4>
7667

7768
def handleConnected(): called when handskake is complete
69+
- self.address: TCP address port tuple of the endpoint
7870

7971
def handleClose(): called when the endpoint is closed or there is an error
8072

8173
def handleMessage(): gets called when there is an incoming message from the client endpoint
8274
- self.opcode: the WebSocket frame type (STREAM, TEXT, BINARY)
83-
- self.data: bytearray payload or None if there was no payload
84-
- self.address: TCP address port tuple of the endpoint
75+
- self.data: bytearray (BINARY frame) or unicode payload (TEXT frame)
8576
- self.request: HTTP details from the WebSocket handshake (refer to BaseHTTPRequestHandler)
86-
- self.server.connections: map containing all the clients connected to the server
87-
88-
def sendMessage(buffer): send some text or binary data to the client endpoint
89-
- sending a buffer as str() will send a text based WebSocket frame otherwise a binary frame
77+
78+
def sendMessage(data): send some text or binary data to the client endpoint
79+
- sending data as a unicode object will send a TEXT frame
80+
- sending data as a bytearray object will send a BINARY frame
9081

9182
def sendClose() : send close frame to endpoint
9283

9384

9485
---------------------
9586
The MIT License (MIT)
9687

97-
Copyright (c) 2013 Dave P.
98-
99-
Permission is hereby granted, free of charge, to any person obtaining a copy
100-
of this software and associated documentation files (the "Software"), to deal
101-
in the Software without restriction, including without limitation the rights
102-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
103-
copies of the Software, and to permit persons to whom the Software is
104-
furnished to do so, subject to the following conditions:
105-
106-
The above copyright notice and this permission notice shall be included in all
107-
copies or substantial portions of the Software.
108-
109-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
110-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
111-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
112-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
113-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
114-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
115-
SOFTWARE.

SimpleExampleServer.py

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,44 @@
11
'''
22
The MIT License (MIT)
3-
43
Copyright (c) 2013 Dave P.
5-
6-
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:
7-
8-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9-
10-
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.
114
'''
125

13-
import signal, sys, ssl, logging
6+
import signal, sys, ssl
147
from SimpleWebSocketServer import WebSocket, SimpleWebSocketServer, SimpleSSLWebSocketServer
158
from optparse import OptionParser
169

17-
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
1810

1911
class SimpleEcho(WebSocket):
2012

2113
def handleMessage(self):
22-
if self.data is None:
23-
self.data = ''
24-
25-
try:
26-
self.sendMessage(str(self.data))
27-
except Exception as n:
28-
print n
29-
14+
self.sendMessage(self.data)
15+
3016
def handleConnected(self):
31-
print self.address, 'connected'
17+
pass
3218

3319
def handleClose(self):
34-
print self.address, 'closed'
20+
pass
3521

3622

3723
class SimpleChat(WebSocket):
3824

3925
def handleMessage(self):
40-
if self.data is None:
41-
self.data = ''
42-
4326
for client in self.server.connections.itervalues():
4427
if client != self:
45-
try:
46-
client.sendMessage(str(self.address[0]) + ' - ' + str(self.data))
47-
except Exception as n:
48-
print n
49-
28+
client.sendMessage(self.address[0] + ' - ' + self.data)
5029

5130
def handleConnected(self):
5231
print self.address, 'connected'
5332
for client in self.server.connections.itervalues():
5433
if client != self:
55-
try:
56-
client.sendMessage(str(self.address[0]) + ' - connected')
57-
except Exception as n:
58-
print n
59-
34+
client.sendMessage(self.address[0] + u' - connected')
35+
6036
def handleClose(self):
6137
print self.address, 'closed'
6238
for client in self.server.connections.itervalues():
6339
if client != self:
64-
try:
65-
client.sendMessage(str(self.address[0]) + ' - disconnected')
66-
except Exception as n:
67-
print n
68-
40+
client.sendMessage(self.address[0] + u' - disconnected')
41+
6942

7043
if __name__ == "__main__":
7144

SimpleHTTPSServer.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
'''
22
The MIT License (MIT)
3-
43
Copyright (c) 2013 Dave P.
5-
6-
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:
7-
8-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9-
10-
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.
114
'''
125

136
import BaseHTTPServer, SimpleHTTPServer
147
import ssl
158

169
# openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem
1710
httpd = BaseHTTPServer.HTTPServer(('', 443), SimpleHTTPServer.SimpleHTTPRequestHandler)
18-
httpd.socket = ssl.wrap_socket (httpd.socket, server_side=True, certfile='./cert.pem', keyfile='./cert.pem', ssl_version=ssl.PROTOCOL_TLSv1)
11+
httpd.socket = ssl.wrap_socket(httpd.socket, server_side=True, certfile='./cert.pem', keyfile='./cert.pem', ssl_version=ssl.PROTOCOL_TLSv1)
1912
httpd.serve_forever()

0 commit comments

Comments
 (0)