简单的 CGI python 网络服务器 + php 不工作

发布于 2024-11-24 02:23:26 字数 861 浏览 0 评论 0原文

现在其他地方也提出了类似的问题。但是,我已经尝试了那里建议的解决方案,但没有帮助。

我正在运行以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

司马昭之心 2024-12-01 02:23:27

#!/usr/bin/php 不会使您的脚本作为 CGI 运行;它使其作为命令行脚本运行。如果您有 php-cgi 二进制文件,请使用它;否则,您可能必须在脚本中添加一些令人讨厌的内容,例如:

#!/usr/bin/php
Content-Type: text/html

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?> 
</body>
</html>

#!/usr/bin/php doesn't make your script run as a CGI; it makes it run as a command-line script. If you have a php-cgi binary sitting around, use that instead; otherwise, you may have to hack something nasty into the script like:

#!/usr/bin/php
Content-Type: text/html

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?> 
</body>
</html>
看海 2024-12-01 02:23:27

使用:
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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文