[Twisted-web] newbiew question

arun chhetri chhetriarun84 at gmail.com
Wed Jul 23 18:40:35 EDT 2008

hi friends,
as per the previous question I think I have found my answer,, the structure
of my program is something like this

from twisted.web import resource, static, server

import sys
import Calendar

account =3D None
account =3D []

class HomePage(resource.Resource):
    def render(self,request):
    <form action=3D/optional method=3D"post">
        User Name <input type=3D"text" name=3D"username"><br/>
        Pass Word <input type=3D"password" name=3D"pswd"><br/>
        ServerAdd <input type=3D"text" name=3D"server"><br/>
        <input type=3D"submit" Value=3D"Submit"><br/>

class Optional(resource.Resource):
    def __init__(self):
    def render(self,request):
        self.user =3D request.args["username"]
        self.pswd =3D request.args["pswd"]
        self.server =3D request.args["server"]
        return "<a href =3D /optional/Calendar> Click this link"
    def getChild(self,path,request):
        return MainCalendar(self.user[0],self.pswd[0],self.server[0])

class MainCalendar(resource.Resource):
    def __init__(self,user,pswd,server):
        if user !=3D None:
            if pswd !=3D None:
                if server !=3D None:
                    self.user =3D user
                    self.pswd =3D pswd
                    self.server =3D server
                    self.CalendarObject =3D
    def render(self,request):
        return """ <p><a href =3D /optional/calendar/month> Month View</br>
                    <p><a href =3D /optional/calendar/week> Month View</br>
                    <p><a href =3D /optional/calendar/day> Day View"""
    def getChild(self,path,request):
        if path =3D=3D "/optional/calendar/month":
            return Month(self.CalendarObject)

        elif path =3D=3D "/optional/calendar/week":
            return week(self.CalendarObject)

        elif path =3D=3D "/optional/calendar/day":
            return day(self.CalendarObject)

class month(resource.Resource):
    def __init__(self,CalendarObject):
        self.CalendarObject =3D CalendarObject
    def render(self,request):
        return "Month for %s is good
class week(resource.Resource):
    def __init__(self,CalendarObject):
        self.CalendarObject =3D CalendarObject
    def render(self,request):
        return "Week for %s is
class day(resource.Resource):
    def __init__(self,CalendarObject):
        self.CalendarObject =3D CalendarObject
    def render(self,request):
        return "Day for %s is good"%self.CalendarObject.account.session.user

if __name__ =3D=3D "__main__":
    from twisted.internet import reactor
    root =3D resource.Resource()
    site =3D server.Site(root)

Is there any fault in the structure of my code, or any error which i cannot
There is one more question,, how can I bind this to my website for ex

Thanks to all

On Wed, Jul 23, 2008 at 2:30 PM, Maarten ter Huurne
<maarten at treewalker.org>wrote:

> On Wednesday 23 July 2008, Christopher Armstrong wrote:
> > On Tue, Jul 22, 2008 at 11:26 PM, Maarten ter Huurne
> > <maarten at treewalker.org> wrote:
> >
> > > The information about the user does not belong in any Resource
> > > subclass: a Resource is a page that can be generated for different
> > > users, so it should only contain information that is the same for all
> > > users. Any user specific data should be fetched via the request objec=
> >
> > This is totally inaccurate. It's perfectly reasonable to store
> > user-specific data in Resource objects.  "a Resource is a page that
> > can be generated for different users" is either irrelevant or not
> > true, I can't tell which. You can dynamically and return Resources
> > based on which user is making the request.
> Ah, I never realized it could be used like that. I thought "resource" was
> intended to be used as "something reachable by URL", with a 1:1 mapping of
> URL path to Resource instance. Sorry for spreading misinformation.
> It is still not entirely clear to me what an avatar is though and how it
> relates to resources and authorization.
> When accessing a file system, would the "traditional" authorization
> approach
> be to have permission bits on every file indicating whether that file can
> be read or written by a certain users, while the "avatar" approach would =
> to give the user a chroot environment with only files under it that that
> user should have access to?
> On Wednesday 23 July 2008, Phil Mayers wrote:
> > Since this is a common mis-conception (one I suffered from and have now
> > disabused myself of) it's worth discussing.
> >
> > If my understanding is correct: twisted.cred uses the concept of an
> > "avatar". Avatars (I think...):
> >
> >   * are protocol objects
> >   * represent the user
> >
> > In twisted.web, the Resource *is* the avatar. In twisted.mail.imap, the
> > Mailbox is the avatar. In twisted.conch, the Shell is the avatar (and so
> > on).
> In what way does the avatar represent the user? Is it like a Mars lander
> representing the control team on Earth?
> > I found this initially confusing, because in many web frameworks e.g.
> > Zope, where I came from, the objects representing resources are:
> >
> >   * long lived
> >   * the same instances serve >1 HTTP request
> >   * instantiated at process start time
> That is the approach I was familiar with from Java servlets and from
> Webware. Since this approach can be mapped onto twisted.web easily I never
> realized it was designed for a different approach.
> On Wednesday 23 July 2008, Jean-Paul Calderone wrote:
> > Sounds like you're basically on target.  One area that you didn't talk
> > much about is what the role of the user object is.  It's possible to
> > just examine the user object and then, in your custom Resource, decide
> > what to do based on that examination.  A more powerful approach is to
> > actually delegate those decisions to the user object (and this is why
> > twisted.web.guard makes the user object a Resource).  This removes all
> > checking from your code and just makes the right code for each user
> > execute automatically.  Explicit checking is tedious and error prone.
> > Delegating all authorization decisions to the avatar simplifies the
> > code and makes it less likely that you'll introduce a security issue.
> Does this mean the top-level Resource node is the user object, so in fact
> there is a user-specific Resource tree?
> Bye,
>                 Maarten
> _______________________________________________
> Twisted-web mailing list
> Twisted-web at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-web/attachments/20080723/c2=

More information about the Twisted-web mailing list