Twisted Perspective Broker: get client ip

Discussion in 'Python' started by Andrea Di Mario, Sep 14, 2011.

  1. Hi, i'm writing a perspective broker server. Now, i should get the
    client IP, that perspective broker writes well in the log. I've tried
    to get it from MyRealm with: mind.broker.transport.getPeer(), without
    success. I've tried self.transport.getPeer() to, with this result:
    exceptions.AttributeError: Listner instance has no attribute 'transport'
    It's strange, because PB wrote the client IP, infact in log i've line with:
    2011-09-11 16:41:58+0200 [Broker,0,127.0.0.1] ............

    Could you suggest me something?
    Thanks.
    Here the code:

    from OpenSSL import SSL
    from twisted.internet import reactor, ssl
    from ConfigParser import SafeConfigParser
    from twisted.python import log
    from twisted.spread import pb
    from twisted.cred import checkers, portal
    from zope.interface import implements
    import hashlib

    class Listner(pb.Avatar):

    def __init__(self, name):
    self.name = name

    def perspective_getDictionary(self, dictionary):
    print dictionary

    def perspective_simplyAccess(self, access):
    print access

    def verifyCallback(connection, x509, errnum, errdepth, ok):
    if not ok:
    log.msg("Certificato non valido: %s" % x509.get_subject())
    return False
    else:
    log.msg("Connessione stabilita, vertificato valido: %s" %
    x509.get_subject())
    return True


    class MyRealm:
    implements(portal.IRealm)
    def requestAvatar(self, avatarId, mind, *interfaces):
    if pb.IPerspective not in interfaces:
    raise NotImplementedError
    return pb.IPerspective, Listner(avatarId), lambda:None


    if __name__ == "__main__":

    CONFIGURATION = SafeConfigParser()
    CONFIGURATION.read('server.conf')
    PORT = CONFIGURATION.get('general', 'port')
    LOGFILE = CONFIGURATION.get('general', 'log')

    log.startLogging(open(LOGFILE,'a'))


    myContextFactory =
    ssl.DefaultOpenSSLContextFactory(CONFIGURATION.get('general',
    'keypath'), CONFIGURATION.get('general', 'certpath'))
    ctx = myContextFactory.getContext()
    ctx.set_verify(SSL.VERIFY_PEER |
    SSL.VERIFY_FAIL_IF_NO_PEER_CERT, verifyCallback)

    ctx.load_verify_locations(CONFIGURATION.get('general', 'cacert'))

    p = portal.Portal(MyRealm())
    c = checkers.FilePasswordDB('passwords.txt',
    caseSensitive=True, cache=True)
    p.registerChecker(c)
    factory = pb.PBServerFactory(p)
    reactor.listenSSL(int(PORT), factory, myContextFactory)
    reactor.run()

    --
    Andrea Di Mario
    Andrea Di Mario, Sep 14, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. June Moore

    Connection broker pooling class

    June Moore, Jun 30, 2003, in forum: Java
    Replies:
    1
    Views:
    1,109
    Mark Benson
    Jun 30, 2003
  2. Y2KYZFR1
    Replies:
    2
    Views:
    423
    Tim Churches
    Sep 21, 2004
  3. sarah Fernandes
    Replies:
    1
    Views:
    314
  4. sarah Fernandes
    Replies:
    0
    Views:
    271
    sarah Fernandes
    Aug 25, 2011
  5. Ryan
    Replies:
    0
    Views:
    492
Loading...

Share This Page