lost and found ( for me ? )

Apache CGI, python script

small tips.
# tail -1 /etc/lsb-release ;uname -ri
3.2.0-58-virtual x86_64

install apache2
# apt-get install apache2

edit /etc/apache2/mods-available/mime.conf to allow us to execute cgi, pl, rb, py scripts.
AddHandler cgi-script .cgi .pl .rb .py

# less /etc/apache2/sites-available/default
     ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
       <Directory "/usr/lib/cgi-bin">
               AllowOverride None
               Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
               Order allow,deny
               Allow from all

start httpd
# apachectl restart

create a python script for the testing and copy it to /usr/lib/cgi-bin directory.
also change permission of python file.
# cat example1.py
#!/usr/bin/env python

print "Content-type: text/html"
print "<html>"
print "<center>Hello, Linux.com!</center>"
print "</html>"

# cp example1.py /usr/lib/cgi-bin/
# chmod 755 /usr/lib/cgi-bin/example1.py

access to http:// IP /cgi-bin/example1.py


[ FORM ]


html file ( form )
# cat /var/www/example2.html
<title>Python CGI Test(1)</title>

<h1>Python CGI Test(1)</h1><hr><p>

<form name = "Form1" method="POST" action="/cgi-bin/example2.py">
name: <input type="text" size=30 name="name"><p>
addr: <input type="text" size=30 name="addr"><p>
<input type="submit" value="submit" name="button1"><p>

python script
# cat /var/www/cgi-bin/example2.py
#!/usr/bin/env python

# CGI Test
import cgi

print "Content-Type: text/html\n\n"

print "<html><body>"

form = cgi.FieldStorage()
form_ok = 0
if form.has_key("name") and form.has_key("addr") :
 form_ok = 1
if form_ok == 0 :
 print "<h1>ERROR</h1>"
else :
 print "<h2>Result</h2><hr><p>"
 print "<p><b>name: </b>", form["name"].value
 print "<p><b>addr: </b>", form["addr"].value

print "</body></html>"

copy the script to /vaw/www/cgi-bin directory.
# mkdir /var/www/cgi-bin   <- create a directory if there is not
# cp example2.py /var/www/cgi-bin/
# chmod 755 /var/www/cgi-bin/example2.py

access to http:// IP /example2.html

enter “submit”

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.