1. Installing the Pyopenssl module
Pip Install Pyopenssl
If the flask is installed in a python3 environment, remember to switch to the PYTHON3 virtual environment
2, upload the certificate file to the project root directory, I use the public network certificate file, you can also manually produce self-signed certificate, there are many examples on the Internet, do not list.
3, configure HTTPS access, can be implemented in three ways
3.1, directly using the Flask framework of the server, modify the code for HTTPS access
From flask Import Flaskapp = Flask (__name__) @app. Route ("/") def index (): Return ' Hello world ' if __name__ = = ' __main__ ': App.run (' 0.0.0.0 ', port=8100,ssl_context= ('./server.pem ', './server.key '))
Start flask
Python myapp.py * Running on https://0.0.0.0:8100/(press CTRL + C to quit)
3.2, using Gunicorn to implement HTTPS access, the code can not add a certificate file configuration
From flask Import Flaskapp = Flask (__name__) @app. Route ("/") def index (): Return ' Hello world ' if __name__ = = ' __main__ ': App.run ()
To start the service with Gunicorn, add the specified certificate file parameter
gunicorn-w4-b0.0.0.0:8000--certfile=server.pem--keyfile=server.key myapp:app[2017-08-22 10:47:34 +0800] [23118] [ Info] Starting Gunicorn 19.7.1[2017-08-22 10:47:34 +0800] [23118] [INFO] Listening at:https://0.0.0.0:8000 (23118) [2017- 08-22 10:47:34 +0800] [23118] [INFO] Using worker:sync[2017-08-22 10:47:34 +0800] [23121] [INFO] booting worker with PID: 23121[2017-08-22 10:47:34 +0800] [23122] [info] booting worker with pid:23122[2017-08-22 10:47:34 +0800] [23123] [INFO] Booting worker with Pid:23123[2017-08-22 10:47:34 +0800] [23124] [INFO] booting worker with pid:23124
Once the service is started, it can be accessed via HTTPS.
3.3. Add the certificate file on the proxy server via the Nginx proxy
server{ listen 443; server_name abc.abc.com; ssl on; ssl_certificate server.pem; ssl_certificate_key server.key; ssl_session_ timeout 5m; ssl_protocols tlsv1 tlsv1.1 tlsv1.2; ssl_ciphers high:! rc4:! md5:!anull:!enull:! Null:! Dh:! edh:! exp:+medium; ssl_prefer_server_ciphers on; location / { proxy_connect_timeout 3; proxy_send_timeout 30; proxy_read_ timeout 30; proxy_pass http://*.*.*.*:5000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote _addr; proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for; } }
This is my implementation of three ways, other small partners have another way, please advise, thank you!
know-how Office, professional mobile office platformhttps://zx.naton.cn/
"Director" 12 Spring and Autumn,[email protected];
"Master" Zelo,[email protected];
"Operation" Wolf World,[email protected];
"Product design" stray cats,[email protected];
"Experience Design",[email protected];
"IOS" Amoy code handyman,[email protected]; imcg33k,[email protected];
"Android", the Ape-Lay,[email protected]; an epiphany of thought,[email protected];
"Java" chief engineer Mr_w,[email protected];
"Test" the Earth Mirror asked,[email protected];
"Data" is much more joyful,[email protected];
"Security" is confidential, you know.
This article is from the "practitioner" blog, so please keep this source http://bluemooder.blog.51cto.com/12822812/1958267
Python HTTPS Implementation method