You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<h2>A very simple WebSocket Server written in Python</h2>
2
-
<h3>No package installation, just one file, enjoy</h3>
3
2
4
3
Supports
5
-
- Hixie 76 (Safari and iPhone)
6
4
- RFC 6455 (All latest browsers)
7
5
- TLS/SSL
8
6
9
-
<h3><ahref = http://opiate.github.io/SimpleWebSocketServer>Click for the fancy Website</a></h3>
7
+
Passes Autobahn Websocket Testsuite
10
8
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>
14
10
`````python
15
11
from SimpleWebSocketServer import WebSocket, SimpleWebSocketServer
16
12
17
13
classSimpleEcho(WebSocket):
18
-
14
+
19
15
defhandleMessage(self):
20
-
ifself.data isNone:
21
-
self.data =''
22
-
23
16
# echo message back to client
24
-
self.sendMessage(str(self.data))
17
+
self.sendMessage(self.data)
25
18
26
19
defhandleConnected(self):
27
20
printself.address, 'connected'
@@ -33,9 +26,7 @@ Supports
33
26
server.serveforever()
34
27
`````
35
28
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.
39
30
40
31
<h4>Want to get up and running faster?</h4>
41
32
@@ -70,46 +61,27 @@ Ensure the <i>websocket.html</i> is also in the same directory to where the serv
70
61
71
62
5) Change <i>ws://localhost:8000/</i> to <i>wss://localhost:8000</i> and click connect.
72
63
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.
74
65
75
66
<h4>For the Programmers</h4>
76
67
77
68
def handleConnected(): called when handskake is complete
69
+
- self.address: TCP address port tuple of the endpoint
78
70
79
71
def handleClose(): called when the endpoint is closed or there is an error
80
72
81
73
def handleMessage(): gets called when there is an incoming message from the client endpoint
82
74
- 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)
85
76
- 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
90
81
91
82
def sendClose() : send close frame to endpoint
92
83
93
84
94
85
---------------------
95
86
The MIT License (MIT)
96
87
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
Copy file name to clipboardExpand all lines: SimpleExampleServer.py
+10-37Lines changed: 10 additions & 37 deletions
Original file line number
Diff line number
Diff line change
@@ -1,71 +1,44 @@
1
1
'''
2
2
The MIT License (MIT)
3
-
4
3
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.
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.
0 commit comments