当我们需要使用 sudo python xxx.py 或只是 python xxx.py 或 xxx.py
我写了一个网站,让我困惑的是当我运行该网站时,首先我需要启动应用程序, 所以有3种方法:
- sudo python xxx.py
- python xxx.py
- xxx.py
我不清楚如何使用它们,目前我的电脑中的NO.3方法效果不佳
I have write a website,what confused me is when i run the website,first i need start the the app,
so there are 3 ways:
- sudo python xxx.py
- python xxx.py
- xxx.py
I didn't clear with how to use each of them,the NO.3 method currently in my computer dosen't work well
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
sudo
将以超级用户权限运行应用程序。考虑到您指的是一个网站,这当然不是您想要做的。 (对于一个 web 应用程序,如果它需要超级用户权限,它就被破坏了。这对于考虑实际使用来说实在是太大了。)在其他情况下,你可能有一个 python 程序,它会进行某种系统维护并需要运行作为根。在这种情况下,您可以使用
sudo
,但您永远都不想对可公开访问且可能被利用的内容执行此操作。事实上,对于测试以外的任何事情,您可能应该以具有非常有限的访问权限的单独用户身份运行 Web 应用程序(例如,将其 shell 设置为/dev/null
,没有对任何内容的读取或写入访问权限)他们不需要,等等...)。其他两个实际上是相同的(就它们的作用而言),但最后一个选项(直接执行脚本)将需要:
unix-y 系统)(例如
chmod +xwhatever.py
)#!
) 指向/usr/bin/python
你想要的 python 可执行文件
运行东西(同样,这只适用于unix-y系统)
调用python来运行代码(
pythonwhatever.py
)并按照上面的步骤操作(生成一个可以直接调用的脚本)whatever.py
)做完全相同的事情(假设 python 文件中的 shebang 指向与“
python
”相同的 python 可执行文件,无论如何......)sudo
will run the application with superuser permissions. Considering that you're referring to a website, this is certainly not what you want to do. (For a webapp, if it requires superuser permissions, it's broken. That's far, far too big of a security risk to consider actually using.)Under other circumstances you might have a python program that does some sort of system maintaince and requires being run as root. In this case, you'd use
sudo
, but you would never want to do this for something that's publicly accessible and could potentially be exploited. In fact, for anything other than testing, you should probably run the webapp as a separate user with very limited access (e.g. with their shell set to/dev/null
, no read or write access to anything that they don't need, etc...).The other two are effectively identical (in therms of what they do), but the last option (executing the script directly) will require:
unix-y systems) (e.g.
chmod +x whatever.py
)#!
) pointing to the/usr/bin/python
python execuctable that you want to
run things with (again, this only applies to unix-y systems)
Calling python to run the code (
python whatever.py
) and following the steps above (resulting in a script that you can call directly withwhatever.py
)do exactly the same thing (assuming that the shebang in the python file points to the same python executable as "
python
" does, anyway...)