以root身份运行twistd,找不到模块
我有一个用 Twisted 编写的简单 Web 服务器,我正在尝试使用 twistd 来启动它。使用reactor.run()一切正常,但是当我使用twistd -y(作为root)时,没有找到位于直接子目录中的包。我以root身份运行twistd,因为服务器在端口80上运行。 twistd 的手册页 说:
请注意,如果 twind 以 root 身份运行,则不会在工作目录中搜索 Python 模块。
嗯,这很好,但是为什么呢?我该如何解决?即使我明确设置了该选项,twistd 似乎也会忽略 --rundir .
。
I have a simple web server written in Twisted, and I'm trying to start it up daemonized with twistd. Everything works fine with reactor.run()
but when I use twistd -y
(as root), none of my packages which are in direct child directories get found. I'm running twistd as root, since the server runs on port 80. The manpage for twistd says:
Note that if twistd is run as root, the working directory is not searched for Python modules.
Well that's great but why? And how can I work around? twistd seems to be ignoring --rundir .
even if I set that option explicitly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般的 UNIX 智慧是,在工作目录中搜索要在 root 时执行的内容是一个坏主意。有人认为它为特洛伊木马打开了大门。在以 root 身份运行时,twistd 并没有特意将工作目录添加到 Python 模块导入搜索路径中,twistd 基本上是在尝试遵循这一智慧。
正如另一位评论者所说,您可以自己显式设置 PYTHONPATH 以包含包含应用程序所需代码的目录。
您还可以完全跳过以 root 身份运行,并使用 authbind 来绑定低编号端口,而无需超级用户权限。这就是我所有服务器的部署方式。
General UNIX wisdom is that searching the working directory for things to execute when root is a bad idea. The argument goes that it opens the door to trojans. In not going out of its way to add the working directory to the Python module import search path when running as root, twistd is basically trying to follow this wisdom.
As another commenter said, you can explicitly set PYTHONPATH yourself to include the directories which contain the code your app needs.
You can also skip running as root entirely and use
authbind
to bind low-numbered ports without having superuser privileges. This is how all of my servers are deployed.