Https with Http in Flask Python

前端 未结 4 780
南方客
南方客 2021-02-04 06:25

I have a client server application. I managed to make them connect over https using SSl encryption using this

    context = SSL.Context(SSL.SSLv3_METHOD)
    con         


        
4条回答
  •  你的背包
    2021-02-04 06:41

    Now i want to run the server using both http and https is there any possible way to do that ??

    I have had a similar problem recently. To test whether a proxy is used after http is redirected to https, I've just started two processes on different ports: one for http, another for https:

    #!/usr/bin/env python3
    """Serve both http and https. Redirect http to https."""
    from flask import Flask, abort, redirect, request # $ pip install flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def index():
        if request.url.startswith('http://'):
            return redirect(request.url.replace('http', 'https', 1)
                            .replace('080', '443', 1))
        elif request.url.startswith('https://'):
            return 'Hello HTTPS World!'
        abort(500)
    
    def https_app(**kwargs):
        import ssl
        context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
        context.load_cert_chain('server.crt', 'server.key')
        app.run(ssl_context=context, **kwargs)
    
    
    if __name__ == "__main__":
        from multiprocessing import Process
    
        kwargs = dict(host='localhost')
        Process(target=https_app, kwargs=dict(kwargs, port=7443),
                daemon=True).start()
        app.run(port=7080, **kwargs)
    

    Needless to say, it is only for testing/debugging purposes.

提交回复
热议问题