Problem with Twisted

Grom grommasher at gmail.com
Mon Sep 1 18:00:43 CEST 2008


Hello. I have something like that:

from twisted.words.protocols.jabber import xmlstream
from twisted.internet import protocol
from twisted.words.xish import domish, utility
from twisted.internet import reactor
from twisted.python import log
import sys, hashlib
log.startLogging(sys.stdout)


def magicHash(password, sid):
        magic1 = 0x50305735
        magic2 = 0x12345671
        sum = 7
        for s in range(len(password)):
            z = ord(password[s]);
            if (z == ' '):
                continue
            if (z == '\t'):
                continue
            magic1 = magic1 ^ ((((magic1 & 0x3f) + sum) * z) + (magic1
<< 8))
            magic2 = magic2 + ((magic2 << 8) ^ magic1)
            sum += z
            magic1 = magic1 & 0x7fffffff
            magic2 = magic2 & 0x7fffffff

        return ('%08x%08x'%(magic1, magic2))

def ping(xmlstream):
	print 'ping'
	xmlstream.send('  \t  ')
	reactor.callLater(40, ping, xmlstream)

class TlenAuthInitializer(object):
	def __init__(self, xs):
		print "Wykonywanie TlenAuthInitializer"
		self.xmlstream = xs
	def initialize(self):
		print "Wykonywanie TlenAuthInitializer - initialize"
		iq = xmlstream.IQ(self.xmlstream, "set")
		print '1'
		iq['id'] = self.xmlstream.sid
		print '2'
		q = iq.addElement('query', 'jabber:iq:auth')
		print '3'
		q.addElement('username', content = self.xmlstream.authenticator.jid)
		print '4'
		q.addElement('digest', content =
hashlib.sha1(magicHash(self.xmlstream.authenticator.password,
self.xmlstream.sid)).hexdigest())
		print '4'
		q.addElement('resource', content  = 't')
		print '6'
		q.addElement('host', content = 'tlen.pl')
		print q.toXml(prefixes=self.prefixes, closeElement=0)
		print '7'
		d = iq.send('q')
		print '8'
		d.addCallback(self._authreply)
		print '9'
		d.addErrBack(self._authfail)
		print '10'

	def _authreply(self, el):
		print "Wykonywanie TlenAuthInitializer - _authreply"
		print el.toXml()
		reactor.callLater(40, ping, self.xmlstream)

	def _authfail(self, el):
		print "Wykonywanie TlenAuthInitializer - _authfail"
		print el.toXml()

class TlenAuthenticator(xmlstream.ConnectAuthenticator):
	def __init__(self, jid, password, host):
		print "Wykonywanie TlenAuthenticator"
		xmlstream.ConnectAuthenticator.__init__(self, host)
		self.jid = jid
		self.password = password

	def associateWithStream(self, xs):
		print "Wykonywanie TlenAuthenticator - associateWithStream"
		xs.version = (0, 0)
		xmlstream.ConnectAuthenticator.associateWithStream(self, xs)

		inits = [(TlenAuthInitializer, True)]
		for initClass, required in inits:
			init = initClass(xs)
			init.required = required
			xs.initializers.append(init)

class TlenStream(xmlstream.XmlStream):
	def sendHeader(self):
		print "Wykonywanie TlenStream - sendHeader"
		rootElem = domish.Element((None, 's'))
		rootElem['v'] = '9'
		rootElem['t'] = '06000224'
		self.rootElem = rootElem
		self.send(rootElem.toXml(prefixes=self.prefixes, closeElement=0))
		self._headerSent = True
		print 'XMLed rootElem from sendHeader
'+rootElem.toXml(prefixes=self.prefixes, closeElement=0)

	def sendFooter(self):
		print "Wykonywanie TlenStream - sendFooter"
		self.send('</s>')

	def onDocumentStart(self, rootelem):
		print "Wykonywanie TlenStream - onDocumentStart"
		xmlstream.XmlStream.onDocumentStart(self, rootelem)
		print 'rootelem from onDocumentStart '+rootelem.toXml()
		if rootelem.hasAttribute("i"):
			self.sid = rootelem["i"]
		self.authenticator.streamStarted(rootelem)

class TlenStreamFactory(xmlstream.XmlStreamFactory):
    def __init__(self, authenticator):
	print "Wykonywanie TlenStreamFactory"
        xmlstream.XmlStreamFactory.__init__(self, authenticator)
        self.authenticator = authenticator


    def buildProtocol(self, _):
	print "Wykonywanie TlenStreamFactory - buildProtocol"
        self.resetDelay()
        # Create the stream and register all the bootstrap observers
        xs = TlenStream(self.authenticator)
        xs.factory = self
        for event, fn in self.bootstraps: xs.addObserver(event, fn)
        return xs

def lg(el):
	print 'all>',el
def err(el):
	print 'err>', el
factory = TlenStreamFactory(TlenAuthenticator('portsentry',
'linux1991','s1.tlen.pl' ))
factory.addBootstrap('/*', lg)

reactor.connectTCP('s1.tlen.pl', 443, factory)
reactor.run()

Now... I have problem with that. It not print me any errors but there
is something with d = iq.send('q') on TlenAuthInitializer -
initialize. I dont know why but python print me just "6" and stops...
its strange... anyone have suggestions?



More information about the Python-list mailing list