Python
170 likes | 426 Views
Python. 20021080 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
Python
E N D
Presentation Transcript
Python 20021080 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!“ • <form name="form" method="get" action="http://sid.redfeel.net/~rene/cgi-bin/test.py"><p>email:<input type="text" name="email"> name:<input type="text" name="name"><input type="submit" value=“Run"></p></form>
Python as CGI • template.html <html> <head> <META NAME="keywords" CONTENT="blah blah -- your ad here“> <title>Python is Fun!</title> </head> <body> <!--INSERT CONTENT HERE--> </body> </html>
Python as CGI • #!/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("<!--INSERT CONTENT HERE-->", 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 • http://www.python.or.kr/