在AWS EC2实例的端口80处启动flask应用程序时,权限被拒绝

2024-09-27 21:33:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经开发了一个基于flask的应用程序,它正在我的本地机器/网络上成功运行

我将其部署到一个AWS EC2实例。是这样开始的:

flask run --host <private ip within AWS>

但当我试图通过Postman访问实现的API时,在Postman控制台中出现以下错误:

Error: connect ECONNREFUSED <IPv4 Public IP within AWS> 

注意,这可能是因为flask应用程序通常在端口5000上运行,但是EC2将http类型的入站规则绑定到端口80

因此,我将我的烧瓶代码更改为:

if __name__ == "__main__":
    app = create_app()
    app.run(host='<private ip within AWS>', port=80)

并尝试在AWS ec2实例中这样运行:

python3 apiapp.py

但是,在尝试运行应用程序时,我得到了以下堆栈跟踪:

Traceback (most recent call last):
  File "apiapp.py", line 49, in <module>
    app.run(host='<private ip within AWS>', port=80)
  File "/home/ec2-user/AwesomeAPIs/venv/lib64/python3.7/site-packages/flask/apiapp.py", line 990, in run
    run_simple(host, port, self, **options)
  File "/home/ec2-user/AwesomeAPIs/venv/lib64/python3.7/site-packages/werkzeug/serving.py", line 1052, in run_simple
    inner()
  File "/home/ec2-user/AwesomeAPIs/venv/lib64/python3.7/site-packages/werkzeug/serving.py", line 1005, in inner
    fd=fd,
  File "/home/ec2-user/AwesomeAPIs/venv/lib64/python3.7/site-packages/werkzeug/serving.py", line 848, in make_server
    host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd
  File "/home/ec2-user/AwesomeAPIs/venv/lib64/python3.7/site-packages/werkzeug/serving.py", line 740, in __init__
    HTTPServer.__init__(self, server_address, handler)
  File "/usr/lib64/python3.7/socketserver.py", line 452, in __init__
    self.server_bind()
  File "/usr/lib64/python3.7/http/server.py", line 137, in server_bind
    socketserver.TCPServer.server_bind(self)
  File "/usr/lib64/python3.7/socketserver.py", line 466, in server_bind
    self.socket.bind(self.server_address)
PermissionError: [Errno 13] Permission denied

我错过了什么明显的东西吗


Tags: runinpyselfawsapphosthome

热门问题