Python flask cross-domain support (Access-Control-Allow-Origin (CORS) cross-domain resource sharing (access control allows origin: allow specified origins to make cross-domain requests) browser same-origin policy

Article directory cross domain advantage shortcoming remedy The flask interface supports cross-domain setting methods Global Settings Individual interface settings Conflict between @app.route decorator and @cross_origin decorator request method How to detect whether a flask interface supports cross domain? Demo 1: View with chrome browser Demo 2: View with postman Access-Control-Allow-Origin (CORS cross-origin resource sharing) field […]

Python Flask web project uwsgi + nginx deployment

1. Install python slightly 2. Virtual environment 2.1 Install verticalenv pip3 install virtualenv 2.2 Create a virtual environment Create a directory to hold the environment: mkdir venvs Create a virtual environment: [<a href=”/cdn-cgi/l/email-protection” class=”__cf_email__” data-cfemail=”493b26263d093b26263d”>[email protected]</a> /]# virtualenv /home/xxx/venvs/flask2 –python=python3 Check out the virtual environment: [<a href=”/cdn-cgi/l/email-protection” class=”__cf_email__” data-cfemail=”cdbfa2a2b98dbfa2a2b9″>[email protected]</a> venvs]# ls flask2 2.3 Activate the virtual […]

Python Flask uses gevent or grpc.gevent module to implement asynchronous non-blocking

Directory 0. Preface: 1. Introduction to gevent.monkey: 2. Introduction to grpc.gevent: 3. Flask Demo code 4. Pressure test results 5. Stress test report 6 Conclusion 0. Foreword: Flask itself is not an asynchronous framework, so there will be performance bottlenecks when processing high concurrent requests. However, Flask can improve concurrency performance through integration with other […]

Deploy the python flask project to the server and make a docker image

Deploy python web project to server (using docker) Reference: https://blog.51cto.com/fish/6023519 (1) Create a virtual environment Create directory mkdir mytest cd mytest [root@python mytest]# pwd /root/mytest Create a virtual environment [root@python mytest]# python3 -m venv myvenv Activate the virtual environment [root@python mytest]# source myvenv/bin/activate Once the virtual environment is activated, it will enter the shell interface […]

Python Flask API explanation and example demonstration (with cookies and session)

Article directory I. Overview 2. Explanation of common functions 1) Flask() function 2) route() function 3) jsonify() function 4) render_template() function 5) redirect() function 6) url_for() function 7) before_request() function 8) after_request() function 9) abort() function 10) send_file() function 3. Explanation of common objects 1) request object 2) session object 4. Cookies and sessions in […]

[Solved] The solution to the invalidation of the Python flask project to modify the access ip and port number

Recently, I found a problem when I was working on the flask project. Changing the default port in the project was invalid: if __name__ == ‘__main__’: app.run(host=’127.0.1.3′,port=5555 ) #run(host, port, debug, **options) #host The hostname to listen on. Defaults to 127.0.0.1 (localhost). Set to “0.0.0.0” to make the server available externally #port port number default […]

[Solved] Python Flask appears No module named ‘markupsafe._compat

Python: A solution to the problem of No module named ‘markupsafe._compat’ in Flask in Python In the Python directory, go to the “\Lib\site-packages\markupsafe” folder and create a new _compat.py file with the following contents: ? # -*- coding: utf-8 -*- “”” markupsafe._compat ~~~~~~~~~~~~~~~~~~ Compatibility module for different Python versions. :copyright: (c) 2013 by Armin Ronacher. […]

[Solved] python flask returns the read image to the web front end

Python code found online (core part): def return_img_stream(img_local_path): “”” Utility function: Get local image stream :param img_local_path: The local absolute path of the single image of the file :return: image stream “”” import base64 img_stream = ” with open(img_local_path, ‘r’) as img_f: img_stream = img_f.read() img_stream = base64.b64encode(img_stream) return img_stream @app.route(‘/’) def hello_world(): img_path = […]