Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
# -*- 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:
Exemplo n.º 6
0
from twisted.internet import reactor

from flask import Flask, render_template
from echo import EchoProtocol

app = Flask(__name__)


@app.route("/")
def index():
    return render_template('index.html')


from flask.ext.twisted import Twisted

twisted = Twisted(app)

container = Container()  # 实例化一个工厂容器

container.buildFactory(RFIDFactory, 'RFID', RFIDProtocol)  # 创建一个名字是rfid的RFIDFactory工厂实例
container.buildFactory(EchoFactory, 'echo', EchoProtocol)  # 创建一个名字是echo的EchoFactory工厂实例

twisted.add_resource("echo", SockJSResource(container.servers['echo']))

SerialPort(RFIDProtocol(container.servers['RFID']), '/dev/ttyUSB0', reactor, baudrate=38400)  # '/dev/ttyUSB0'
# twisted老套路(端口号,工厂)
reactor.listenTCP(8001, container.servers['echo'])

if __name__ == '__main__':
    print 'reactor begin to run '
    app.run()