[Twisted-web] web2 response.setCookie and request.getCookie
shortcuts?
Clark C. Evans
cce at clarkevans.com
Wed Nov 16 15:42:34 MST 2005
I'm still having fun with web2, it is such a huge improvment over
twisted.web and I'm absolutely thrilled with it. However, I have
a few suggestions:
1. It would be supreme if render() and locateChild() just provided
me the Request object; the incantation to ctx.locate() seems a
tedious an unnecessary step.
2. I would love a request.getCookie('cookie-name') and
response.setCookie('cookie-name','cookie-value', path = '/')
helper; that adds the given cookie to the current list.
3. Make the 'path' in the Cookie constructor and setCookie default
to '/'. Almost all of the time you want your cookies to be
site-wide; adding path = '/' to each request adds a point of
failure that makes the system less usable.
Kind Regards,
Clark
P.S. Following is sample cookie code that works with web2
---
from twisted.internet import reactor, defer
from twisted.web2 import server, http, resource, channel, iweb
from twisted.web2.http_headers import Cookie
class Toplevel(resource.Resource):
addSlash = True
def render(self, ctx):
request = ctx.locate(iweb.IRequest)
found = False
for cookie in request.headers.getHeader('cookie',[]):
if cookie.name == 'testing':
print "found", cookie.value
found = True
response = http.Response(stream="Hello monkey!")
if not found:
print "not-found"
cookie = Cookie("testing","womble",path="/")
response.headers.setHeader('Set-Cookie',(cookie,))
return response
# Standard twisted application Boilerplate
from twisted.application import service, strports
application = service.Application("demoserver")
site = server.Site(Toplevel())
s = strports.service('tcp:8080', channel.HTTPFactory(site))
s.setServiceParent(application)
More information about the Twisted-web
mailing list