Python Hyunjong Lee
contents Introduction Syntax & data types Tools Python as CGI
Introduction Developed by Guido Van Rossum in 1990 Interpret based language Has simple syntax Dynamic type binding Embedded high level data structure Garbage collection Module structure (glue language) Fast development cycle
Syntax Syntax is very simple Similar to most of other languages Indentation sensitive
High level built-in data types Complex number List (can be nested) Tuples Dictionary
Tools Py2exe – convert python script into exe file Python2c – convert python script into C source file SWIG(simplified wrapper and interface generator) – glue code generator
Python as CGI #!/usr/bin/python import cgi#import CGI module print "Content-Type: text/plain\n\n" The_Form = cgi.FieldStorage() for name in The_Form.keys(): print "Input: " + name print " value: " + The_Form[name].value + “\n” print "Finished!“ name: name:
Python as CGI template.html <html><head> Python is Fun! Python is Fun! </head><body> </body></html>
Python as CGI #!/usr/bin/python #!/usr/bin/python import re #import regular expression module import cgi TemplateFile = "template.html" # Display function def def Display(Content): fp = open(TemplateFile, "r") TemplateInput = fp.read() #open & read entire file fp.close() BadTemplateException = “problem in template file" SubResult = re.subn(" ", Content,TemplateInput) if SubResult[1] == 0: raise BadTemplateException print "Content-Type: text/html\n\n“ print SubResult[0] #make dynamic contents here …
Python as CGI import MySQLdb# import SQL module connection = MySQLdb.connect(user=‘uuu', passwd=‘ppp', db=‘db') cursor = connection.cursor() cursor.execute(“SELECT * FROM some_table”) cursor.close()
References