Python 如何让 Flask 在端口 80 上运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20212894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:54:23  来源:igfitidea点击:

How do I get Flask to run on port 80?

pythonnetworkingflaskport

提问by quantumtremor

I have a Flask server running through port 5000, and it's fine. I can access it at http://example.com:5000

我有一个通过端口 5000 运行的 Flask 服务器,它很好。我可以在http://example.com:5000访问它

But is it possible to simply access it at http://example.com? I'm assuming that means I have to change the port from 5000 to 80. But when I try that on Flask, I get this error message when I run it.

但是是否可以简单地在http://example.com 上访问它?我假设这意味着我必须将端口从 5000 更改为 80。但是当我在 Flask 上尝试时,我在运行它时收到此错误消息。

Traceback (most recent call last):
  File "xxxxxx.py", line 31, in <module>
app.run(host="0.0.0.0", port=int("80"), debug=True)
   File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 772, in run
run_simple(host, port, self, **options)
  File "/usr/local/lib/python2.6/dist-packages/werkzeug/serving.py", line 706, in run_simple
    test_socket.bind((hostname, port))
  File "<string>", line 1, in bind
socket.error: [Errno 98] Address already in use

Running lsof -i :80returns

运行lsof -i :80返回

COMMAND   PID     USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
apache2   467     root    3u  IPv4 92108840      0t0  TCP *:www (LISTEN)
apache2  4413 www-data    3u  IPv4 92108840      0t0  TCP *:www (LISTEN)
apache2 14346 www-data    3u  IPv4 92108840      0t0  TCP *:www (LISTEN)
apache2 14570 www-data    3u  IPv4 92108840      0t0  TCP *:www (LISTEN)
apache2 14571 www-data    3u  IPv4 92108840      0t0  TCP *:www (LISTEN)
apache2 14573 www-data    3u  IPv4 92108840      0t0  TCP *:www (LISTEN)

Do I need to kill these processes first? Is that safe? Or is there another way to keep Flask running on port 5000 but have the main website domain redirect somehow?

我需要先杀死这些进程吗?那安全吗?或者是否有另一种方法可以让 Flask 在端口 5000 上运行但以某种方式重定向主网站域?

采纳答案by Ewan

So it's throwing up that error message because you have apache2running on port 80.

所以它抛出该错误消息,因为您apache2在端口 80 上运行。

If this is for development, I would just leave it as it is on port 5000.

如果这是用于开发,我会将其保留在端口 5000 上。

If it's for production either:

如果是用于生产:

Not Recommended

不建议

  • Stop apache2first;
  • apache2先停下来;

Not recommended as it states in the documentation:

不推荐,因为它在文档中说明:

You can use the builtin server during development, but you should use a full deployment option for production applications. (Do not use the builtin development server in production.)

您可以在开发期间使用内置服务器,但您应该为生产应用程序使用完整的部署选项。(不要在生产中使用内置的开发服务器。)

Recommended

受到推崇的

  • Proxy HTTPtraffic through apache2to Flask.
  • HTTP通过apache2Flask 的代理流量。

