Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)

<form method="POST" action="./">
  <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">

Python: (file is

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"):

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 (, and the script is run.

But, that isn't what's happening. When I hit the 'Submit' button in the html file, the 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 from the command line, it works fine.

The fact that 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 as the 'ServerName' (i.e. ServerName 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.

share|improve this question
up vote 3 down vote accepted

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

CGI is a form of server-side scripting, a CGI script runs on the web server, not on the web client. There is also client-side scripting. These days that usually takes the form of Javascript embedded in script tags in HTML documents. This is unrelated to CGI.

At root, your web browser really only has two jobs: requesting data from other computers via a network protocol called HTTP, and rendering the data it gets back in some pretty fashion on your screen. The only thing your browser knows about your web form is that is that when you press the submit button, it should send a request to some Internet address with the name of the script and the data from the form. The browser doesn't know anything about Python, or where in the file system the document '' lives. It doesn't even know that '' is a script.

Another piece of software has to be running to receive the browser's requests, look up the data it asks for, and send it back to the browser. That other piece of software is the web server, in this case Apache. Apache has to be configured so that when it gets the request for '" from the browser, it knows where is in the file system, that when it sees a request for a file ending in ".py" it shouldn't just transmit the contents of the file back to the browser, but rather that is should start a copy of the python interpreter using that script, pass it the form data that came with the request, and then send the output of the script back to the browser.

One issue that may be making this more confusing for you: I ove-simplified when I said your browser really only has two jobs. As a convenience, most browsers can also act as local file viewers, that is, you can do an "File Open" on a local file, and the browser will render that file, without having requested it from a web server. The browser still won't know anything about running scripts unless they're embedded in a script tag as part of an HTML document.

share|improve this answer
thanks for that top-down view. That's what I've been looking for. Your description is clear and comprehensible. So, here's a question based on your answer. My 'form' (getname.html) is a local file. So I get to it by the method you indicate. So when I hit 'submit', it sends to "some internet address" the data and the script name. This address is...what? since the internet is not involved. Does it go to – bev Feb 29 '12 at 4:29
The trick here is that your form specifies a relative address: "./". That effectively says: go to same place where you got this page (the one containing the form), and ask for a file in the same directory, but called ''. Since you got the form file from the local file system, it skips the HTTP protocol, and also gets '' from the local file system. If you look closely at the address field in your browser you should see that the address for your form starts with "file://". If you go to some site actually on the web you'll see it starts with "http://". – Charles E. Grant Feb 29 '12 at 4:51
Charles, Upon further reflection, here are more questions: 1) I would gather that the 'return' Internet address is part of the info sent to your browser. I would imagine that opening a local file will substitute 'localhost' for the 'return' address. Is this so? Is it more problematical if the server and client are the same machine? 2) If apache is running on localhost, it will know what to do with info sent from a local file (if it is configured properly). – bev Feb 29 '12 at 4:52
3) Apache needs to know: where scripts are (set with ScriptAlias), that .py is a script file (set with AddHandler - thx James Anderson), what to do with with python files (I don't have mod_python yet, but I assume it doesn't need it to run my one script), not to mention the other variables that are set in the httpd.conf file. – bev Feb 29 '12 at 4:53
Charles, it may help to give you some background on me, since I'm not typical for this site. I'm an electrical engineer who has never been trained in CS. On my own, over the last 20 years I've taught myself C,C++,perl,python,XHTML,css,javascript,and lisp. But people like me have large and random gaps in what we know because of the way we learn. So I'm not a novice, and usually I know what to ask (not always). – bev Feb 29 '12 at 4:58

You need to tell apache that files ending in ".py" are scripts and not just plain text files.

To do this you need to modify the "httpd.conf" file the line in question probaly looks like:

AddHandler cgi-script .cgi .pl .asp

You need to add the '.py' python extension to this line:

AddHandler cgi-script .cgi .pl .asp .py

There may be more to this depending on which distribution of Apache you installed full details are here:- How To

If you intend to develop this further I would recommend you install apache's "mod_python" which will run your scripts inside apache and keep a python interpreter ready and waiting for new requests, rather, than firing up a python script in separate process for every request.

share|improve this answer
James - thanks. I found your answer to be very helpful. – bev Feb 29 '12 at 6:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.