跨域访问定义,以及python,flask-restful解决跨域访问的问题
2023-09-11 14:14:27 时间
跨域是指从一个域名的网页去请求另一个域名的资源。比如从http://www.baidu.com/ 页面去请求 http://www.google.com 的资源。跨域的严格一点的定义是:只要 协议,域名,端口有任何一个的不同,就被当作是跨域。 如何实现跨域取到数据,LS说的JSONP是最为常见,你可以参考这篇文章 跨域与跨域访问
安装Flask-Cors
pip install Flask-Cors
或https://pypi.python.org/pypi/… 下载tar 解压安装
在文件中加入:
#cors 跨域访问
from flask_cors import *
app = Flask(name);
CORS(app, supports_credentials=True);
即可完成
# -*- coding: utf-8 -*-
from flask import Flask
from flask_restful import reqparse, Api, Resource
from flask_cors import *
from model.ernie_gram_zh_static._36Kr.static_json import PredictErnie
app = Flask(__name__)
app.config["JSON_AS_ASCII"] = False
app.config["RESTFUL_JSON"] = {'ensure_ascii': False}
CORS(app, supports_credentials=True)
api = Api(app)
Todos = {
'XX': '实体抽取',
}
parser = reqparse.RequestParser()
parser.add_argument('text')
# Todo
# shows a single todo item and lets you delete a todo item
predict_ernie = PredictErnie()
class EntityRec(Resource):
"""
pass
"""
def get(self):
return {'task': Todos.get('XX')}
def post(self):
args = parser.parse_args()
results = predict_ernie.do_predict(args['text'])
return results, 200
# Actually setup the Api resource routing here
api.add_resource(EntityRec, 'XXX')
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0', port=9535)
# server = pywsgi.WSGIServer(('0.0.0.0', 5000), app, log=LogWrite()) # 日志模块影响通信效率,在nginx端做
print('server 已启动')
# server = pywsgi.WSGIServer(('0.0.0.0', 5000), app)
# server.serve_forever()
相关文章
- Python 基本语法,文件读写,数据结构和类型
- Python中Selenium模块的使用
- python执行脚本加参数_命令行运行Python脚本时传入参数的三种方式详解以及argparse子命令subparsers()方法
- Python任意参数的数量/预习课python基础
- Python 卸载python
- Python零基础入门,2分钟带你了解python
- Python的条件锁与事件共享
- 《教孩子学编程(Python语言版)》——1.3 运行Python程序
- python学习之OpenCV-Python模块的部分应用示例(生成素描图和动漫图)
- python安装requests (win7 & centos7)
- python使用flask模块和前端交互基础
- python版本的两款NVIDIA显卡管理查询工具
- 华为OD机试 - 运动会(Python) | 机试题+算法思路+考点+代码解析 【2023】
- python openpyxl 操作excel xlrd.biffh.XLRDError: Excel xlsx file; not supported错误修改
- 【Python注意事项】如何理解python中间generator functions和yield表情
- 2.1 The Python Interpreter(python解释器)
- GET RESTful With Python
- Selenium4 Python实现Page Factory设计模式,python新的定位方法