[Twisted-Python] (no subject)

Moshe Zadka m at moshez.org
Fri Sep 21 09:49:13 MDT 2001


>------------------ control_panel.py -----------<
# Twisted, the Framework of Your Internet
# Copyright (C) 2001 Matthew W. Lefkowitz
# 
# This library is free software; you can redistribute it and/or
# modify it under the terms of version 2.1 of the GNU Lesser General Public
# License as published by the Free Software Foundation.
# 
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

from twisted.spread import pb
from twisted.internet import passport, main

class ControlPanelPerspective(pb.Perspective):
    def perspective_shutdown(self):
        main.shutDown()

    def perspective_save(self, name=''):
        import __main__
        __main__.application.save(name)

class ControlPanelService(pb.Service):
    def getPerspectiveNamed(self, name):
        return ControlPanelPerspective("any", self)

if __name__ == '__main__':
    import control_panel
    from twisted.internet.main import Application
    app = Application("control_panel")
    i = passport.Identity("guest", app)
    i.setPassword("guest")
    app.authorizer.addIdentity(i)
    bf = pb.BrokerFactory(app)
    svc = control_panel.ControlPanelService("control_panel", app)
    i.addKeyForPerspective(svc.getPerspectiveNamed('any'))
    app.listenOn(pb.portno, bf)
    app.save("start")
>------------------------------------------------------------<

>----------------- pbshutdown.py -----------------------------<

# Twisted, the Framework of Your Internet
# Copyright (C) 2001 Matthew W. Lefkowitz
# 
# This library is free software; you can redistribute it and/or
# modify it under the terms of version 2.1 of the GNU Lesser General Public
# License as published by the Free Software Foundation.
# 
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

from twisted.spread import pb
from twisted.internet import tcp, main

def failure(error):
    print "Failure...",error
    main.shutDown()

def disconnected():
    print "disconnected."
    main.shutDown()

def connected(perspective):
    perspective.shutdown(pberrback=main.shutDown, pbcallback=main.shutDown)
    print "connected."

def preConnected(identity):
    identity.attach("control_panel", "any", None,
                    pbcallback=connected,
                    pberrback=failure)

# run a client
b = pb.Broker()
b.requestIdentity("guest",  # username
                  "guest",  # password
                  callback = preConnected,
                  errback  = failure)

tcp.Client("localhost",pb.portno,b)

main.run()
>------------------------------------------------------------<

>------------------- pbsave.py  ----------------------------<
# Twisted, the Framework of Your Internet
# Copyright (C) 2001 Matthew W. Lefkowitz
# 
# This library is free software; you can redistribute it and/or
# modify it under the terms of version 2.1 of the GNU Lesser General Public
# License as published by the Free Software Foundation.
# 
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

from twisted.spread import pb
from twisted.internet import tcp, main

def failure(error):
    print "Failure...",error
    main.shutDown()

def disconnected():
    print "disconnected."
    main.shutDown()

def connected(perspective):
    perspective.save('pbservice', pberrback=main.shutDown, pbcallback=main.shutDown)
    print "connected."

def preConnected(identity):
    identity.attach("control_panel", "any", None,
                    pbcallback=connected,
                    pberrback=failure)

# run a client
b = pb.Broker()
b.requestIdentity("guest",  # username
                  "guest",  # password
                  callback = preConnected,
                  errback  = failure)

tcp.Client("localhost",pb.portno,b)

main.run()
>------------------------------------------------------------<

-- 
The Official Moshe Zadka FAQ: http://moshez.geek
The Official Moshe Zadka FAQ For Dummies: http://moshez.org
Read the FAQ





More information about the Twisted-Python mailing list