简体   繁体   English

我正在Python3中处理CGI脚本,它可以识别错误的目录

[英]I am working on a CGI script in Python3 and it recognizes wrong directory

I am working on a CGI script in Python3, but it is referencing the wrong directory. 我正在Python3中处理CGI脚本,但是它引用了错误的目录。 For simplicity, the following works 为简单起见,以下作品

#!/home/linuxbrew/.linuxbrew/bin/python3

print ("Content-type:text/html")
print ()                             
print ('<html>')
print ('<head>')
print ('</head>')
print ('<body>')
print ('<h2>Hello Word!</h2>')
print ('</body>')
print ('</html>')

But the following does not: 但是以下内容却没有:

#!~/anaconda3/bin/python

print ("Content-type:text/html")
print ()                             
print ('<html>')
print ('<head>')
print ('</head>')
print ('<body>')
print ('<h2>Hello Word!</h2>')
print ('</body>')
print ('</html>')

If I try to run this I get a 500 Internal Server Error. 如果我尝试运行此命令,则会收到500 Internal Server Error。 The latter is where I actually have my packages stored and execute things from however: 后者实际上是我存储程序包并从中执行内容的地方:

which python
~/anaconda3/bin/python

Any idea what the problem is? 知道是什么问题吗? And how I can get CGI to work with the directory I want it to? 以及如何使CGI与我想要的目录一起使用? thanks! 谢谢!

When you're running a CGI script, you're running it by the same user who is running your server. 运行CGI脚本时,您是由运行服务器的同一用户运行它的。 This will often be, for example, the www-data user. 例如,这通常是www-data用户。 This means that ~ is expanded to the www-data home directory. 这意味着~被扩展到www-data主目录。 If you want to have the CGI script run a specific version of Python, you should put the entire path to the executable after the #! 如果要让CGI脚本运行特定版本的Python,则应将整个路径放在可执行文件的#!之后#! .

I highly recommend that you NOT try to get Apache or NginX to run as a different user. 我强烈建议您不要尝试让Apache或NginX以其他用户身份运行。 That can cause unexpected results and more difficulties down the road. 这可能会导致意外的结果,并给以后带来更多困难。 It is better to simply use the full path or rely on something already in the ENV. 最好只使用完整路径或依靠ENV中已有的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM