一个python脚本程序,基于jabber.py(http://jabberpy.sourceforge.net/

Gtalk采用了xmpp协议,文本聊天基本上是xmpp的一个子集,同时在连接采用了TLS来作为 安全措施。但是gtalk对标准的jabberd通讯协议做了一些修改.


from GtalkClient import JabberConnection 
jbc =JabberConnection('gmail.com','name','pwd','group') 
while 1: 
	jbc.run() 


name,pwd分别填写gtalk的账号,group的值随便添,是xmpp协议的概念 JabberConnection里面定义了一些事件响应函数,只做了简单的处理,你可以新建一个继承于JabberConnection的子类,重载事件函数.


class JabberConnection: 
''' notificationfunction should be override ''' 
	def loggedIn(self): 
	''' To be overidden ''' 
		print 'LogIn.' 

	def connectionLost(self, reason): 
	''' To be overidden ''' 
		print reason 

	def loginFailure(self, message): 
	''' To be overidden ''' 
		print message 
	def serverGoingDown(self): 
		pass # retrieved buddy presence list 
	def listSynchronized(self): 
		return self.contact_info.items() 
	def contactLoggedIn(self, jid): 
		print '%s log in' % jid 
	def contactStatusChanged(self, jid): 
		print '%s status changed ' % jid 
	def userAddedMe(self, jid): 
	''' user have add me''' 
		print '%s add me' % jid

	def userRemovedMe(self, jid): 
	 ''' To be overidden ''' 
		print '%s remove me' % jid 

	 def gotMessage(self, fromjid, msg): 
	 ''' To be overidden ''' 
	 	print '%s:[MSG]:%s' % (fromjid,msg) 

	 # deliver failed notification 
	 def failedMessage(self, tojid, error): 
	 	''' To be overidden ''' 
	 	print '[Error]:%s' % error 


完整的源代码下载地址: http://lutaf.com/download/gtalk_client_source.zip

本文地址: http://lutaf.com/30.htm 鲁塔弗原创文章,欢迎转载,请附带原文链接