def test_app_run(self): app = Flask(__name__) twisted = Twisted() self.assertNotEqual(twisted.run, app.run) twisted.init_app(app) self.assertEqual(twisted.run, app.run) app = Flask(__name__) twisted = Twisted(app) self.assertEqual(twisted.run, app.run)
def test_run_trigger(self): app = Flask(__name__) twisted = Twisted(app) @twisted.on('run') def on_run(_app): self.assertEqual(app, _app) self.assertTrue('run' in twisted.events and twisted.events['run']) app.run(host='127.0.0.1', port=5000, run_reactor=False) twisted.run(host='127.0.0.1', port=5001, run_reactor=False) twisted.run_simple(app=app, host='127.0.0.1', port=5002, run_reactor=False)
# -*- coding: utf-8 -*- # Flask from flask import Flask, render_template app = Flask(__name__) @app.route("/") def index(): return render_template('index.html', endpoint='auth') # Twisted from flask.ext.twisted import Twisted twisted = Twisted(app) # SockJS import json from flask.ext.sockjs import SockJS, broadcast ws = SockJS(twisted) auth = ws.createEndpoint('auth') def authenticated(): return [transport for transport in auth.transports() if transport.authenticated is True] @auth.on('connection') def ws_connection(transport): transport.authenticated = False transport.write(json.dumps({'cmd': 'AUTHENTICATION_REQUIRED'})) @auth.on('data') def ws_message(transport, message): if transport.authenticated: