我也遇到了这个问题。我原来有:
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)
一次只允许 http 或 https ,而不是两者。
所以我使用多线程让两者同时工作。我将每个 app.run 放在它自己的函数中,并在每个函数上调用一个独立的线程。
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()
这就是我让 http 和 https 同时工作的方式。