I'm just learning about using cgi in html files. I've read a lot of stuff on the web, but most of it is all about the trees and not enough about the forest. I'm a top-down learner and I'm having trouble understanding what's going on with cgi, apache, and html. At the moment I'm using python as my cgi scripting language, but if you'd care to answer using perl, I can handle that. Here's a MWE for a failed test I ran.
HTML: (file is getname.html)
<html>
<head></head>
<body>
<form method="POST" action="./test.py">
<p>Your First Name: <input type="text" name="firstname">
<p>Click here to submit form: <input type="submit" value="Submit">
<input type="hidden" name="session" value="1f9a2">
</form>
</body>
</html>
Python: (file is test.py)
#!/usr/bin/python
import sys, os, os.path, shutil, string, fileinput
import cgi, cgitb
print "Content-type: text/html\n"
form = cgi.FieldStorage()
f = open('./testOutput', 'w')
if form.has_key("firstname"):
f.write(form["firstname"])
else:
f.write("Failure")
f.close()
So, it looks like what's happening is that the info the user enters into the form is put into a variable called 'firstname' (actually it's a key-value pair put into a hash-like thing in the scripting language. In python it's a dictionary). This variable is then sent as input to the cgi script (test.py), and the script is run.
But, that isn't what's happening. When I hit the 'Submit' button in the html file, the test.py script is displayed in the browser and the script isn't run. BTW, the reason that I have the script write to a file is so I can see whether it gets run. When I run test.py from the command line, it works fine.
The fact that test.py is displayed in my browser indicates, I think, that apache isn't running or is misconfigured or has permission problems. As far as I can see, none of those things is true, but I could be wrong. So here are my questions: 1) why is apache involved at all? Is it needed to send the variables gathered in the html form to the script? If so, why? I know that html is just a markup language, but I thought the 'form' tag had the ability to send a variable to a script and run the script. 2) Is there a way to test an apache configuration? Is it possible for apache to use 127.0.0.1 as the 'ServerName' (i.e. ServerName 127.0.0.1:80 in my httpd.conf file)
Basically, I just want to write cgi scripts for some webpages I'm hand-coding and test them on my home laptop. I don't seem to know how to do that and I could use some help. TIA.