From c86608d90fbe9217b9135c500f0cf301ccaf2c44 Mon Sep 17 00:00:00 2001 From: misha shelemetyev Date: Fri, 12 Aug 2016 16:51:11 -0400 Subject: [PATCH 1/3] Simple broadcasting demo by autobahn, need tons of love from me... add integrations and etc --- BroadcastWebSocketsServer.py | 77 ++++++++++++++++++++++++++++++++++++ index.html | 68 +++++++++++++++++++++++++++++++ uiServer.py | 48 ---------------------- 3 files changed, 145 insertions(+), 48 deletions(-) create mode 100644 BroadcastWebSocketsServer.py create mode 100644 index.html delete mode 100644 uiServer.py diff --git a/BroadcastWebSocketsServer.py b/BroadcastWebSocketsServer.py new file mode 100644 index 0000000..a898ecd --- /dev/null +++ b/BroadcastWebSocketsServer.py @@ -0,0 +1,77 @@ +import sys + +from twisted.internet import reactor +from twisted.python import log +from twisted.web.server import Site +from twisted.web.static import File + +from autobahn.twisted.websocket import WebSocketServerFactory, \ + WebSocketServerProtocol, \ + listenWS + + +class BroadcastServerProtocol(WebSocketServerProtocol): + + def onOpen(self): + self.factory.register(self) + + def onMessage(self, payload, isBinary): + if not isBinary: + msg = "{} from {}".format(payload.decode('utf8'), self.peer) + self.factory.broadcast(msg) + + def connectionLost(self, reason): + WebSocketServerProtocol.connectionLost(self, reason) + self.factory.unregister(self) + + +class BroadcastServerFactory(WebSocketServerFactory): + + """ + Simple broadcast server broadcasting any message it receives to all + currently connected clients. + """ + + def __init__(self, url): + WebSocketServerFactory.__init__(self, url) + self.clients = [] + self.tickcount = 0 + self.tick() + + def tick(self): + self.tickcount += 1 + self.broadcast("tick %d from server" % self.tickcount) + reactor.callLater(1, self.tick) + + def register(self, client): + if client not in self.clients: + print("registered client {}".format(client.peer)) + self.clients.append(client) + + def unregister(self, client): + if client in self.clients: + print("unregistered client {}".format(client.peer)) + self.clients.remove(client) + + def broadcast(self, msg): + print("broadcasting message '{}' ..".format(msg)) + for c in self.clients: + c.sendMessage(msg.encode('utf8')) + print("message sent to {}".format(c.peer)) + +if __name__ == '__main__': + + log.startLogging(sys.stdout) + + ServerFactory = BroadcastServerFactory + # ServerFactory = BroadcastPreparedServerFactory + + factory = ServerFactory(u"ws://127.0.0.1:9126") + factory.protocol = BroadcastServerProtocol + listenWS(factory) + + webdir = File(".") + web = Site(webdir) + reactor.listenTCP(8181, web) + + reactor.run() diff --git a/index.html b/index.html new file mode 100644 index 0000000..c310958 --- /dev/null +++ b/index.html @@ -0,0 +1,68 @@ + + + + + + +

Autobahn WebSocket Broadcast Demo

+ +
+

Broadcast Message:

+
+ +

+   
+
\ No newline at end of file
diff --git a/uiServer.py b/uiServer.py
deleted file mode 100644
index 5f107b5..0000000
--- a/uiServer.py
+++ /dev/null
@@ -1,48 +0,0 @@
-#!/usr/bin/env python
-import os
-import subprocess
-import sys
-
-from compression.compressor import Compressor
-from transport.server.UdpSocket import UdpSocket
-from encryption.encryptor import Encryptor
-from config.config import Config
-
-
-sys.path.insert(0, os.getcwd())
-
-
-# Get the args
-def main():
-    # Clear the screen
-    subprocess.call('clear', shell=True)
-    config_object = Config(os.getcwd() + '/config/config.ini').raw_config_object
-    transport = UdpSocket(config_object)
-
-    if config_object['COMPRESSION']['switch'] == 'On':
-        compressor = Compressor(config_object)
-        transport.add_compression(compressor)
-
-    if config_object['ENCRYPTION']['switch'] == 'On':
-        #encryptor = Encryptor(config_object)
-        #transport.add_encryption(encryptor)
-        print("test")
-    try:
-        while True:
-            '''
-            do stuff
-            '''
-            transport.handle_client()
-            print('\nPrint ... Server running \n')
-
-    except LookupError as e:
-        print(e)
-        sys.exit(2)
-
-    except KeyboardInterrupt:
-        print('keyboard interruption')
-        sys.exit(1)
-
-
-if __name__ == "__main__":
-    main()

