[Twisted-Python] session

William Dode wilk-ml at flibuste.net
Sat Feb 15 13:11:40 EST 2003


I did an other litle example to see how can work session. A shop where
you put some articles in your bag...

I do request.getSession() and set attribute directly to the session...
Is it the good way ?
import twisted.web.resource
import cgi

class Shop(twisted.web.resource.Resource):
    """ ReportRequest with forms and cookies """
    def isLeaf(self):
        return true
    def render(self, request):
        session = request.getSession()
            bag = session.bag
        except AttributeError:
            bag = session.bag={}

            article = request.args["put"][0]
            bag[article] = session.bag.get(article,0)+1
        except KeyError:
            pass # no article to put
        out = []
        out.append("""<html><body>What do you want to put in your bag ?<br>
        <a href='?put=chocolate'>chocolate</a><br>
        <a href='?put=banana'>banana</a><br>
        <a href='?put=apple'>apple</a><br>
        if len(bag) == 0:
            out.append("Your bag is empty")
            out.append("You have in your bag :<br>")
            for k,v in bag.items():
                out.append("%d %s<br>"%(v,k))
        return "".join(out)

from twisted.internet import reactor
from twisted.web import static, server

site = server.Site(Shop())

More information about the Twisted-Python mailing list