#!/usr/bin/python2 # -*- coding: utf-8 -*- import cgi form=cgi.FieldStorage() who = form.getvalue('who') print "Content-Type: text/html" # ALWAYS this header (HTML is following) # more headers, such as Set-Cookie: if (who): print "Set-Cookie: whop=%s" % who print "Set-Cookie: who=%s; Path=/~jensen" % who #available to your php and html pages, but NOT to other python scripts print # blank line, end of headers print """
""" # -------------- fetch a cookie in our path (/cgi-bin ....) import Cookie import os try: cookie = Cookie.SimpleCookie(os.environ["HTTP_COOKIE"]) print "Welcome back " + cookie["whop"].value except (Cookie.CookieError, KeyError): print "First python order" import psycopg2 try: con=psycopg2.connect('dbname=fruitstand') except: print "Database connect trouble" import sys sys.exit() curs=con.cursor() # get a dictionary of prices curs.execute("select trim(name), price from fruit where stock>0") prices={} for f,p in curs.fetchall(): prices[f]=float(p) print "Fruit | Quantity | Price | Total |
---|---|---|---|
%s | %d | %.2f | %.2f |
%s | Seems we don't have %d | ||
Total for all | $ %.2f |