This way, apache2can handle all your static files (which it's very good at - much better than the debug server built into Flask) and act as a reverse proxy for your dynamic content, passing those requests to Flask.

这样,apache2可以处理所有静态文件(它非常擅长 - 比 Flask 内置的调试服务器要好得多)并充当动态内容的反向代理,将这些请求传递给 Flask。

Here's a linkto the official documentation about setting up Flask with Apache + mod_wsgi.

这是有关使用 Apache + mod_wsgi 设置 Flask 的官方文档的链接

Edit 1 - Clarification for @DHyman

编辑 1 - 澄清@DHyman

Proxy HTTP traffic to Flask through apache2

通过 apache2 将 HTTP 流量代理到 Flask

When a request comes to the server on port 80 (HTTP) or port 443 (HTTPS) a web server like Apache or Nginx handles the connection of the request and works out what to do with it. In our case a request received should be configured to be passed through to Flask on the WSGI protocol and handled by the Python code. This is the "dynamic" part.

当请求通过端口 80 ( HTTP) 或端口 443 ( HTTPS)到达服务器时,Apache 或 Nginx 之类的 Web 服务器会处理请求的连接并确定如何处理它。在我们的例子中,接收到的请求应该被配置为通过 WSGI 协议传递到 Flask 并由 Python 代码处理。这是“动态”部分。

reverse proxy for dynamic content

动态内容的反向代理

There are a few advantages to configuring your web server like the above;

像上面那样配置 Web 服务器有几个优点;

  • SSL Termination - The web server will be optimized to handle HTTPS requests with only a little configuration. Don't "roll your own" in Python which is probably very insecure in comparison.
  • Security - Opening a port to the internet requires careful consideration of security. Flask's development server is not designed for this and could have open bugs or security issues in comparison to a web server designed for this purpose. Note that a badly configured web server can also be insecure!
  • Static File Handling - It is possible for the builtin Flask web server to handle static files however this is not recommended; Nginx/Apache are much more efficient at handling static files like images, CSS, Javascript files and will only pass "dynamic" requests (those where the content is often read from a database or the content changes) to be handled by the Python code.
  • +more. This is bordering on scope for this question. If you want more info do some research into this area.
  • SSL 终止 - 将优化 Web 服务器以仅进行少量配置即可处理 HTTPS 请求。不要在 Python 中“推出自己的”,相比之下,这可能非常不安全。
  • 安全性 - 打开 Internet 端口需要仔细考虑安全性。Flask 的开发服务器不是为此目的而设计的,与为此目的而设计的 Web 服务器相比,它可能存在未解决的错误或安全问题。请注意,配置不当的 Web 服务器也可能不安全!
  • 静态文件处理 - 内置 Flask Web 服务器可以处理静态文件,但不推荐这样做;Nginx/Apache 在处理静态文件(如图像、CSS、Javascript 文件)方面效率更高,并且只会传递由 Python 代码处理的“动态”请求(那些内容经常从数据库读取或内容更改的请求)。
  • +更多。这接近于这个问题的范围。如果您想了解更多信息,请对该领域进行一些研究。

回答by greg

You don't need to change port number for your application, just configure your www server (nginx or apache) to proxy queries to flask port. Pay attantion on uWSGI.

您不需要更改应用程序的端口号,只需配置您的 www 服务器(nginx 或 apache)以将查询代理到 Flask 端口。注意uWSGI

回答by sebastian

Your issue is, that you have an apache webserver already running that is already using port 80. So, you can either:

您的问题是,您有一个已经在运行的 apache 网络服务器已经在使用端口 80。因此,您可以:

  1. Kill apache: You should probably do this via /etc/init.d/apache2 stop, rather than simply killing them.

  2. Deploy your flask app in your apache process, as flask in apachedescribes.

  1. 杀死 apache:您可能应该通过 来执行此操作/etc/init.d/apache2 stop,而不是简单地杀死它们。

  2. 在 apache 进程中部署您的烧瓶应用程序,如 apache 中的烧瓶所述。

回答by Amir Mofakhar

1- Stop other applications that are using port 80. 2- run application with port 80 :

1- 停止使用端口 80 的其他应用程序。 2- 使用端口 80 运行应用程序:

if __name__ == '__main__':
      app.run(host='0.0.0.0', port=80)

回答by Harun-Ur-Rashid

For externally visible server, where you don't use apache or other web server you just type

对于外部可见的服务器,您不使用 apache 或其他 Web 服务器,只需键入

flask run --host=0.0.0.0 --port=80

回答by Cathal Cronin

I had to set FLASK_RUN_PORTin my environment to the specified port number. Next time you start your app, Flask will load that environment variable with the port number you selected.

我必须FLASK_RUN_PORT在我的环境中设置为指定的端口号。下次启动应用程序时,Flask 将使用您选择的端口号加载该环境变量。

回答by muca

If you use the following to change the port or host:

如果您使用以下命令更改端口或主机:

if __name__ == '__main__':
  app.run(host='0.0.0.0', port=80)

use the following code to start the server (my main entrance for flask is app.py):

使用以下代码启动服务器(我的flask主入口是app.py):

python app.py

instead of using:

而不是使用:

flask run

回答by valentine

set the port with app.run(port=80,debug=True)you should set debug to true when on dev

设置端口,app.run(port=80,debug=True)你应该在开发时将 debug 设置为 true

回答by Alok Singh

If you want your application on same port i.e port=5000 then just in your terminal run this command:

如果您希望您的应用程序在同一个端口上,即 port=5000,那么只需在您的终端中运行以下命令:

fuser -k 5000/tcp

and then run:

然后运行:

python app.py

If you want to run on a specified port, e.g. if you want to run on port=80, in your main file just mention this:

如果您想在指定的端口上运行,例如,如果您想在 port=80 上运行,请在您的主文件中提及这一点:

if __name__ == '__main__':  
    app.run(host='0.0.0.0', port=80)

回答by Jagannath Banerjee

Easiest and Best Solution

最简单和最好的解决方案

Save your .py file in a folder. This case my folder name is test. In the command prompt run the following

将您的 .py 文件保存在一个文件夹中。在这种情况下,我的文件夹名称是 test。在命令提示符下运行以下命令

c:\test> set FLASK_APP=application.py
c:\test> set FLASK_RUN_PORT=8000
c:\test> flask run

----------------- Following will be returned ----------------

----------------- 以下将被返回----------------

 * Serving Flask app "application.py"
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:8000/ (Press CTRL+C to quit)
127.0.0.1 - - [23/Aug/2019 09:40:04] "[37mGET / HTTP/1.1[0m" 200 -
127.0.0.1 - - [23/Aug/2019 09:40:04] "[33mGET /favicon.ico HTTP/1.1[0m" 404 -

Now on your browser type: http://127.0.0.1:8000. Thanks

现在在您的浏览器上输入:http://127.0.0.1:8000。谢谢