继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

flask配置https详解,请查收

幕布斯7119047
关注TA
已关注
手记 432
粉丝 28
获赞 99

    今天给大家说一说flask配置https的方法,具体有以下几步:

1,flask自身配置https
    flask启动按照下面进行配置

    app.run(host='0.0.0.0',port=5000,debug=True,ssl_context=('./server.crt','./server.key'))

2,nginx配置https
    一般情况并不会由flask自己做https,总需要nginx做反向代理,进行内外网隔离。故可以在nginx中增加配置

server {

  listen 443 ssl;

client_max_body_size 100M;

#mycom

server_name www.dzmsoft.com ;

charset utf-8; 

ssl on;

#ssl_certificate /application/nginx/nginx/conf/eds_ca/server.crt;

#ssl_certificate_key /application/nginx/nginx/conf/eds_ca/server.key;

ssl_certificate /application/nginx/nginx/conf/mycert.pem;

ssl_certificate_key /application/nginx/nginx/conf/server.key;

ssl_session_cache shared:SSL:10m;

ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;

ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

ssl_prefer_server_ciphers on;

index index.jsp default.jsp index.do default.do index.html index.htm index.php forum.php;

access_log  logs/dzmsoft_access.log main;

location / {

proxy_pass http://dzmsoft_p;

}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico|svg|flv|xml)(.*)$

{

expires 15d;

proxy_pass http://dzmsoft_p;

}

location ~ .*\.(js|css|gzcss|gzjs)(.*)$

{

expires 1d;

proxy_pass http://dzmsoft_p;

}

location /(WEB-INF)/ {

deny all;

}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico|svg|flv|js|css|gzcss|gzjs)?$

{

if (-f $request_filename) {

expires 1d;

break;

}

}

error_page   500 502 503 504  /50x.html;

location = /50x.html {

        root   html;

    }

}

    这时flask的启动,应该是

app.run(host='0.0.0.0',port=5000,debug=True)

    这里有一点需要注意,nginx配置了https了,那么flask是可以不用配置的https了。
    另外下面的配置,也代表了nginx转发请求到flask的web服务是http的,

location / {

proxy_pass http://dzmsoft_p;

}

    如果proxy_pass https://dzmsoft_p;,那么就要求flask也应该是https的,否则接口协议转换就会出现异常,提示HTTP/0.9的问题,以及nginx出现502的问题,和flask接收到乱码。因为nginx负责转发,而且nginx配置了https,那么nginx已经做了协议转换,就不需要flask再多次一举,故这里配置http即可。

3,python调用https请求示例
    重点是verify=False配置。

import requests

import json

requests.packages.urllib3.disable_warnings()

edata ={

    "code" : str(4201),

    'paper': str(1)

}

r = requests.post('https://www.dzmsoft.com/api/edata',params = edata, verify=False)

try:

    dic_source = json.loads(r.text)

    # print(len(dic_source['_source']))

    print("总共查询到{}条数据".format(dic_source['total']))

except:

    print(r.text)

    以上便是flask配置https详解,请查收的全部内容,更多内容干货可关注慕课网~

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP