【问题标题】:cgi script doesn't render properly in browsercgi 脚本无法在浏览器中正确呈现
【发布时间】:2015-06-14 01:41:25
【问题描述】:

我是 python web 开发的初学者。因此,我迅速完成了在我的 ubuntu 14.04 操作系统中运行 cgi 脚本的所有设置。我将hello.py 保留在/var/www/ 中,但是当我在浏览器中运行它时,它看起来不像预期的那样。

这是来自/etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/

        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
                AddHandler mod_python .py
                PythonHandler mod_python.publisher
                PythonDebug On
        </Directory>


       <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
                AddHandler mod_python .psp
                PythonHandler mod_python.psp
                PythonDebug On
        </Directory>



    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

以下是启用的模组:

Hello.py 文件:

#!/usr/bin/python

print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print '</body>'
print '</html>'

【问题讨论】:

    标签: python apache localhost cgi


    【解决方案1】:

    正如@Skywrath 所指出的,您确实需要在任何内容之前打印标题。除此之外,在 /var/www/ 的 .htaccess 文件中为 python 文件添加一个 CGI 处理程序,如下所示:

    AddHandler cgi-script .cgi .py
    Options +ExecCGI 
    

    【讨论】:

    • 除了,这通常被认为是不好的做法,并且建议在您的文档目录中包含可执行脚本...至少在您真正了解自己是什么之前正在做。我将开始使用脚本目录,而不是(即,首先参见 Apache: Tutorial: Dynamic Content with CGI
    • @RVT 如果是我,我会使用 Python Web 框架(OP,如果您有兴趣,请尝试 Pyramid)而不仅仅是原始 CGI。我只是在解决 OP 的问题。
    • @DarthVader 我在 var/www/ 中没有任何 .htaccess 文件,我应该创建一个吗?该文件的名称和权限应该是什么?
    • 很公平,@Darth...不过,框架就是它们,我目前对 python 的迷恋(除了更强大的django)是Flask 微框架。跨度>
    • @androidplusios.design 是的,你需要创建.htaccess文件,默认不存在。阅读更多here
    【解决方案2】:

    您需要通过在 HTML 代码顶部添加以下行来表明此页面是纯 HTML 文件,甚至在 .例如:

     print 'Content-type: text/html'
     print #An empty line is needed between the previous line and your HTML code
     print '<html>'
     ...
     print '</html>'
    

    【讨论】:

    • 没有效果,现在我看到 #!/usr/bin/python print 'Content-type: text/html' print '' print '' print '' print '' print '' print ' in浏览器
    • 再一次,这是一个 CGI / 处理程序问题...不是脚本问题。 (您应该能够通过在浏览器中执行“查看源代码”轻松地证明这一点 - 它应该只是您脚本的副本)
    【解决方案3】:

    您应该着眼于将脚本移动到您的 CGI 目录,而不是您的文档根目录,并使用 ScriptAlias 在您的 Apache 配置文件中指定此类。

    然后,您可能会改为通过 localhost/cgi-bin/hello.py 访问脚本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多