简单的 CGI python 网络服务器 + php 不工作
现在其他地方也提出了类似的问题。但是,我已经尝试了那里建议的解决方案,但没有帮助。
我正在运行以下 python CGI Web 服务器:
#!/usr/bin/python
from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler
serve = HTTPServer(("",80),CGIHTTPRequestHandler)
serve.serve_forever()
在“cgi-bin”目录中,存储了以下 php 文件“simple.php”:
#!/usr/bin/php
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>
该文件已被授予可执行权限,并且可以从服务器上的命令行正确执行。
现在,如果我启动服务器并尝试使用“http://server/cgi-bin/simple.php”访问该页面,我只会得到一个空白页面,并且在服务器标准输出上看不到任何错误。
我查看了 CGIHTTPServer.py 的代码,我认为当分叉进程来执行 cgi-bin 并使用 dup2 操作文件描述符(stdin、stdout)时,可能会出现问题。但是,我不知道如何让它发挥作用。这个简单的事情让我困惑了几个小时,非常感谢任何形式的帮助。
服务器机器是ubuntu 9.04机器。
Now similar question(s) has been asked at other places. However, I have tried the suggested solution(s) there and it did not help.
I am running following python CGI webserver:
#!/usr/bin/python
from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler
serve = HTTPServer(("",80),CGIHTTPRequestHandler)
serve.serve_forever()
In the "cgi-bin" directory, following php file "simple.php" is stored:
#!/usr/bin/php
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>
This file has been given executable permission and executes properly from just the commandline on the server.
Now if I start the server and try to access the page using "http://server/cgi-bin/simple.php", I get just a blank page and I don't see any error on the server stdout.
I looked through the code for CGIHTTPServer.py and I think the problem might be occurring when a process is forked to execute the cgi-bin and the file descriptors (stdin,stdout) are manipulated using dup2. However, I don't know how to get it working. This simple thing has been puzzling me for a few hours and would really appreciate any kind of help.
The server machine is a ubuntu 9.04 box.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
#!/usr/bin/php
不会使您的脚本作为 CGI 运行;它使其作为命令行脚本运行。如果您有 php-cgi 二进制文件,请使用它;否则,您可能必须在脚本中添加一些令人讨厌的内容,例如:#!/usr/bin/php
doesn't make your script run as a CGI; it makes it run as a command-line script. If you have aphp-cgi
binary sitting around, use that instead; otherwise, you may have to hack something nasty into the script like:使用:
handler.cgi_directories.append('/')
否则,只有当您的 cgi 脚本是 '/cgi-bin' 或 '/htbin' 文件夹时才会执行,如所述 此处。基本上,cgi_directories 意味着从该文件夹和所有子文件夹执行cgi。希望有帮助
USE:
handler.cgi_directories.append('/')
Otherwise your cgi scripts will be executed ONLY if they are '/cgi-bin' or '/htbin' folders as described Here. Basically cgi_directories means to execute cgi from that folder and all subfolders. Hope that helped