Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First commit.

  • Loading branch information...
commit 2c8b9f47d14bf91829d6d924bbf2360a4f2a8d1f 1 parent 82becc8
@AdrianGaudebert authored
View
1  .gitignore
@@ -0,0 +1 @@
+*.pyc
View
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2010 Adrian Gaudebert - adrian@gaudebert.fr
+
+ 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.
View
4 config.py
@@ -0,0 +1,4 @@
+# Socket configuration
+httphost = "localhost"
+socketport = 9876
+sockethost = "localhost:9876"
View
5 server.py
@@ -0,0 +1,5 @@
+#!/usr/bin/env python
+
+import websocketserver
+
+websocketserver.WebSocketServer().start()
View
64 websocketclient.py
@@ -0,0 +1,64 @@
+#!/usr/bin/env python
+
+import socket
+import threading
+import config
+
+class WebSocketClient(threading.Thread):
+ """
+ A single connection (client) of the program
+ """
+ def __init__(self, sock, addr, server):
+ threading.Thread.__init__(self)
+ self.s = sock
+ self.addr = addr
+ self.server = server
+
+ def run(self):
+ # Handshaking, create the WebSocket connection
+ handshake = "HTTP/1.1 101 Web Socket Protocol Handshake\r\nUpgrade: WebSocket\r\nConnection: Upgrade\r\nWebSocket-Origin: http://%s\r\nWebSocket-Location: ws://%s/\r\nWebSocket-Protocol: sample\r\n\r\n" % (config.httphost, config.sockethost)
+ self.s.send(handshake)
+ data = self.s.recv(1024)
+
+ # Receive and handle data
+ while 1:
+ data = self.s.recv(1024)
+ if not data: break
+ print 'Data from', self.addr, ':', data
+ self.onreceive(data)
+
+ # Close the client connection
+ self.close()
+
+ def close(self):
+ """
+ Close this connection
+ """
+ print 'Client closed: ', self.addr
+ self.server.remove(self.s)
+ self.s.close()
+
+ def send(self, msg):
+ """
+ Send a message to this client
+ """
+ msg = '\x00' + msg + '\xff'
+ self.s.send(msg)
+
+ def onreceive(self, data):
+ """
+ Event called when a message is received from this client
+
+ Example:
+ Here, send back the received message to all the clients
+ """
+ data = self._clean(data)
+ self.server.send_all(data)
+
+ def _clean(self, msg):
+ """
+ Remove special chars used for the transmission
+ """
+ msg = msg.replace('\x00', '', 1)
+ msg = msg.replace('\xff', '', 1)
+ return msg
View
31 websocketserver.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python
+
+import socket
+import threading
+import config
+import websocketclient
+
+class WebSocketServer:
+ def __init__(self):
+ self.clients = []
+
+ def start(self):
+ s = socket.socket()
+ s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ s.bind(('', config.socketport))
+ s.listen(1)
+ while 1:
+ conn, addr = s.accept()
+ print 'Connected by', addr
+ self.clients.append(conn)
+ websocketclient.WebSocketClient(conn, addr, self).start()
+
+ def send_all(self, data):
+ data = '\x00' + data + '\xff'
+ [conn.send(data) for conn in self.clients]
+
+ def remove(self, client):
+ l = threading.Lock()
+ l.acquire()
+ self.clients.remove(client)
+ l.release()
Please sign in to comment.
Something went wrong with that request. Please try again.