[jdev] Inputting arbitrary XML for testing

Julien PUYDT julien.puydt at laposte.net
Sat May 7 11:15:31 CDT 2005


Alexey Nezhdanov a écrit :
> no attach :)

I did attach... some mail filter must have thought it was a virus ; 
let's try to put it inline then (but it may need some editing):

#!/usr/bin/python

import socket

class VerboseSocket:
         def __init__ (self, sock):
                 self.socket = sock

         def connect (self, host, port):
                 print 'connecting to %s on port %s' % (host, port)
                 self.socket.connect ((host, port))

         def send (self, data, flags = 0):
                 print 'SEND:'
                 print data
                 self.socket.send (data, flags)

         def recv (self, size, flags = 0):
                 data = self.socket.recv (size, flags)
                 print 'RECV:'
                 print data
                 return data

         def bind (self, *args):
                 self.socket.bind (*args)

         def accept (self):
                 (s, info) = self.socket.accept ()
                 return (VerboseSocket(s), info)

         def listen (self, *args):
                 self.socket.listen (*args)

         def fileno (self):
                 return self.socket.fileno ()

class JabberServer:

         def __init__ (self):
                 from sys import stdin
                 import select
                 s  = VerboseSocket (socket.socket (socket.AF_INET, 
socket.SOCK_STREAM))
                 s.bind (('localhost', 5223))
                 s.listen (1)
                 (client, info) = s.accept ()
                 print 'New connection'
                 running = True
                 while running:
                         (r, w, x) = select.select ([stdin, client], [], [])
                         if client in r:
                                 data = client.recv (1024)
                                 pass # we don't care about the data: 
VerboseSocket!
                         if stdin in r:
                                 client.send (stdin.readline ())


class JabberClient:

         def __init__ (self, name, passwd):
                 from sys import stdin
                 import select
                 s = self.get_ready_socket (name, passwd)
                 running = True
                 while running:
                         (r, w, x) = select.select ([stdin, s], [], [])
                         if s in r:
                                 data = s.recv (1024)
                                 pass # we don't care about the data: 
VerboseSocket!
                         if stdin in r:
                                 s.send (stdin.readline ())

         def get_ready_socket (self, name, passwd):
                 import sre, sha
                 s = VerboseSocket (socket.socket (socket.AF_INET, 
socket.SOCK_STREAM))
                 s.connect ('localhost', 5222)
                 s.send ("""<?xml version='1.0' encoding='UTF-8'?>""")
                 s.send ("""<stream:stream xmlns="jabber:client" 
xmlns:stream="http://etherx.jabber.org/streams" to="localhost" 
id="msg_1">""")
                 data = s.recv (1024)
                 match = sre.findall ("id='[0-9A-F]+'", data)[0]
                 id = match[4:-1]
                 s.send ("""<iq type="get" id="msg_2">  <query 
xmlns="jabber:iq:auth">  <username>%s</username></query></iq>""" % name)
                 s.recv (1024) # just eat the question about our password!
                 digest = sha.new (id+passwd).hexdigest ()
                 s.send ("""<iq type="set" id="msg_3">  <query 
xmlns="jabber:iq:auth">  <username>%s</username><digest 
sid='%s'>%s</digest><resource>JabberHelper</resource></query></iq>""" % 
(name, id, digest))
                 return s



if __name__ == '__main__':
         program = JabberClient ('boojum', 'toto')
         #JabberServer ()



More information about the JDev mailing list