From 86df47360c66b9c46f2a4dada4299c2067c0ac90 Mon Sep 17 00:00:00 2001
From: misha shelemetyev 
Date: Fri, 12 Aug 2016 16:54:47 -0400
Subject: [PATCH 2/3] just simple client count fucn

---
 BroadcastWebSocketsServer.py | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/BroadcastWebSocketsServer.py b/BroadcastWebSocketsServer.py
index a898ecd..8b1be4b 100644
--- a/BroadcastWebSocketsServer.py
+++ b/BroadcastWebSocketsServer.py
@@ -11,7 +11,6 @@
 
 
 class BroadcastServerProtocol(WebSocketServerProtocol):
-
     def onOpen(self):
         self.factory.register(self)
 
@@ -26,7 +25,6 @@ def connectionLost(self, reason):
 
 
 class BroadcastServerFactory(WebSocketServerFactory):
-
     """
     Simple broadcast server broadcasting any message it receives to all
     currently connected clients.
@@ -59,8 +57,11 @@ def broadcast(self, msg):
             c.sendMessage(msg.encode('utf8'))
             print("message sent to {}".format(c.peer))
 
-if __name__ == '__main__':
+    def getClientsCount(self):
+        return list.count(self.clients)
+    
 
+if __name__ == '__main__':
     log.startLogging(sys.stdout)
 
     ServerFactory = BroadcastServerFactory

From 29512915ceb749d61bc214babca37c7060fe3f7b Mon Sep 17 00:00:00 2001
From: Misha Shelemetyev 
Date: Mon, 1 Oct 2018 14:43:05 -0400
Subject: [PATCH 3/3] *Project restarted*

---
 README.md         | 12 ++++--------
 config/config.ini |  2 +-
 requirements.txt  | 11 +++++++++++
 3 files changed, 16 insertions(+), 9 deletions(-)
 create mode 100644 requirements.txt

diff --git a/README.md b/README.md
index 52363cc..666654a 100644
--- a/README.md
+++ b/README.md
@@ -12,14 +12,10 @@ To enable encryption you need to run keyGen.py to generate priv/public keys
 Also make sure that switches are on in config.ini
 
 ##How to run:
-* sudo python3 client.py (on the client side (where the camera is))
-* sudo python3 server.py (on the server side (where the web ui should be))
-
-## Things you need to run it
-* linux
-* python3
-* openCv
-* python Crypto (sudo apt-get install python3-crypto)
+* pip install -r requirements.txt
+* adjust config.ini
+* on the server python server.py
+* on the client python client.py
 
 ## Trello board with info of what is going on:
 https://trello.com/b/ZxRjPN2B/pythonvideostream
\ No newline at end of file
diff --git a/config/config.ini b/config/config.ini
index 560b72a..3e2de45 100644
--- a/config/config.ini
+++ b/config/config.ini
@@ -2,7 +2,7 @@
 tmp_directory = /tmp/
 buffer_file = /tmp/buff.jpg
 [TRANSPORT]
-server_address = web2u.org
+server_address = 0.0.0.0
 port = 4080
 ui_port = 8181
 [COMPRESSION]
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..50dbe94
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,11 @@
+certifi==2018.8.24
+chardet==3.0.4
+crypto==1.4.1
+idna==2.7
+Naked==0.1.31
+numpy==1.15.2
+opencv-python==3.4.3.18
+PyYAML==3.13
+requests==2.19.1
+shellescape==3.4.1
+urllib3==1.23