gpt4 book ai didi

How to run a Flask application on port 80 while a venv is active?(如何在venv处于活动状态时在端口80上运行FlaskTM应用程序?)

转载 作者:bug小助手 更新时间:2023-10-25 13:26:44 25 4
gpt4 key购买 nike



There are multiple questions and answers about running Flask applications on port 80, but none address the issue of venv.

有许多关于在端口80上运行FlaskTM应用程序的问题和答案,但没有一个涉及venv的问题。


Typically the answer quoted is to run the application as root.

通常,引用的答案是以根用户身份运行应用程序。


This is ok as a solution as it doesn't require any special system configuration, but on the other hand isn't viable in many cases if the user doesn't have access to sudo or the root account.

这是一种不错的解决方案,因为它不需要任何特殊的系统配置,但另一方面,如果用户无法访问sudo或根帐户,则在许多情况下是不可行的。


In this particular case, although I do have access to sudo, I am running Python 3 with a virtual environment. That means that just running sudo ./my_flask_thing doesn't work. This is a Debian 12 system, so there isn't much in the way of alternatives. (eg: no system wide pip3 install flask)

在本例中,虽然我可以访问sudo,但我在虚拟环境中运行的是Python3。这意味着只运行sudo./my_flaskthing不起作用。这是一个Debian 12系统,所以没有太多的替代方案。(例如:没有系统范围的PIP3安装烧瓶)


How can I run a Flask application on port 80 in this particular case?

在这种情况下,如何在端口80上运行FlaskTM应用程序?


One solution which occured to me which doesn't require anything in the way of Python would be to run some kind of application which forwards requests to port 80 to port 5000 (the Flask default).

我想到的一个解决方案是运行某种应用程序,该应用程序将请求从端口80转发到端口5000(Flask默认为)。


I'm not sure if this would work, or how it could be done. This could perhaps be done with iptables, but I am not sure at this time.

我不确定这是否会奏效,也不知道如何才能做到。这或许可以通过iptabes来实现,但我现在还不确定。


更多回答

There is absolutely no reason a priori why "I'm running a virtual environment" would change absolutely anything whatsoever about how this works. We would need a proper minimal reproducible example in order to see an actual problem. If your Python process needs to run with a specific Python installation - whether or not that's a venv - as root, then run that Python as root. If it's a venv, then activate the venv as root. But first, make absolutely sure that you really do need that privilege. Permissions exist for a reason.

“我正在运行一个虚拟环境”绝对没有理由先验地改变它的工作原理。我们需要一个适当的、最小的可重现的例子才能看到实际的问题。如果您的Python进程需要与特定的Python安装一起运行--无论该安装是否是venv--作为根用户,那么以根用户身份运行该Python。如果是venv,则将venv作为根激活。但首先,要绝对确保你确实需要这种特权。权限的存在是有原因的。

"That means that just running sudo ./my_flask_thing doesn't work." Well, maybe that could be fixed, if we had some idea of the contents of the ./my_flask_thing script... ? Is that just the top-level Python script without an extension, or is it something that tries to activate the venv first, or just what?

“这意味着只运行sudo./my_flaskthing不起作用。”嗯,如果我们对./my_flaskthing脚本的内容有一些了解,也许这是可以修复的……?这是不是只是没有扩展的顶级Python脚本,或者它是试图首先激活venv的东西,或者只是其他什么?

"This is a Debian 12 system, so there isn't much in the way of alternatives. (eg: no system wide pip3 install flask)" - there are a million different Linuxes with comparable default restrictions, for good reason. Generally you can add Pip to the system Python with the system package manager, but generally you shouldn't as it increases the risk exposure surface for any malware that makes it on to PyPI.

“这是一个Debian12系统,所以没有太多的替代方案。(例如:没有系统范围的PIP3安装烧瓶)”--有一百万种不同的Linux有类似的默认限制,这是有充分理由的。通常,您可以使用系统包管理器将Pip添加到系统Python中,但通常不应该这样做,因为这会增加任何恶意软件进入PyPI的风险暴露表面。

Apologies; root users will ignore the virtualenv activation in terms of locating Python. But configuring PATH (and clearing out PYTHONHOME) is the only meaningful thing that activating a virtualenv actually does (the rest is cosmetic - read the activate script if you don't believe me); so just explicitly specify the path to the virtualenv's Python executable.

抱歉;根用户将忽略在定位Python方面的Virtualenv激活。但是,配置路径(并清除PYTHONHOME)是激活Virtualenv真正做的唯一有意义的事情(其余部分只是表面上的--如果您不相信我,请阅读激活脚本);所以只需显式地指定到Virtualenv的Python可执行文件的路径即可。

优秀答案推荐

One way to do it would be to install and configure a reverse proxy with nginx. Presumably nginx would have to be run with root privilages, but this would be ok as a solution.

要做到这一点,一种方法是使用nginx安装和配置反向代理。假设nginx必须以超级用户权限运行,但这作为一种解决方案是可以的。


更多回答

Gave up. Just changed the url to ask for port 5000. Much easier solution

放弃了。刚将url更改为请求端口5000。更简单的解决方案

This is an absurd circumlocution around what should be a non-problem.

这是一种荒谬的迂回说法,绕过了本应不成问题的问题。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com