3

HTMLファイルをPythonで接続しようとしています

ここに私の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/*"

誰かが私が間違っていることを教えてもらえますか? 気がおかしくなりそうだ!

4

4 に答える 4

1

主な問題は、IIS サーバーによって処理されるように Python スクリプトが構成されていないことです。

Python モジュールが有効になっていないため、Apache は Python コードを理解せず、完全なコードをそのまま出力します。

WAMP サーバーでは (MOD_PYTHON ではなく) MOD_WSGI を使用してください。詳細については、 https : //code.google.com/p/modwsgi/ をご覧ください。

そして、これはあなたがそれを素早く設定するのに役立つはずです: https://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide

これを参照してください: Windows 7 で実行されている WAMP サーバーに mod_wsgi をインストールする

于 2013-05-02T06:38:02.810 に答える
1

また、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
于 2013-05-01T15:15:47.037 に答える
0

この行を変更してみてください:

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

これに:

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

サーバーなしで取得する方法をブラウザーが認識しているローカル ファイルへの絶対パスではなく、アクションをサーバー経由で処理される URL にする必要があります。

于 2013-05-01T14:45:53.740 に答える
0

また、ヘッダー行の Content-type: text/html\n を Content-type: text/html\r\n に変更する必要があることもわかりました。これは、改行を行う Windows の方法です。

于 2013-05-01T15:47:44.570 に答える