Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

First commit.

  • Loading branch information...
commit 2c8b9f47d14bf91829d6d924bbf2360a4f2a8d1f 1 parent 82becc8
Adrian Gaudebert authored July 21, 2010
1  .gitignore
... ...
@@ -0,0 +1 @@
  1
+*.pyc
19  LICENSE
... ...
@@ -0,0 +1,19 @@
  1
+Copyright (c) 2010 Adrian Gaudebert - adrian@gaudebert.fr
  2
+
  3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
  4
+ of this software and associated documentation files (the "Software"), to deal
  5
+ in the Software without restriction, including without limitation the rights
  6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7
+ copies of the Software, and to permit persons to whom the Software is
  8
+ furnished to do so, subject to the following conditions:
  9
+
  10
+ The above copyright notice and this permission notice shall be included in
  11
+ all copies or substantial portions of the Software.
  12
+
  13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19
+ THE SOFTWARE.
4  config.py
... ...
@@ -0,0 +1,4 @@
  1
+# Socket configuration
  2
+httphost = "localhost"
  3
+socketport = 9876
  4
+sockethost = "localhost:9876"
5  server.py
... ...
@@ -0,0 +1,5 @@
  1
+#!/usr/bin/env python
  2
+
  3
+import websocketserver
  4
+
  5
+websocketserver.WebSocketServer().start()
64  websocketclient.py
... ...
@@ -0,0 +1,64 @@
  1
+#!/usr/bin/env python
  2
+
  3
+import socket
  4
+import threading
  5
+import config
  6
+
  7
+class WebSocketClient(threading.Thread):
  8
+    """
  9
+    A single connection (client) of the program
  10
+    """
  11
+    def __init__(self, sock, addr, server):
  12
+        threading.Thread.__init__(self)
  13
+        self.s = sock
  14
+        self.addr = addr
  15
+        self.server = server
  16
+
  17
+    def run(self):
  18
+        # Handshaking, create the WebSocket connection
  19
+        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)
  20
+        self.s.send(handshake)
  21
+        data = self.s.recv(1024)
  22
+
  23
+        # Receive and handle data
  24
+        while 1:
  25
+            data = self.s.recv(1024)
  26
+            if not data: break
  27
+            print 'Data from', self.addr, ':', data
  28
+            self.onreceive(data)
  29
+
  30
+        # Close the client connection
  31
+        self.close()
  32
+
  33
+    def close(self):
  34
+        """
  35
+        Close this connection
  36
+        """
  37
+        print 'Client closed: ', self.addr
  38
+        self.server.remove(self.s)
  39
+        self.s.close()
  40
+
  41
+    def send(self, msg):
  42
+        """
  43
+        Send a message to this client
  44
+        """
  45
+        msg = '\x00' + msg + '\xff'
  46
+        self.s.send(msg)
  47
+
  48
+    def onreceive(self, data):
  49
+        """
  50
+        Event called when a message is received from this client
  51
+
  52
+        Example:
  53
+        Here, send back the received message to all the clients
  54
+        """
  55
+        data = self._clean(data)
  56
+        self.server.send_all(data)
  57
+
  58
+    def _clean(self, msg):
  59
+        """
  60
+        Remove special chars used for the transmission
  61
+        """
  62
+        msg = msg.replace('\x00', '', 1)
  63
+        msg = msg.replace('\xff', '', 1)
  64
+        return msg
31  websocketserver.py
... ...
@@ -0,0 +1,31 @@
  1
+#!/usr/bin/env python
  2
+
  3
+import socket
  4
+import threading
  5
+import config
  6
+import websocketclient
  7
+
  8
+class WebSocketServer:
  9
+    def __init__(self):
  10
+        self.clients = []
  11
+
  12
+    def start(self):
  13
+        s = socket.socket()
  14
+        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  15
+        s.bind(('', config.socketport))
  16
+        s.listen(1)
  17
+        while 1:
  18
+            conn, addr = s.accept()
  19
+            print 'Connected by', addr
  20
+            self.clients.append(conn)
  21
+            websocketclient.WebSocketClient(conn, addr, self).start()
  22
+
  23
+    def send_all(self, data):
  24
+        data = '\x00' + data + '\xff'
  25
+        [conn.send(data) for conn in self.clients]
  26
+
  27
+    def remove(self, client):
  28
+        l = threading.Lock()
  29
+        l.acquire()
  30
+        self.clients.remove(client)
  31
+        l.release()

0 notes on commit 2c8b9f4

Please sign in to comment.
Something went wrong with that request. Please try again.