3

I'm trying to connect an HTML file with python

Here's my HTML code

<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>

Here's the content of 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()

So yes I am running this using WAMP as my server.

So when I open my HTML file I can see the form but when i press the button all it does is shows me the python code.

What am I doing wrong?

Here's the httpd.conf file in C:\wamp\bin\apache\apache2.2.22\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/*"

Could someone please tell me what I'm doing wrong? I'm losing my mind!

4 Answers 4

1

The main problem I see is that you have NOT configured Python scripts to be processed by your IIS Server.

Because the Python Module is NOT enabled, Apache does NOT understand your Python code and simply outputs the fully code as it is.

Please use MOD_WSGI (not MOD_PYTHON) in your WAMP Server. You can learn more here: https://code.google.com/p/modwsgi/

And, this should help you to quickly set it up: https://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide

Refer this: Installing mod_wsgi on WAMP server running on Windows 7

Sign up to request clarification or add additional context in comments.

4 Comments

looking at this stackoverflow.com/questions/11602653/… where should we link it to? I mean what do we do in place of Step 4?
Also the root of our issue is the fact that I am not able to access the cgi-bin directory through WAMP. The error is: Forbidden You don't have permission to access /cgi-bin/ on this server.
I've been trying to install this for a few days now and it isn't working. If I enable mod_wsgi, wamp crashes. I opened the configuration file for mod_wsgi and the first line is #! /bin/sh
would this mean it would not be compatible with windows?
1

Also the top line of the python scripts needs to point to your interpreter like this.

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

Im not sure what the -u is for though I got the info from this page.

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

3 Comments

i'm on windows so what would be be the equivalent of -u here?
-u is for unbuffered binary output for standard and error outputs. However, you don't need this in your scripts unless you want to directly run it in the shell.
The -u is just a switch for unbuffered std out. You can go to your python install directory under a command prompt and type python.exe /? to see all the switches.
0

Try changing this line:

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

To this:

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

You want the action to be a URL, which will be handled through your server, rather than an absolute path to a local file that your browser knows how to get without your server.

10 Comments

it says the webpage is not found
Are you getting to the html page through a URL like this: http: //localhost/mypage.html? If so, see if you can get to the script with a URL like this: http: //localhost/cgi-bin/nidhi.py. You will get errors due to missing form values, but just check to see if you can access the CGI script directly.
nope i'm unable to access it directly. so i'm guessing I haven't configured Apache correctly?
Not Found The requested URL /wamp/www/cgi-bin/nidhi.py was not found on this server.
You should not be requesting that URL. You set DocumentRoot to C:/wamp/www. That means that when you type in http: //localhost/whatever, Apache resolves that to C:/wamp/www/whatever on your filesystem. So your script should live at something like http: //localhost/cgi-bin/nidhi.py ... does that make sense?
|
0

I also just figured out that your header line Content-type: text/html\n needs to be changed to Content-type: text/html\r\n Which is the windows way of doing a newline.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.