## http://www.apache.org/licenses/LICENSE-2.0 ## ## Unless required by applicable law or agreed to in writing, software ## distributed under the License is distributed on an "AS IS" BASIS, ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ## See the License for the specific language governing permissions and ## limitations under the License. ## ############################################################################### from twisted.internet.defer import inlineCallbacks, returnValue from klein import Klein from autobahn.twisted.wamp import Application app = Klein() wampapp = Application() @app.route('/square/submit', methods=['POST']) @inlineCallbacks def square_submit(request): x = int(request.args.get('x', [0])[0]) res = yield wampapp.session.call('com.example.square', x) returnValue("{} squared is {}".format(x, res)) if __name__ == "__main__": import sys from twisted.python import log from twisted.web.server import Site from twisted.internet import reactor
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # ############################################################################### from flask import Flask from crochet import setup, run_in_reactor, wait_for # this MUST be called _before_ any Autobahn or Twisted imports! setup() from autobahn.twisted.wamp import Application # noqa # our WAMP app # wapp = Application() # this is a synchronous wrapper around the asynchronous WAMP code # @wait_for(timeout=1) def publish(topic, *args, **kwargs): return wapp.session.publish(topic, *args, **kwargs) # our Flask app # app = Flask(__name__) app._visits = 0
# 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. # ############################################################################### import jinja2 from klein import Klein from twisted.internet.defer import inlineCallbacks, returnValue from autobahn.twisted.wamp import Application # This is our WAMP application ## wampapp = Application('com.example') @wampapp.register() def square(x): print("square() called with {}".format(x)) return x * x # This is our Web application ## webapp = Klein() webapp.visits = 0 webapp.templates = jinja2.Environment(loader=jinja2.FileSystemLoader('templates'))
## See the License for the specific language governing permissions and ## limitations under the License. ## ############################################################################### from autobahn.twisted.wamp import Application import socket import uuid # Comme pour flask, l'objet app # est ce qui lie tous les éléments # de notre code ensemble. On lui donne # un nom, ici "demo" app = Application('io.crossbar.demo.videocontroller') # Bien que l'app va démarrer un serveur # pour nous, l'app est bien un CLIENT # du serveur WAMP. Le serveur démarré # automatiquement n'est qu'une facilité # pour le dev. En prod on utiliserait # crossbar. # Juste un conteneur pour y mettre notre IP app._data = {} # On déclare que cette fonction sera appelée # quand l'app se sera connectée au serveur WAMP. # Ceci permet de lancer du code juste après # le app.run() que l'on voit en bas du fichier. # '_' est une convention en Python pour dire
if (filters.get('show_disk', True)): disks = {} for device in psutil.disk_partitions(): usage = psutil.disk_usage(device.mountpoint) disks[device.mountpoint] = '{used}/{total} ({percent}%)'.format( used=to_gib(usage.used), total=to_gib(usage.total), percent=usage.percent) results['disks'] = disks return results # On créé le client WAMP. app = Application('monitoring') # Ceci est l'IP publique de ma machine puisque # ce client doit pouvoir accéder à mon serveur # depuis l'extérieur. SERVER = '172.17.42.1' # D'abord on utilise une astuce pour connaître l'IP publique de cette # machine. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("8.8.8.8", 80)) # On attache un dictionnaire à l'app, ainsi # sa référence sera accessible partout. app._params = {'name': socket.gethostname(), 'ip': s.getsockname()[0]} s.close()
# -*- coding: utf-8 -*- from autobahn.twisted.wamp import Application import socket import uuid # Comme pour flask, l'objet app # est ce qui lie tous les éléments # de notre code ensemble. On lui donne # un nom, ici "demo" app = Application('demo') # Bien que l'app va démarrer un serveur # pour nous, l'app est bien un CLIENT # du serveur WAMP. Le serveur démarré # automatiquement n'est qu'une facilité # pour le dev. En prod on utiliserait # crossbar. # Juste un conteneur pour y mettre notre IP app._data = {} # On déclare que cette fonction sera appelée # quand l'app se sera connectée au serveur WAMP. # Ceci permet de lancer du code juste après # le app.run() que l'on voit en bas du fichier. # '_' est une convention en Python pour dire # "ce nom n'a aucune importance, c'est du code # jetable qu'un utilisera une seule fois". @app.signal('onjoined')
from autobahn.twisted.wamp import Application from twisted.internet.defer import inlineCallbacks SERVER = 'drc.blue-labs.org' # note, this prefix specification is actually pointless at the moment root = 'org.blue_labs.drchrono' app = Application(root) @app.signal('onjoined') def called_on_joined(): """ Loop sending the state of this machine using WAMP every x seconds. This function is executed when the client (myself) joins the router, which means it's connected and authenticated, ready to send WAMP messages. Things we'll do here: 1. pull the patient and appointment data on startup 2. emit a publish() message when a modification to the base data occurs. this is so the appointments page updates in real time. """ print("Connected") @app.subscribe(root + '.appointment') @app.subscribe(root + '.appointment.create') @app.subscribe(root + '.appointment.modify') @app.subscribe(root + '.appointment.delete') @app.subscribe(root + '.patient') @app.subscribe(root + '.patient.create')
# 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. # ############################################################################### from os import environ from twisted.internet.defer import returnValue from autobahn.twisted.wamp import Application app = Application(u'com.example') @app.register() def add2(a, b): print("add2() called") return a + b @app.register(u'com.example.hello') def hello(): print("hello() called") res = yield app.session.call(u'com.example.add2', 2, 3) returnValue("Hello {}".format(res))
# coding: utf-8 from __future__ import unicode_literals from autobahn.twisted.wamp import Application import socket import uuid # Just like for flask, the app object # is what's bind all elements together. # We give it a name, here "demo". app = Application("demo") # While the app is going to start # a server for us, the app is a CLIENT # of the WAMP server. The server is # started automatically as a courtesy # for dev purpose. In production, we # would use crossbar. # Just a container to store the IP app._data = {} # We ask for this function to be called when the # app is connected to the WAMP server. This allow # us to run caode right after the app.run() call # you can see at the bottom of the file. '_' is # a convention in Python meaning "this name has # no importance, it's disposable code that we'll # use only once" @app.signal("onjoined")