Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/Jajcus/pyxmpp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacek Konieczny <jajcus@jajcus.net>2003-06-06 12:40:52 +0400
committerJacek Konieczny <jajcus@jajcus.net>2003-06-06 12:40:52 +0400
commit91bb36727648e0da04c7052fee4eacae9ab6ca49 (patch)
treece1c2a5a82a970645a45f6c794561f105c1f7413 /examples
parent94d924aed12c054f5f48edecec2285e46051642e (diff)
- no more ugly libxml2 hacks
Diffstat (limited to 'examples')
-rw-r--r--examples/stest.py91
-rwxr-xr-xexamples/streamtest.py14
2 files changed, 71 insertions, 34 deletions
diff --git a/examples/stest.py b/examples/stest.py
index 486edf1..b0c2bd1 100644
--- a/examples/stest.py
+++ b/examples/stest.py
@@ -8,18 +8,62 @@ import socket
from pyxmpp import ClientStream,JID,Iq,Presence,Message,StreamError
-class Disconnected(Exception):
- pass
+accounts={
+ u'test': '123',
+ u'kurczę': 'żółtko',
+ };
class Stream(ClientStream):
+ def __init__(self,jid,password=None,server=None,port=5222,
+ auth_methods=["sasl:DIGEST-MD5","digest"],
+ enable_tls=0,require_tls=0):
+ ClientStream.__init__(self,jid,password,server,port,auth_methods,
+ enable_tls,require_tls)
+ self.disconnect_time=time.time()+60
+
def post_auth(self):
- print ":-)"
- self.set_message_handler("normal",self.message_in)
- self.set_message_handler("chat",self.message_in)
+ m=Message(type="chat",to=self.peer,
+ body="You have authenticated with: %r" % (self.auth_method_used))
+ self.send(m)
+ m=Message(type="chat",to=self.peer,body="You will be disconnected in 1 minute.")
+ self.send(m)
+ m=Message(type="chat",to=self.peer,body="Thank you for testing.")
+ self.send(m)
+
+ def idle(self):
+ ClientStream.idle(self)
+ if time.time()>=self.disconnect_time:
+ m=Message(type="chat",to=self.peer,
+ body="Bye." % (self.auth_method_used))
+ self.send(m)
+ self.disconnect()
+
+ def get_password(self,username,realm=None,acceptable_formats=("plain",)):
+ if "plain" in acceptable_formats and accounts.has_key(username):
+ return accounts[username],"plain"
+ return None,None
+
+print "creating socket..."
+sock=socket.socket()
+sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
+sock.bind(("127.0.0.1",5222))
+sock.listen(1)
+
+print "creating stream..."
+s=Stream(JID("localhost"),auth_methods=("sasl:DIGEST-MD5","plain","digest"))
+
+while 1:
+ print "accepting..."
+ s.accept(sock)
+
+ print "processing..."
+ try:
+ s.loop(1)
+ finally:
+ print "closing..."
+ s.close()
+
- def post_disconnect(self):
- raise Disconnected
-
def get_password(self,username,realm=None,acceptable_formats=("plain",)):
if "plain" in acceptable_formats:
if username==u"test":
@@ -28,14 +72,6 @@ class Stream(ClientStream):
return unicode("zieleń","iso-8859-2"),"plain"
return None,None
- def message_in(self,stanza):
- echo=Message(fr=stanza.get_to(),to=stanza.get_from(),
- body=stanza.get_body(), subject=stanza.get_subject())
- self.send(echo)
-
-
-libxml2.debugMemory(1)
-
print "creating socket..."
sock=socket.socket()
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
@@ -43,7 +79,7 @@ sock.bind(("127.0.0.1",5222))
sock.listen(1)
print "creating stream..."
-s=Stream(JID("localhost"),auth_methods=("plain","digest"))
+s=Stream(JID("localhost"),auth_methods=("sasl:DIGEST-MD5","plain","digest"))
while 1:
print "accepting..."
@@ -51,20 +87,7 @@ while 1:
print "processing..."
try:
- try:
- s.loop(1)
- finally:
- print "closing..."
- s.close()
- except KeyboardInterrupt:
- traceback.print_exc(file=sys.stderr)
- break
- except (StreamError,Disconnected),e:
- traceback.print_exc(file=sys.stderr)
-
-libxml2.cleanupParser()
-if libxml2.debugMemory(1) == 0:
- print "OK"
-else:
- print "Memory leak %d bytes" % (libxml2.debugMemory(1))
- libxml2.dumpMemory()
+ s.loop(1)
+ finally:
+ print "closing..."
+ s.close()
diff --git a/examples/streamtest.py b/examples/streamtest.py
new file mode 100755
index 0000000..e12772d
--- /dev/null
+++ b/examples/streamtest.py
@@ -0,0 +1,14 @@
+#!/usr/bin/python
+
+import sys,os
+from pyxmpp import xmlextra
+
+h=xmlextra.StreamHandler()
+r=xmlextra.StreamReader(h)
+print dir(r.reader)
+
+while 1:
+ r.feed(os.read(sys.stdin.fileno(),100))
+
+del r
+