python cgi programming

Download Python CGI programming

Post on 20-Mar-2016

45 views

Category:

Documents

1 download

Embed Size (px)

DESCRIPTION

Python CGI programming. Outline. HTML forms Basic CGI usage Setting up a debugging framework Security Handling persistent data Locking Sessions Cookies File upload Generating HTML Performance. A typical HTML form. - PowerPoint PPT Presentation

TRANSCRIPT

  • PythonCGI programming

    1999 CNRI, Guido van Rossum

  • OutlineHTML forms Basic CGI usageSetting up a debugging frameworkSecurityHandling persistent dataLockingSessionsCookiesFile uploadGenerating HTMLPerformance

    1999 CNRI, Guido van Rossum

  • A typical HTML form

    Your first name: Your last name: Click here to submit form:

    1999 CNRI, Guido van Rossum

  • A typical CGI script#!/usr/local/bin/pythonimport cgi

    def main(): print "Content-type: text/html\n" form = cgi.FieldStorage()# parse query if form.has_key("firstname") and form["firstname"].value != "": print "Hello", form["firstname"].value, "" else: print "Error! Please enter first name."

    main()

    1999 CNRI, Guido van Rossum

  • CGI script structureCheck form fieldsuse cgi.FieldStorage class to parse querytakes care of decoding, handles GET and POST"foo=ab+cd%21ef&bar=spam" --> {'foo': 'ab cd!ef', 'bar': 'spam'} # (well, actually, ...)Perform actionthis is up to you!database interfaces availableGenerate HTTP + HTML outputprint statements are simplesttemplate solutions available

    1999 CNRI, Guido van Rossum

  • Structure refinementform = cgi.FieldStorage()if not form: ...display blank form...elif ...valid form...: ...perform action, display results (or next form)...else: ...display error message (maybe repeating form)...

    1999 CNRI, Guido van Rossum

  • FieldStorage detailsBehaves like a dictionary:.keys(), .has_key()# but not others!dictionary-like object ("mapping")Itemsvalues are MiniFieldStorage instances.value gives field value!if multiple values: list of MiniFieldStorage instancesif type(...) == types.ListType: ...may also be FieldStorage instancesused for file upload (test .file attribute)

    1999 CNRI, Guido van Rossum

  • Other CGI nicetiescgi.escape(s)translate "" to "

Recommended

View more >