Python Web Applications A KISS Introduction. Web Applications with Python Fetching, parsing, text processing Database client – mySQL, etc., for building.

Slides:



Advertisements
Similar presentations
PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
Advertisements

Web forms and CGI scripts Dr. Andrew C.R. Martin
Adding Dynamic Content to your Web Site
Ruby (on Rails) CSE 190M, Spring 2009 Week 3. Web Programming in Ruby Ruby can be used to write dynamic web pages Similar to PHP, chunks of Ruby begins.
Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
Python, CGI November 23, Unit 8. So Far We can write programs in Python (in theory at least) –Conditionals –Variables –While loops We can create a form.
Creating WordPress Websites. Creating a site on your computer Local server Local WordPress installation Setting Up Dreamweaver.
MIS2502: Data Analytics MySQL and SQL Workbench David Schuff
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
2440: 141 Web Site Administration Web Server-Side Programming Professor: Enoch E. Damson.
CGI Programming Languages Web Based Software Development July 21, 2005 Song, JaeHa.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
SJSU CS157B Dr. Lee1  2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section PHP and MySQL.
CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.
CS 1704 Introduction to Data Structures and Software Engineering.
1 Day 16 Sed and Awk. 2 Looking through output We already know what “grep” does. –It looks for something in a file. –Returns any line from the file that.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 24 – Web Servers (PWS, IIS, Apache, Jigsaw) Outline 24.1Introduction 24.2Microsoft Personal.
NETWORK CENTRIC COMPUTING (With included EMBEDDED SYSTEMS)
PHP1-1 PHP & SQL Xingquan (Hill) Zhu
INFM 603: Information Technology and Organizational Context Jimmy Lin The iSchool University of Maryland Thursday, October 18, 2012 Session 7: PHP.
PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.
Server-side Scripting Powering the webs favourite services.
4-1 INTERNET DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
Basics of Web Databases With the advent of Web database technology, Web pages are no longer static, but dynamic with connection to a back-end database.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
Student Learning Environment on the World Wide Web l CGI-programming in Perl for the connection of databases over the Internet. l Web authoring using Frontpage.
Web Application Programming Carol Wolf Computer Science.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
PHP TUTORIAL. HISTORY OF PHP  PHP as it's known today is actually the successor to a product named PHP/FI.  Created in 1994 by Rasmus Lerdorf, the very.
An program As a simple example of socket programming we can implement a program that sends to a remote site As a simple example of socket.
Python CGI programming
Python MySQL Database Access
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
1 In the good old days... Years ago… the WWW was made up of (mostly) static documents. –Each URL corresponded to a single file stored on some hard disk.
CSU - DCE Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the.
SQL Queries Relational database and SQL MySQL LAMP SQL queries A MySQL Tutorial and applications Database Building Assignment.
Putting it all together Dynamic Data Base Access Norman White Stern School of Business.
CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.
HTML, PHP, and MySQL: Putting It All Together. Making a Form Input tags Types: “text” “radio” “checkboxes” “submit”
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
CS 4720 Dynamic Web Applications CS 4720 – Web & Mobile Systems.
CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,
CSE 154 LECTURE 5: INTRO TO PHP. URLs and web servers usually when you type a URL in your browser: your computer looks up the.
Rails & Ajax Module 5. Introduction to Rails Overview of Rails Rails is Ruby based “A development framework for Web-based applications” Rails uses the.
1 Developing WBEM Clients Using Python Tim Potter Hewlett-Packard Company
Intermediate CGI & CGI.pm Webmaster II - Fort Collins, CO Copyright © XTR Systems, LLC CGI Programming & The CGI.pm Perl Module Instructor: Joseph DiVerdi,
CITA 310 Section 7 Installing and Testing a Programming Environment (Textbook Chapter 7)
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
CHAPTER 10 PHP MySQL Database
CP476 Internet Computing Perl CGI and MySql 1 Relational Databases –A database is a collection of data organized to allow relatively easy access for retrievals,
CSC 405: Web Application Engineering II8.1 Web programming using PHP What have we learnt? What have we learnt? Underlying technologies of database supported.
Chapter 16 Web Pages And CGI Scripts Department of Biomedical Informatics University of Pittsburgh School of Medicine
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
Web Page Designing With Dreamweaver MX\Session 1\1 of 9 Session 1 Introduction to PHP Hypertext Preprocessor - PHP.
MYSQL AND MYSQL WORKBENCH MIS2502 Data Analytics.
PHP stands for …….. “PHP Hypertext Pre-processor” and is a server-side scripting language like ASP. PHP scripts are executed on the server PHP supports.
Lesson 11. CGI CGI is the interface between a Web page or browser and a Web server that is running a certain program/script. The CGI (Common Gateway Interface)
The Common Gateway Interface (CGI) Pat Morin COMP2405.
Web Database Programming Using PHP
Introduction to Dynamic Web Programming
Web Database Programming Using PHP
mysql and mysql workbench
PHP / MySQL Introduction
PHP Introduction.
Intro to PHP.
Tutorial 6 PHP & MySQL Li Xu
PHP By Prof. B.A.Khivsara Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and.
Web Application Development Using PHP
Presentation transcript:

