Https with Http in Flask Python

前端 未结 4 754
南方客
南方客 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:34

    I've ran into trouble with this also. I originally had:

        if sys.argv[1] == 'https' or sys.argv[1] == 'Https':
            app.run(host="0.0.0.0", port=12100, ssl_context='adhoc')
        elif sys.argv[1] == 'http' or sys.argv[1] == 'HTTP':
            app.run(host="0.0.0.0", port=12100)
    

    which only allowed either http or https at a time and not both.

    So I used multi-threading to make both work at the same time. I put each app.run in it's own function and called an independent thread on each one of them.

    import threading
    import time as t
    
    ...
    
    def runHttps():
        app.run(host="0.0.0.0", port=12101, ssl_context='adhoc')
    
    def runHttp():
        app.run(host="0.0.0.0", port=12100)
    
    if __name__ == '__main__':
        # register_views(app)
        CORS(app, resources={r"*": {"origins": "*"}}, supports_credentials=True)
        if sys.argv[1] == 'https' or sys.argv[1] == 'Https' or sys.argv[1] == 'http' or sys.argv[1] == 'Http':
            print("here")
            y = threading.Thread(target=runHttp)
            x = threading.Thread(target=runHttps)
            print("before running runHttps and runHttp")
            y.start()
            t.sleep(0.5)
            x.start()
    

    And that's how I made http and https work at the same time.

提交回复
热议问题