我正在尝试在localhost上运行它。一个简短的介绍。我在发布之前阅读过的一些链接: 如何运行 Python CGI 脚本 如何在我的 Web 服务器上运行 Python CGI 脚本? 最精确:需要帮助配置 apache 服务器以运行用 Python 编写的 CGI 脚本 以及官方http://httpd.apache.org/docs/1.3/misc/FAQ.html#CGIoutsideScriptAlias http://www.editrocket.com/文章/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 未找到
错误。什么?我已经尝试过和/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 的路径。谢谢埃弗特和大家!