The purpose is to get the reader started as quickly as
possible, since we
beleive strongly that the way to learn a new language
is to write programs
in it. The tutorial does assume a working knowledge
of the basic elements
of programming; there is no explanation of computers,
of compilation, nor
of the meaning of an expression like
n = n + 1.
Although we have tried where
possible to show useful programming techniques,
the book is not intended
to be a reference work on data structures and
algorithms; when forced to
make a choice, we have concentrated on the language.
The quote above is from the mother of all programming books: The C Programming Language [KR88] by Kernighan and Ritchie. I can only hope to emulate the spirit here. Nothing more. Kernighan and Ritchie are inimitable,
Python is a server side interpreted programming language. Its name pays hommage to Monty Python. Interpreted means that it is translated into computer code when the page is requestd by some user. Server side means that it runs invoked by a web server calling the python interpreter to execute the code. After the interpretation, the output of that process is sent to the requesting program, normally a browser.
As an introduction here is an outline of the World Wide Web architecture. It places the course disciplines in a wider context. The aim is to get a high altitude perspective of the setting for your work in this course.
Python as a backend language? Yes, if PHP can do it, if perl can do it, Python can do it. So if your web server is set up to handle scripts via the CGI, Common Gateway Interface, then indeed, why not?
So here follows the briefest of intros to that.
#!/usr/bin/python import cgitb # trackback manager, included cgitb.enable() # and enabled for debugging purposes htmlString = '''<!doctype html> <html> <head> <title>First Python CGI</title> <meta charset='utf-8'/> </head> <body> <h1>Hello World!</h1> <h2>from</h2> <p>Python</p> <p><a href='./htmlform.py'>Page with form.</a></p> </body> </html>''' # triple quoting allows multiline strings print('Content-type: text/html') # http-header print() # header must end with empty line print(htmlString) # http body
#!/usr/bin/python import cgitb cgitb.enable() htmlTopString = '''<!doctype html> <html> <head> <title>First Python CGI</title> <meta charset='utf-8'/> </head> <body> <main>''' htmlBotString = ''' </main> </body> </html>''' htmlNeck = ''' <header> <h1>Form Generated by Python</h1> <h2>Fill In and Submit</h2> </header>''' htmlForm = ''' <form action='./htmlformdata.py?abc=test' method='post'> <table> <tr> <td>First Name:</td> <td><input type='text' name='first_name'/></td> <td></td> </tr> <tr> <td>Last Name:</td> <td><input type='text' name='last_name'/></td> <td><input type='submit' value='Submit'/></td> </tr> </table> </form>''' print('Content-type: text/html') # http-header print() print(htmlTopString) # http body print(htmlNeck) print(htmlForm) print(htmlBotString)
#!/usr/bin/python import cgi # common gateway interface module import cgitb cgitb.enable() form = cgi.FieldStorage() # access to form fields from cgi # accepts get and post # create local vars from post fields first_name = form.getvalue('first_name') last_name = form.getvalue('last_name') myvar = form.getvalue('abc') # create local vars from get fields htmlTopString = '''<!doctype html> <html> <head> <title>First Python CGI</title> <meta charset='utf-8'/> <link rel='stylesheet' href='./myStyles.css'/> </head> <body> <main>''' htmlReturn = ''' <p><a href='./htmlform.py'>Retur</a></p>''' htmlBotString = ''' </main> </body> </html>''' htmlNeck = ''' <header> <h1>Data Received by Python from Form via CGI</h1> </header>''' print('Content-type: text/html') # http-header print() print(htmlTopString) # http body print(htmlNeck) print ("%12s<p>Hello <em>%s %s</em></p>" % (' ', first_name, last_name)) print ("%12s<p>I'm a get var <em>%s</em></p>" % (' ', myvar)) print(htmlReturn) print(htmlBotString)