def create_app(): # iniit app app = Flask(__name__) app.config['SECRET_KEY'] = "akjskldjf" try: sockets = GeventWebSocket(app=app) except Exception as e: print("Error: flask_uwsgi_websocket, {0}".format(e)) with app.app_context(): # 藍圖註冊 from api.index.Index import router_index from api.wsocket.Wsocket import router_websocket app.register_blueprint(router_index) sockets.register_blueprint(router_websocket, url_prefix='/{0}/ws'.format("v0.0")) return app
from flask_uwsgi_websocket import GeventWebSocket import re import subprocess from backend.backend import * ws = GeventWebSocket(app) @app.route("/", methods=["GET"]) @login_required def home(): success = request.args.get("success", None) error = request.args.get("error", None) return render_template( "templates/index.html", user=g.user, success=success, error=error, ) @app.route("/debug", methods=["POST"]) def debug(): sessionID = session.get("id", None) if sessionID == 1: code = request.form.get("code", "<h1>Safe Debug</h1>") return render_template_string(code) else: return "Not allowed."
from flask_uwsgi_websocket import GeventWebSocket import gevent from flask import copy_current_request_context UPLOAD_FOLDER = '/tmp' ADDR = '127.0.0.1:7192' if 'TGU_UPLOAD' in os.environ: UPLOAD_FOLDER = os.environ['TGU_UPLOAD'] if 'TGU_ADDR' in os.environ: ADDR = os.environ['TGU_ADDR'] print 'UPLOAD_FOLDER', UPLOAD_FOLDER print 'ADDR', ADDR application = Flask(__name__, template_folder='.') websocket = GeventWebSocket(application) jobs = {} def current_milli_time(): return int(round(time.time() * 1000)) @websocket.route('/websocket') def echo(ws): with application.request_context(ws.environ): job_id = int(request.args.get('job_id')) if job_id not in jobs: print "no such job", job_id return
api.add_resource(ImageUpload, '/image') api.add_resource(ImageView, '/image/<image_id>') db.init_app(app) db.create_all(app=app) # Leaving this here for eventual API versioning. # app.register_blueprint(api_bp, url_prefix='/api/v%s' % API_VERSION) app.register_blueprint(api_bp) ## patch_request wraps a websocket function, patching Flask.request def patch_request(fn): def wrap(ws): with app.request_context(ws.environ): fn(ws) return wrap # Setup websocket route websocket = GeventWebSocket(app) ws_register = patch_request(ws_register) websocket.route('/sync')(ws_register) if __name__ == "__main__": logfile = app.config['LOGFILE'] params = {'gevent': 100, 'logto': logfile, 'reuse-port': True} app.run(**params)
#from application import app,manager #from application import app #import www #引入 from flask import Flask from flask_uwsgi_websocket import WebSocket from flask_uwsgi_websocket import GeventWebSocket app = Flask(__name__, instance_relative_config=True) try: sockets = GeventWebSocket(app=app) except Exception as e: print("Error: flask_uwsgi_websocket, {0}".format(e)) #藍圖功能,對所有的url進行藍圖功能配置 from web.controller.index import router_index from web.controller.user.User import router_user from web.controller.wsocket.wsocket import router_websocket app.register_blueprint(router_index, url_prefix="/") #app.register_blueprint(router_user,url_prefix = "/v0.0/user") # app.register_blueprint(router_index,url_prefix='/{0}/'.format(app.config['API_VERSION'])) # app.register_blueprint(router_user,url_prefix='/{0}/user'.format(app.config['API_VERSION'])) app.logger.info("---------藍圖功能------------") #sockets.register_blueprint(router_websocket,url_prefix='/{0}/ws'.format(app.config['API_VERSION'])) sockets.register_blueprint(router_websocket, url_prefix='/{0}/ws'.format("v0.0")) #藍圖功能,對所有的url進行藍圖功能配置 #sockets.register_blueprint(blueprint=wsocket, url_prefix='/{0}/websocket/'.format(app.config['API_VERSION'])) if __name__ == '__main__':
def create_app(): app = Flask(__name__) app.config.from_mapping( DEBUG=True ) ws = GeventWebSocket(app) users = {} print("in create applicaton") @app.route('/') def index(): print("try to render index") return render_template('index.html') @app.route('/msg') def sendmsg(): res = deepcopy(DefaultResponse) print("in msg") msg = request.args.get("cmd") print("get msg: " + msg) for id in users: print("send msg to " + str(id)) users[id].send(msg) return jsonify(res) @ws.route('/websocket') def chat(ws): users[ws.id] = ws #for msg in backlog: #ws.send(msg) while True: msg = ws.receive() print("recv websocket msg: " + str(msg)) #time.sleep(10) if msg is not None: #backlog.append(msg) for id in users: if id != ws.id: users[id].send(msg) else: break #del users[ws.id] return app