Adding HTML to Web Pages Using CGI Scripts


#!/usr/bin/python print "Content-type: text/html\n" print "<title>CGI Text</title>\n" webText = """ <H1>Useful Python Links</H1> . . . """ print webText

Adding HTML content to web pages using Python CGI scripts is a very straightforward and simple process. The first line of the CGI script should be nonexecutable and point to the location of the Python interpreter using the #!<path> syntax.

When the CGI script is called by the web server, all output to stdout is directed back to the web browser. All you need to do to send the HTML code to the browser is print it to stdout.

Note

The permission on the CGI scripts must be executable. You will need to set the file permission to 755 on Linux servers for the scripts to be able to execute.


Note

Scripts that are created with the DOS EOL character set \r\n will not run properly on Linux web servers. Depending on the web server you are using, you might need to make configuration changes to understand how to serve CGI files.


#!/usr/bin/python #Send header to browser print "Content-type: text/html\n" print "<title>CGI Text</title>\n" webText = """ <H1>Useful Python Links</H1> <li><a href="http://www.python.org"> Python Web Site</a></li> <li><a href="http://docs.python.org"> Python Documentation</a></li> <li><a href="http://cheeseshop.python.org"> Cheeseshop (Python Packages Library)</a></li> """ #Send page content to browser print webText


cgi_text.cgi

<!DOCTYPE html> <html lang="en" xml:lang="en"> <head> <meta content="text/html; charset=utf-8"  http-equiv="content-type" /> <title>Form Page</title> </head> <body> <H1>Test Link to CGI Script</H1> <A HREF="cgi_text.cgi">cgi_text.cgi</A></body> </html>


cgi_link.html

Figure 10.1 shows how cgi_text.cgi appears in a web browser.

Figure 10.1. Output HTML page created by cgi_text.cgi code.




Python Phrasebook(c) Essential Code and Commands
Python Phrasebook
ISBN: 0672329107
EAN: 2147483647
Year: N/A
Pages: 138
Authors: Brad Dayley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net