Twisted Perspective Broker: get client ip

Andrea Di Mario anddimario at
Wed Sep 14 09:06:39 EDT 2011

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:, 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,] ............

Could you suggest me something?
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):
  = 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
      log.msg("Connessione stabilita, vertificato valido: %s" %
   return True

class MyRealm:
    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()'server.conf')
       PORT = CONFIGURATION.get('general', 'port')
       LOGFILE = CONFIGURATION.get('general', 'log')


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

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

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

Andrea Di Mario

More information about the Python-list mailing list