-2

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.

bev
  • 407
  • 4
  • 10

2 Answers2

3

"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 'test.py' lives. It doesn't even know that 'test.py' 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 'test.py" from the browser, it knows where test.py 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.

Charles E. Grant
  • 16,612
  • 1
  • 46
  • 73
  • 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 127.0.0.1? – bev Feb 29 '12 at 04:29
  • The trick here is that your form specifies a relative address: "./test.py". 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 'test.py'. Since you got the form file from the local file system, it skips the HTTP protocol, and also gets 'test.py' 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 04: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 04: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 04: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 04:58
  • When you are using your web browser to look at local files through "File, Open", all the stuff about TCP/IP and HTTP and making requests from servers goes right out the window. It's just acting like any other application opening files on the disk. You can use a web browser in this mode even if you don't have any network installed. Now, you could install a web server on your computer and access local files via HTTP from the web server. In that case you could certainly use 127.0.0.1 as the server address, and the address would look like 'http: //127.0.0.1/getname.html" – Charles E. Grant Feb 29 '12 at 05:10
  • Charles - it sounds like what you're telling me is that in order to write and test cgi scripts on my laptop I'll need to follow the procedure you outline above (i.e. don't use 'Open File', but use http: //127....). Is that so? – bev Feb 29 '12 at 05:55
  • Yes, and you'll have to install and configure a local copy of Apache, IIS, or some other web server. – Charles E. Grant Feb 29 '12 at 06:12
  • Ah, well, that's incredibly...ah.. badly conceived. Well we'll have to make do. Thanks for all your help. – bev Feb 29 '12 at 06:54
  • Well, bear in mind this wasn't invented to make it easy to run scripts on your local computer. It was invented to enable the sharing of loosely specified data across a world-wide network of computers, most of which can't be trusted. It's been rather successful at that. – Charles E. Grant Feb 29 '12 at 08:20
2

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.

James Anderson
  • 18,049
  • 1
  • 42
  • 72