[Twisted-Python] Twisted Perspective Broker: get client ip

Andrea Di Mario anddimario at gmail.com
Mon Sep 12 02:19:53 MDT 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: 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




More information about the Twisted-Python mailing list