Loadosenvironhttpcookie except keyerror except

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: s, it could be from the same person who made the previous one, or maybe not. • An connected set of interactions must somehow implement – persistence (information that is remembered) – identity (an ID to something like it) CS 206 G. Dudek 130 Cookies • How to correlate sessions from the same user? sessions – Store "cookie" in browser » controversial, but useful – Module: Cookie.py (Tim O'Malley) py (T » writes "Set-Cookie" headers » parses HTTP_COOKIE environment variable – Note: using cookies affects our debug framework » cookies must be printed as part of HTTP headers » cheapest solution: » move printing of blank line into worker module » (and into exception handler of debug framework) CS 206 G. Dudek 131 Cookie example import os, cgi, Cookie import os cgi c = Cookie.Cookie() try: c["user"] = user print c c.load(os.environ["HTTP_COOKIE"]) except KeyError: except KeyError pass print """ <form action="/cgi-bin/test.py" method="get"> <input type="text" name="user" value="%s"> form = cgi.FieldStorage() form cgi </form> try: """ % cgi.escape(user) """ cgi user = form["user"].value except KeyError: except KeyError try: user = c["user"].value except KeyError: KeyError user = "nobody" # debug: show the cookie header we wrote print "<pre>" print cgi.escape(str(c)) print cgi print "</pre>" CS 206 G. Dudek 132 File upload example import cgi import cgi form = cgi.FieldStorage() form cgi if not form: print """ <form action="/cgi-bin/test.py" method="POST" enctype="multipart/form-data"> enctype <input type="file" name="filename"> type="file" <input type="submit"> </form> """ elif form.has_key("filename"): elif form.has_key("filename"): item = form["filename"] if item.file: data = item.file.read() # read cont...
View Full Document

Ask a homework question - tutors are online