我正在 Python3 中处理 CGI 脚本,它识别出错误的目录
I am working on a CGI script in Python3 and it recognizes wrong directory
我正在 Python3 中处理 CGI 脚本,但它引用了错误的目录。为简单起见,以下工作
#!/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>')
但以下不是:
#!~/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>')
如果我尝试 运行 这样做,我会收到 500 内部服务器错误。后者是我实际存储我的包并从中执行操作的地方:
which python
~/anaconda3/bin/python
知道问题出在哪里吗?以及如何让 CGI 与我想要的目录一起工作?谢谢!
当您运行使用 CGI 脚本时,运行您使用的是 运行使用您的服务器的同一用户。例如,这通常是 www-data
用户。这意味着 ~
被扩展到 www-data
主目录。如果你想让 CGI 脚本 运行 成为 Python 的特定版本,你应该在 #!
.
之后放置可执行文件的完整路径
我强烈建议您不要尝试将 Apache 或 NginX 作为不同的用户运行。这可能会导致意想不到的结果和更多的困难。最好简单地使用完整路径或依赖 ENV 中已有的东西。
我正在 Python3 中处理 CGI 脚本,但它引用了错误的目录。为简单起见,以下工作
#!/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>')
但以下不是:
#!~/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>')
如果我尝试 运行 这样做,我会收到 500 内部服务器错误。后者是我实际存储我的包并从中执行操作的地方:
which python
~/anaconda3/bin/python
知道问题出在哪里吗?以及如何让 CGI 与我想要的目录一起工作?谢谢!
当您运行使用 CGI 脚本时,运行您使用的是 运行使用您的服务器的同一用户。例如,这通常是 www-data
用户。这意味着 ~
被扩展到 www-data
主目录。如果你想让 CGI 脚本 运行 成为 Python 的特定版本,你应该在 #!
.
我强烈建议您不要尝试将 Apache 或 NginX 作为不同的用户运行。这可能会导致意想不到的结果和更多的困难。最好简单地使用完整路径或依赖 ENV 中已有的东西。