How to Call a CGI Script From Another CGI
Sometimes, the best way to handle a big problem is to break it up into smaller, easier to handle chunks. This is also a good strategy to use when writing a complicated CGI or Common Gateway Interface web application. You can split a complex CGI application into a series small, easy to test, easy to debug scripts that can be executed sequentially. For example, you can program one Python CGI script to import and execute another Python script once it is finished with its slice of the overall task. Python's "import" statement will load a Python CGI script into the interpreter, compile, then execute it.
Things You'll Need
- Web browser such as Internet Explorer
- Plain-text editor like Notepad
- FTP or "File Transfer Protocol" application
- FTP access to a web server with the latest version of Python installed
Instructions
-
-
1
Click on the "Start" button. Click on "All Programs." Click the "Accessories" menu option. Click "Notepad" to launch the text editor.
-
2
Type the following code into the text editor. Alternatively, select the code with the mouse, press “Control-c,” then press “Control-v” to paste the code into the editor:
#!/usr/bin/python3.2
print('Content-type: text/html\n\n', end = '')
print ('<html><body>Hello</body></html>')import program2
-
-
3
Click the "File" menu item. Select the "Save" option. Save under the file name, "program1.py."
-
4
Click the "File" menu item. Click on the "New" menu item. Type the code below into the new text editor window:
print ('world')
-
5
Click the "File" menu item. Select the "Save" option. Save under the file name "program2.py." Click on the File menu and select "Quit" or "Exit" to close the text editor.
-
6
Click the "Start" button then click on "All Programs." Locate and launch the FTP application. Login to your web server.
-
7
Upload the files "program1.py" and program2.py" to the root directory of your web server. Click on "program1.py" on your web server. With your FTP application, change the “user,” “group” and “other” permission fields to "Execute."
-
8
Click on the “Disconnect” button of the FTP application.
-
9
Click the Windows "Start" button, then click on "All Programs." Click the "Internet Explorer" icon. Type the following URL into the browser address bar, but replace the text "yourserver.com" with your actual domain name:
http://yourserver.com/program1.py
-
10
Press the "Enter" key. The browser should display a page with the words "Hello world" at the top.
-
1
Tips & Warnings
Use the "execfile" statement if you are using Python versions prior to 3.0. While Python 3 is the latest version of the language, many web servers still run the older Python 2.x versions for compatibility reasons.
Include "Content-type" headers in your CGI scripts so that the web server knows what kind of data is coming and how to send it to the user's browser. Most CGI languages require "Content-type" headers to be sent as output before any HTML code. Otherwise, the server may send the user the actual HTML code: both the markup tags and the content between them.
References
Resources
- Photo Credit Hemera Technologies/AbleStock.com/Getty Images