在浏览器中运行python cgi脚本没有错误

时间:2012-11-27 11:40:42

标签: python browser cgi http-status-code-404

我正在尝试在 localhost 上运行它。 简短介绍。我在帖子之前阅读的一些链接: How to run Python CGI script How can I run Python CGI scripts on my web server? 最精确:Need Help to Configure apache Server to run CGI Script written in Python 还有官方http://httpd.apache.org/docs/1.3/misc/FAQ.html#CGIoutsideScriptAlias http://www.editrocket.com/articles/python_apache_windows.html 和其他...


他们建议为.cgi和.py脚本配置Apache的所有步骤: 1)

  

安装libapache2-mod-wsgi

[完成] 2)检查脚本是否可执行并可用于apache 〜$ ls -lah /var/www/cgi-bin/cgi101.py

-rwxrwxr-x 1 user user 318 2012-11-27 03:03 /var/www/cgi-bin/cgi101.py

3)编辑/ etc / apache2 / sites-available / default [更新为实际]:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
#   <Directory /var/www>
    <Directory /var/www/cgi-bin>
        Options Indexes FollowSymLinks MultiViews ExecCGI
        AllowOverride None
        Order allow,deny
        allow from all
        AddHandler cgi-script .cgi .py
#       AddHandler wsgi-script .wsgi
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

<Directory /var/www/cgi-bin>

然后重启Apache

sudo /etc/init.d/apache2 restart

最后,尝试脚本。类似于[更新为实际]:

#!/usr/bin/python3

import cgi
form = cgi.FieldStorage()
# parse form data
print('Content-type: text/html\n')
# hdr plus blank line
print('<title>Reply Page</title>')
# html reply page
if not 'user' in form:
  print('<h1>Who are you?</h1>')
else:
  print('<h1>Hello <i>%s</i>!</h1>' % cgi.escape(form['user'].value))

在输出上我得到了

  

404未找到

错误。 WTH?我试过了两个和/ var / www。我的python3的python路径:

~$ ls -lah /usr/bin/python*
...
lrwxrwxrwx 1 root root    9 2011-10-05 23:53 /usr/bin/python3 -> python3.2
lrwxrwxrwx 1 root root   11 2012-10-20 06:17 /usr/bin/python3.2 -> python3.2mu
-rwxr-xr-x 1 root root 2.8M 2012-10-20 06:17 /usr/bin/python3.2mu
...

这就是我使用

的原因
#!/usr/bin/python3

提前致谢!


[更新] /var/log/apache2/error.log

[Tue Nov 27 13:47:56 2012] [error] [client 127.0.0.1] script not found or unable to stat: /usr/lib/cgi-bin/script.py, referer: http://localhost/cgi1$

为什么它在/ usr / lib / ??

中查找script.py

[更新] 我在读行时闭上了眼睛

 ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">

它必须是/ var / www / cgi-bin中可执行文件.cgi或.py的路径。谢谢Evert和大家!

2 个答案:

答案 0 :(得分:2)

猜测一下,您的脚本位于/var/www/cgi-bin/cgi101.py

但是你的Apache配置有这个:

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

并在其中添加AddHandler指令:

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
        AddHandler cgi-script .cgi .py
    </Directory>

(并且可能会删除此{1}}目录上方的部分。)

然后我会将您的脚本放在/var/www,然后重试。

Apache tutorial似乎对此非常清楚。

答案 1 :(得分:0)

首先,您必须确保您的脚本可以由Python / OS运行。在您的情况下,您尝试使用Python3,我发现您使用旧形式的print。对于Python3,您必须使用print()作为函数。当您尝试从命令行执行脚本时,必须运行该脚本。确保它具有execute属性。

当您从命令行运行脚本时,您可以尝试通过CGI运行它。遇到问题时请检查http服务器日志。它们可能位于/var/log/http,应该有error.log。检查一下。

还要确保您的文件使用unix类型EndOfLine,即仅使用LF,而不是使用DOS / Windows中的CRLF。如果您使用CRLF,则第一行(shebang)已损坏。

此外,您必须使用空行将HTTP标头与内容分开。而不是

print("Content-Type: text/html")

使用:

print("Content-Type: text/html\n")
相关问题