Python Web Applications A KISS Introduction

Web Applications with Python Fetching, parsing, text processing Database client – mySQL, etc., for building dynamic information on-the-fly Python CGI web pages – or even web servers!

Fetching Pages Use urllib or urllib2 library Python is used frequently to parse data pages – regular expressions are very important Nothing new here >>> from urllib2 import * >>> file = urlopen(" >>> file.read() ' \n \n A Simple Page \n \n \nNothing to see...\n \n ‘ >>> file.read.split(‘\n’)( all the lines at once) >>> file.readline() (one line at a time)

Parsing Data Say there is a webpage you want to pull certain information from every day…use Python! You already know how to parse webpages though – and that isn’t really a web application Lots of complex parsers that are beyond the scope of this demo On to the interesting stuff!

Databases A database is a program that manages a set of data (gee, who would have guessed) Most databases are relational SQL databases SQL – Structured Query Language, a standardized syntax for telling a database what to do – although Microsoft’s SQL is different from everyone else’s

Databases mySQL is the most widely used open- source database – Python supports it! Uses library MySQLdb – a mySQL API for Python Must install this yourself – on Linux with a package manager or by tarball, on Windows with an archive

How SQL Databases Work Databases take some sort of connection as communication – usually through TCP/IP Most databases are set up as a group of schemas, each containing a set of tables Each table contains a set of columns, each with a different data type Rows are the entries in a table

MySQL Queries The library is called _mysql, and it implements the standard Python Database API Specification Almost all database modules for python implement the API, which defines the set of functions connect() takes parameters to establish a connection, and select_db() chooses the schema to work with >>> from _mysql import * >>> db = connect(host="localhost",user="mao",passwd="") >>> db.select_db("test")

Some Python Database Commands CommandWhat it does _mysql.connect(params) Returns a new object that represents the database conn.select_db(db) Selects the schema (database) to use after connecting conn.query(string) Queries the database with a SQL syntactic string conn.store_result() Returns all the results of the last query at once conn.use_result() Returns an iterator over the results of the last query conn.fetch_row(maxrows) Returns up to maxrows of the query conn.info() Returns some information about the last query conn.close() Closes the database connection

Some Simple Database Examples >>> db.query("CREATE TABLE students (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, lname TEXT, fname TEXT)") >>> db.query("INSERT INTO students (lname, fname) VALUES ('Mao', 'Andrew')") >>> db.query("INSERT INTO students (lname, fname) VALUES ('Walker', 'James')") >>> db.query("INSERT INTO students (lname, fname) VALUES ('Cohen', 'Ian')") >>> db.query("SELECT * FROM students") >>> people = db.store_result() >>> people.fetch_row() (('1', 'Mao', 'Andrew'),) >>> people.fetch_row() (('2', 'Walker', 'James'),) >>> people.fetch_row() (('3', 'Cohen', 'Ian'),) >>> people.fetch_row() () >>> db.close()

What Do We Do Now? So we have access to a database and we can get data from it – but how do we get that across the web? Answer – CGI! CGI stands for Common Gateway Interface – Allows any script (perl, bash, python) to output to HTTP. It’s how you submit your CSE 121 homework

Using Python through CGI First, you must set up the web server to use handle Python scripts with CGI With Apache, this is easy – 2 lines in httpd.conf AddHandler cgi-script.cgi.py … Options ExecCGI

A Web Python Hello World! When we run a Python program as a CGI script, the output goes out directly and the file must be executable (+x). The first line we have to print are the HTTP headers – this tells the browser to read it as either text, or render it as HTML. Any line after that comprises the actual HTML or text itself #!/usr/bin/python # Required header that tells the browser how to render the text. print "Content-Type: text/plain\n\n" # Print a simple message to the display window. print "Hello, World!\n"

Lo and Behold… Notice that the first line went through as HTTP headers.

Tying It All Together… We want to be able to receive some input to our python program, and then output something based on that input. The python cgi library can find all the HTTP POST and GET variables. We can then use these to query the database, etc.

A Final Simple Program cgitb is a library that dumps errors to the output – you don’t have to go searching for it in your log file. cgi.FieldStorage() is a pseudo-dictionary containing all the POST and GET variables and their values. #!/usr/bin/python import cgi,_mysql,cgitb; cgitb.enable() search = cgi.FieldStorage() # returns a dictionary print "Content-Type: text/plain\n\n" if not (search.has_key("fname") and search.has_key("lname")): print "Need a first and last name!" exit fname,lname = search["fname"].value,search["lname"].value conn = _mysql.connect(host="localhost",user="mao") conn.select_db("test") conn.query("SELECT * FROM students WHERE lname = '"+lname+"' AND fname = '"+fname+"'") people = conn.store_result() row = people.fetch_row() if row == (): print fname,lname,"is not a student.\n" else: print fname,lname,"is a student.\n" conn.close()

HTML Form Interface Student Search First Name: Last Name: </html

Questions?