无法在 aws cloud9 c9 python flask 中预览 'Hello World' 应用程序

Can't preview 'Hello World' application in aws cloud9 c9 python flask

我无法使用 AWS Cloud9 (c9) 预览此应用程序 python flask:

from flask import Flask
import os
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

host = 'localhost' # '0.0.0.0' #"127.0.0.1" # I tried all of these ip's

if __name__ == '__main__':
    app.debug = True
    port = int(os.environ.get("PORT", 8080)) # I also tried port was 5000
    app.run(host=host, port=port)

# This is the error I got 52.15.73.96 The connection has timed out
#The server at 52.15.73.96 is taking too long to respond.

这类似于AWS cloud9 timeout when running flask application

您需要 运行 使用端口 8080(或其他可用的 C9 端口)在 0.0.0.0 中的服务器。

将您的 app.run() 命令更改为如下内容:

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

如果 8080 不行,试试 80.

答案如下:您必须通过 AWS 防火墙。 你必须

  1. 进入 EC2(来自所有 AWS 服务的列表)
  2. 单击安全组
  3. 单击您的 Cloud9 实例
  4. 点击入站
  5. 单击编辑
  6. 点击添加规则
  7. 添加这条规则:

    • 对于类型,选择自定义 TCP 规则。 - 所有流量也有效。
    • 对于端口范围,键入 8080、8081 或 8082。- 如果您输入 'All Traffic',这将默认为所有端口。
    • 对于 Source,选择 Anywhere,类似于 0.0.0.0/0

    这是一个屏幕截图link:https://imgur.com/a/zhQbA

A​​WS 在他们的 C9 文档中确实有这个。 https://docs.aws.amazon.com/cloud9/latest/user-guide/app-preview.html#app-preview-share-security-group 在通过 Internet 共享 运行 应用程序中,第 2 步:为实例设置安全组

flask run --host=127.0.0.1 --port=8080