【问题标题】:CGI programming with Python using WAMP and HTML使用 WAMP 和 HTML 使用 Python 进行 CGI 编程
【发布时间】:2013-04-25 13:42:11
【问题描述】:

我正在尝试用 python 连接一个 HTML 文件

这是我的 HTML 代码

<html>
<head>
</head>
<body>
<form method="POST" action="C:\wamp\www\cgi-bin\nidhi.py">
    <p>Your first name: <input type="text" name="firstname">
    <p>Your last name: <input type="text" name="lastname">
    <p>Click here to submit form: <input type="submit" value="Yeah!">
    <input type="hidden" name="session" value="1f9a2">
</form>
</body>
</html>

这里是 C:\wamp\www\cgi-bin\nidhi.py 的内容

#!/usr/local/bin/python
import cgi

def main():
    print "Content-type: text/html\n"
    form = cgi.FieldStorage()   # parse query
    if form.has_key("firstname") and form["firstname"].value != "":
        print "<h1>Hello", form["firstname"].value, "</h1>"
    else:
        print "<h1>Error! Please enter first name.</h1>"

if __name__ == '__main__':
    main()

所以是的,我正在使用 WAMP 作为我的服务器来运行它。

所以当我打开我的 HTML 文件时,我可以看到表单,但是当我按下按钮时,它所做的只是显示 python 代码。

我做错了什么?

这是 C:\wamp\bin\apache\apache2.2.22\conf 中的 httpd.conf 文件

ServerRoot "c:/wamp/bin/apache/apache2.2.22"
Listen 80
LoadModule actions_module modules/mod_actions.so
LoadModule alias_module modules/mod_alias.so
LoadModule asis_module modules/mod_asis.so
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule authn_default_module modules/mod_authn_default.so
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authz_default_module modules/mod_authz_default.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule cgi_module modules/mod_cgi.so
LoadModule dir_module modules/mod_dir.so
LoadModule env_module modules/mod_env.so
LoadModule include_module modules/mod_include.so
LoadModule isapi_module modules/mod_isapi.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module modules/mod_mime.so
LoadModule negotiation_module modules/mod_negotiation.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule php5_module "c:/wamp/bin/php/php5.3.13/php5apache2_2.dll"

<IfModule !mpm_netware_module>
<IfModule !mpm_winnt_module>
User daemon
Group daemon

</IfModule>
</IfModule>
ServerAdmin admin@localhost

ServerName localhost:80

DocumentRoot "c:/wamp/www/"

<Directory />
    Options FollowSymLinks ExecCGI
    AllowOverride None
    Order deny,allow
    Deny from all
</Directory>
<Directory "c:/wamp/www/">
    Options Indexes FollowSymLinks 
    AllowOverride None
    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1
</Directory>
<IfModule dir_module>
    DirectoryIndex index.php index.php3 index.html index.htm
</IfModule>
<FilesMatch "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy All
</FilesMatch>
ErrorLog "c:/wamp/logs/apache_error.log"
LogLevel warn

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "c:/wamp/logs/access.log" common
</IfModule>

<IfModule alias_module>
</IfModule>

<IfModule cgid_module>

</IfModule>

<Directory "C:/wamp/www/cgi-bin">
    AllowOverride None
    Options ExecCGI
    Order allow,deny
    Allow from all
</Directory>
DefaultType text/plain

<IfModule mime_module>

    TypesConfig conf/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php .php3

    AddHandler cgi-script .cgi .pl .py
</IfModule>
Include conf/extra/httpd-autoindex.conf


<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>

Include "c:/wamp/alias/*"

有人可以告诉我我做错了什么吗?我快疯了!

【问题讨论】:

    标签: python html cgi wamp


    【解决方案1】:

    尝试更改此行:

    <form method="POST" action="C:\wamp\www\cgi-bin\nidhi.py">
    

    到这里:

    <form method="POST" action="/cgi-bin/nidhi.py">
    

    您希望操作是一个 URL,它将通过您的服务器处理,而不是您的浏览器知道如何在没有服务器的情况下获取的本地文件的绝对路径。

    【讨论】:

    • 说找不到网页
    • 您是否通过如下 URL 访问 html 页面:http://localhost/mypage.html?如果是这样,请查看是否可以使用如下 URL 访问脚本:http://localhost/cgi-bin/nidhi.py。由于缺少表单值,您会收到错误,但请检查您是否可以直接访问 CGI 脚本。
    • 不,我无法直接访问它。所以我猜我没有正确配置Apache?
    • 未找到请求的 URL /wamp/www/cgi-bin/nidhi.py 在此服务器上未找到。
    • 您不应该请求该 URL。您将 DocumentRoot 设置为 C:/wamp/www。这意味着当您输入 http://localhost/whatever 时,Apache 会将其解析为文件系统上的 C:/wamp/www/whatever。所以你的脚本应该存在于类似 http://localhost/cgi-bin/nidhi.py ... 这有意义吗?
    【解决方案2】:

    python 脚本的第一行也需要像这样指向您的解释器。

    #!C:\Python27\python.exe -u
    

    虽然我从这个页面获得了信息,但我不确定 -u 的用途。

    http://wiki.python.org/moin/CgiScripts

    #!C:\Python27\python.exe -u
    
    import cgi
    
    print 'Content-Type: text/plain\n\n' #this line is compulsory to separate body from header in http response
    

    【讨论】:

    • 我在 Windows 上,所以这里的 -u 是什么?
    • -u 用于标准和错误输出的无缓冲二进制输出。但是,除非您想直接在 shell 中运行它,否则您不需要在脚本中使用它。
    • -u 只是无缓冲标准输出的开关。你可以在命令提示符下进入你的 python 安装目录并输入 python.exe /?查看所有开关。
    【解决方案3】:

    我也刚刚发现您的标题行 内容类型:文本/html\n 需要改为 内容类型:文本/html\r\n 这是windows的换行方式。

    【讨论】:

      【解决方案4】:

      我看到的主要问题是您没有配置 Python 脚本以由您的 IIS 服务器处理。

      由于未启用 Python 模块,Apache 无法理解您的 Python 代码,而只是按原样输出完整代码。

      请在您的 WAMP 服务器中使用 MOD_WSGI(不是 MOD_PYTHON)。你可以在这里了解更多:https://code.google.com/p/modwsgi/

      而且,这应该可以帮助您快速设置它:https://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide

      请参考:Installing mod_wsgi on WAMP server running on Windows 7

      【讨论】:

      • 看看这个stackoverflow.com/questions/11602653/… 我们应该把它链接到哪里?我的意思是我们用什么来代替第 4 步?
      • 我们问题的根源是我无法通过 WAMP 访问 cgi-bin 目录。错误是:Forbidden You don't have permission to access /cgi-bin/ on this server.
      • 我这几天一直在尝试安装它,但它无法正常工作。如果我启用 mod_wsgi,wamp 会崩溃。我打开了 mod_wsgi 的配置文件,第一行是 #! /bin/sh
      • 这是否意味着它与windows不兼容?
      猜你喜欢
      • 2010-11-15
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-01
      相关资源
      最近更新 更多