声明:如有侵权请联系下线
邮箱:fangyingdon@163.com
-
使用flask和geventwebsocket模块创建websocket服务端
from flask import Flask, request from geventwebsocket.handler import WebSocketHandler from gevent.pywsgi import WSGIServer import json from flask_cors import CORS app = Flask(__name__) CORS(app, resources=r'/*') app.secret_key = ';lkjnfdidiclsjek' WEBSOCKET_DICT = {} @app.route('/message') def message(): # 1. 判断是否为Websocket请求,http不包含wsgi.websocket ws = request.environ.get('wsgi.websocket') if not ws: return 'use websocket' # 此处连接成功 current_user_remote_addr = request.remote_addr WEBSOCKET_DICT[current_user_remote_addr] = ws while True: # 2. 等待用户发送消息,并接受 message = ws.receive() # 投票对应的ID print("message\n", message) # 关闭 mesaage = None if not message: del WEBSOCKET_DICT[current_user_remote_addr] break return '完毕' @app.route('/notify', methods=["GET", "POST"]) def notify(): if request.method == "POST": data = request.get_json() for conn in WEBSOCKET_DICT.values(): conn.send(json.dumps(data)) return '完毕' if __name__ == '__main__': # 如果是http请求走app使用原有的wsgi处理,如果是websocket请求走WebSocketHandler处理 http_server = WSGIServer(('127.0.0.1', 5000), app, handler_class=WebSocketHandler) http_server.serve_forever()
-
找到对应的加密js代码本地覆盖
- 在js代码中编写一个自执行的websocket客户端,在接收参数后,调用加密方法(如果不是全局的需要重新赋值给全局变量)
- 之后就可以愉快是调用notify接口了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)