免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 9370 | 回复: 16
打印 上一主题 下一主题

使用XMPPPY模块与Jabber服务器通信。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-09-15 13:32 |只看该作者 |倒序浏览
转自肥肥世家论坛

Jabber服务器采用开放的XMPP协议,Google Talk也是采用XMPP协议的IM系统。在Python中有一个xmpppy模块支持该协议。也就是说,我们可以通过该模块与Jabber服务器通信,是不是很Cool。下面是一个简单的示例,可使大家对该模块有一个大概的了解。

#导入xmpppy模块
>;>;>; import xmpp
#建立Client实例,debian是我的jabber服务器名,jabber服务器的安装可参考我的Debian学习笔记。
>;>;>; c=xmpp.Client('debian',debug=[])
#连接
>;>;>; c.connect()
'tcp'
#验证
>;>;>; c.auth('yangjing','12345')
'old_auth'
#登入
>;>;>; c.sendInitPresence()
#向ringkee@debian 发送信息
>;>;>; c.send(xmpp.protocol.Message('ringkee@debian ','test message from yangjing'))
'20'
#下面测试信息接收,如果没有信息,则pending_data()为空
>;>;>;c.TCPsocket.pending_data()
[]
#如果有信息,则pending_data()不为空
>;>;>; c.TCPsocket.pending_data()
[<socket._socketobject object at 0xb795beb4>;]
#接收信息
>;>;>; c.TCPsocket.receive()
"<message type='chat' to='yangjing@debian/xmpppy' from='ringkee@debian/Gaim'>;<x xmlns='jabber:event'>;<composing/>;</x>;<body>;message from ringkee@debian</body>;<html xmlns='http://jabber.org/protocol/xhtml-im'>;<body xmlns='http://www.w3.org/1999/xhtml'>;message from <a HREF='mailto:ringkee@debian'>;ringkee@debian</a>;</body>;</html>;</message>;"
#登出
>;>;>; c.disconnect()


现在我正在学习XMPPPY模块和XMPP协议,看可不可以在Zope中集成IM功能。

论坛徽章:
0
2 [报告]
发表于 2005-09-15 13:39 |只看该作者

使用XMPPPY模块与Jabber服务器通信。

试没试过使用xmpppy与gtalk相联呢?没试成功。

论坛徽章:
0
3 [报告]
发表于 2005-09-15 15:16 |只看该作者

使用XMPPPY模块与Jabber服务器通信。

我试过,也是不行。

论坛徽章:
0
4 [报告]
发表于 2005-09-15 15:48 |只看该作者

使用XMPPPY模块与Jabber服务器通信。

出错信息如下:

>;>;>; c=xmpp.Client('gmail.com',debug=[])
>;>;>; c.connect(server=('talk.google.com',5222))
'tls'
>;>;>; c.auth('jims.yang@gmail.com','mypassword')
Traceback (most recent call last):
  File "<stdin>;", line 1, in ?
  File "/usr/lib/python2.3/site-packages/xmpp/client.py", line 192, in auth
    auth.SASL().PlugIn(self)
  File "/usr/lib/python2.3/site-packages/xmpp/client.py", line 71, in PlugIn
    if self.__class__.__dict__.has_key('plugin'): return self.plugin(owner)
  File "/usr/lib/python2.3/site-packages/xmpp/auth.py", line 102, in plugin
    try: self.FeaturesHandler(self._owner.Dispatcher,self._owner.Dispatcher.Stream.features)
  File "/usr/lib/python2.3/site-packages/xmpp/auth.py", line 139, in FeaturesHandler
    sasl_data='%s\x00%s\x00%s'%(self.username+'@'+self._owner.Server,self.username,self.password)
AttributeError: SASL instance has no attribute 'username'

论坛徽章:
0
5 [报告]
发表于 2005-09-15 16:30 |只看该作者

使用XMPPPY模块与Jabber服务器通信。

查过资料,是xmpppy的一个sasl验证的Bug。这里有个patch,但好象patch也有问题:
这句  def auth(self,user,password,resource='',sasl=1):里的sasl=1应该改成sasl=0。

下面是原版patch:

Index: client.py
===================================================================
RCS file: /cvsroot/xmpppy/xmpppy/xmpp/client.py,v
retrieving revision 1.37
diff -u -r1.37 client.py
--- client.py        12 May 2005 09:00:23 -0000        1.37
+++ client.py        24 May 2005 13:27:05 -0000
@@ -184,15 +184,15 @@
         self.connected='tls'
         return self.connected

-    def auth(self,user,password,resource=''):
+    def auth(self,user,password,resource='',sasl=1):   
         """ Authenticate connnection and bind resource. If resource is not provided
             random one or library name used. """
         self._User,self._Password,self._Resource=user,password,resource
         while not self.Dispatcher.Stream._document_attrs and self.Process(): pass
         if self.Dispatcher.Stream._document_attrs.has_key('version') and self.Dispatcher.Stream._document_attrs['version']=='1.0':
             while not self.Dispatcher.Stream.features and self.Process(): pass      # If we get version 1.0 stream the features tag MUST BE presented
-        auth.SASL().PlugIn(self)
-        if self.SASL.startsasl=='not-supported':
+        if sasl: auth.SASL().PlugIn(self)
+        if not sasl or self.SASL.startsasl=='not-supported':
             if not resource: resource='xmpppy'
             if auth.NonSASL(user,password,resource).PlugIn(self):
                 self.connected+='+old_auth'


用了该patch后就可正常连接google talk了。

论坛徽章:
0
6 [报告]
发表于 2005-09-15 16:52 |只看该作者

使用XMPPPY模块与Jabber服务器通信。

我是报Dispatcher找不到

论坛徽章:
0
7 [报告]
发表于 2005-09-15 17:06 |只看该作者

使用XMPPPY模块与Jabber服务器通信。

正常连接过程如下:

>;>;>; import xmpp
>;>;>; c=xmpp.Client('gmail.com',debug=[])
>;>;>; c.connect(server=('talk.google.com',5223))
'tls'
>;>;>; c.auth('ringkee','123456','xmpppy')
'old_auth'
>;>;>; c.sendInitPresence()
>;>;>; c.send(xmpp.protocol.Message('jims.yang@gmail.com','test message from ringkee@gmail'))
'5'
>;>;>;
问题:在我的gaim中可正常收到该信息,但在gaim里却不能向该帐号发信息。下班了,有时间再试一试。

论坛徽章:
0
8 [报告]
发表于 2005-09-15 17:44 |只看该作者

使用XMPPPY模块与Jabber服务器通信。

关注

论坛徽章:
0
9 [报告]
发表于 2005-09-16 12:15 |只看该作者

使用XMPPPY模块与Jabber服务器通信。

上面问题解决,双方帐号认证后就可互相发送信息了。用以下命令向jims.yang@gmail.com申请认证。

>;>;>;c.Roster.Subscribe('jims.yang@gmail.com')

论坛徽章:
0
10 [报告]
发表于 2005-09-16 13:02 |只看该作者

使用XMPPPY模块与Jabber服务器通信。

我是在windows下测试,加上了patch也不行,报下面的错:

Traceback (most recent call last):
  File "txmpp.py", line 5, in ?
    cl.auth(jid.getNode(),'bootbo123', 'conference')
  File "C:\python24\lib\site-packages\xmpp\client.py", line 190, in auth
    while not self.Dispatcher.Stream._document_attrs and self.Process(): pass
AttributeError: Client instance has no attribute 'Dispatcher'

我看到你的版本是1.37,而我的是1.36,你是使用cvs里的版本吗?我再试试。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP