{"id":151,"date":"2018-03-12T11:22:54","date_gmt":"2018-03-12T03:22:54","guid":{"rendered":"http:\/\/www.max-shu.com\/blog\/?p=151"},"modified":"2018-03-12T11:23:13","modified_gmt":"2018-03-12T03:23:13","slug":"%e4%bd%bf%e7%94%a8xmpp%e8%b7%9fgtalk%e5%af%b9%e8%af%9d","status":"publish","type":"post","link":"http:\/\/www.max-shu.com\/blog\/?p=151","title":{"rendered":"\u4f7f\u7528xmpp\u534f\u8bae\u8ddfgtalk\u5bf9\u8bdd"},"content":{"rendered":"<p>\u9996\u5148\u5f00\u901a\u4e24\u4e2agtalk\u5e10\u53f7\uff0c\u76f8\u4e92\u52a0\u4e3a\u597d\u53cb\u3002\u5982xxxxx@gmail.com\u548cyyyyy@gmail.com\u3002<br \/>\n\u7136\u540e\u5b89\u88c5xmpppy-0.5.0rc1\u4ee5\u4e0a\u7248\u672c\u7684xmpp\u5e93\u3002<\/p>\n<p>\u7528xxxxx@gmail.com\u767b\u5f55gtalk\uff0c\u7136\u540e\u8ddfyyyyy@gmail.com\u5bf9\u8bdd\u3002<br \/>\n<b>\u4ee3\u7801\u5982\u4e0b\uff1a<\/b><br \/>\n#!\/usr\/bin\/env python<br \/>\n# -*- coding: utf-8 -*-<\/p>\n<p>import\u00a0 os, sys, string<\/p>\n<p>import time<br \/>\nimport codecs<br \/>\nimport threading<\/p>\n<p>import getpass<br \/>\nimport xmpp<\/p>\n<p>programName=&#8221;<\/p>\n<p>class Xmpp_Msg_Class(threading.Thread):<br \/>\ndef __init__(self, threadName, loginUser, loginPasswd, xmppServer, xmppDomain, xmppPort):<br \/>\nthreading.Thread.__init__(self)<br \/>\nself.name = threadName<\/p>\n<p>self.loginUser = loginUser<br \/>\nself.loginPasswd = loginPasswd<br \/>\nself.xmppServer = xmppServer<br \/>\nself.xmppDomain = xmppDomain<br \/>\nself.xmppPort = xmppPort<\/p>\n<p>self.isQuit = False<\/p>\n<p>self.xmppTo = &#8221;<br \/>\nself.client = None<br \/>\nself.conn = None<br \/>\nself.connOK = False<\/p>\n<p>def __del__(self):<br \/>\ntime.sleep(1)\u00a0\u00a0 # some older servers will not send the message if you disconnect immediately after sending<br \/>\nif not self.client == None:<br \/>\nself.client.disconnect()<\/p>\n<p>def quit(self):<br \/>\nself.isQuit = True<\/p>\n<p>def run(self):<br \/>\nwhile True:<br \/>\nif self.isQuit:<br \/>\nbreak<br \/>\nif self.client == None:<br \/>\nif not self.ConnectToXmppServer():<br \/>\nprint &#8216;ERROR: can not connect to XMPP server.&#8217;<br \/>\nself.client = None<br \/>\ntime.sleep(5)<br \/>\ncontinue<br \/>\nif not self.client.isConnected():<br \/>\nif not self.client.reconnectAndReauth():<br \/>\nprint &#8216;ERROR: can not connect to XMPP server.&#8217;<br \/>\ntime.sleep(5)<br \/>\ncontinue<br \/>\nif self.client.Process(1) == None:<br \/>\nprint &#8216;ERROR: lost connection!&#8217;<br \/>\ntime.sleep(5)<br \/>\ntime.sleep(0.1)<\/p>\n<p>def ConnectToXmppServer(self):<br \/>\nprint &#8216;Create Client: DOMAIN:%s, port:%u&#8217; % (self.xmppDomain, 5222)<br \/>\n#self.client = xmpp.Client(self.xmppDomain, port=5222, debug=[&#8216;always&#8217;])<br \/>\nself.client = xmpp.Client(self.xmppDomain, port=5222, debug=[])<br \/>\nprint &#8216;Create Connect: SERVER:%s, port:%u&#8217; % (self.xmppServer, self.xmppPort)<br \/>\nself.conn = self.client.connect((self.xmppServer, self.xmppPort), proxy=None, secure=None, use_srv=True)<br \/>\nif not self.conn:<br \/>\nprint &#8216;ERROR: Could not connect to %s !&#8217; % (self.xmppServer)<br \/>\nreturn False<br \/>\nprint &#8216;INFO: Connected with &#8216;,self.conn<br \/>\nprint &#8216;Create Auth: USER:%s, PASSWORD:%s&#8217; % (self.loginUser.split(&#8216;@&#8217;)[0], &#8216;******&#8217;)#self.loginPasswd)<br \/>\nauth=self.client.auth(self.loginUser.split(&#8216;@&#8217;)[0], self.loginPasswd, resource=&#8221;, sasl=1)<br \/>\nif not auth:<br \/>\nprint &#8216;ERROR: Could not authenticate with %s !&#8217; % (self.xmppServer)<br \/>\nreturn False<br \/>\nprint &#8216;INFO: Aauthenticated using &#8216;,auth<br \/>\n# &#8230;register some handlers (if you will register them before auth they will be thrown away)<br \/>\nprint &#8216;Register Handle: presence\/iq\/message\/disconnect&#8217;<br \/>\nself.client.RegisterHandler(&#8216;presence&#8217;,self.presenceHandler)<br \/>\nself.client.RegisterHandler(&#8216;iq&#8217;,self.iqHandler)<br \/>\nself.client.RegisterHandler(&#8216;message&#8217;,self.messageHandler)<br \/>\nself.client.RegisterDisconnectHandler(self.disconnectHandler)<br \/>\n# &#8230;become available<br \/>\nprint &#8216;INFO: Online&#8217;<br \/>\nself.client.sendInitPresence(requestRoster=0)\u00a0\u00a0 # you may need to uncomment this for old server<br \/>\nprint &#8220;============&gt; I&#8217;m online &lt;=============&#8221;<br \/>\nself.connOK = True<br \/>\nreturn True<\/p>\n<p>def presenceHandler(self, conn,presence_node):<br \/>\n&#8220;&#8221;&#8221; Handler for playing a sound when particular contact became online &#8220;&#8221;&#8221;<br \/>\nfor targetJID in self.xmppTo:<br \/>\nif presence_node.getFrom().bareMatch(targetJID):<br \/>\nprint &#8216;USER: %s has login!&#8217; % (targetJID)<\/p>\n<p>def iqHandler(self, conn,iq_node):<br \/>\n&#8220;&#8221;&#8221; Handler for processing some &#8220;get&#8221; query from custom namespace&#8221;&#8221;&#8221;<br \/>\nreply=iq_node.buildReply(&#8216;result&#8217;)<br \/>\n# &#8230; put some content into reply node<br \/>\nself.conn.send(reply)<br \/>\nraise NodeProcessed\u00a0 # This stanza is fully processed<\/p>\n<p>def messageHandler(self, conn,mess_node):<br \/>\nprint &#8216;Send message to &lt;%s&gt;, MSG: &#8220;%s&#8221;&#8216; % (str(mess_node.getFrom()), mess_node.getBody().encode(&#8216;utf-8&#8217;))<br \/>\nid = self.client.send(xmpp.protocol.Message(str(mess_node.getFrom()), mess_node.getBody().encode(&#8216;utf-8&#8217;)))<br \/>\nprint &#8216;INFO: Sent message with id &#8216;,id<\/p>\n<p>def disconnectHandler(self, conn,disconn_node):<br \/>\nprint &#8216;INFO: Disconnect!&#8217;<\/p>\n<p>def SendMsg(self, xmppTo, xmppMsg):<br \/>\nself.xmppTo = xmppTo<\/p>\n<p>if not self.connOK:<br \/>\nprint &#8216;ERROR: not connected to XMPP server.&#8217;<br \/>\nreturn<\/p>\n<p>for tojid in self.xmppTo:<br \/>\nprint &#8216;Send message to &lt;%s&gt;, MSG: &#8220;%s&#8221;&#8216; % (tojid, xmppMsg)<br \/>\nid = self.client.send(xmpp.protocol.Message(tojid, xmppMsg))<br \/>\nprint &#8216;INFO: Sent message with id &#8216;,id<br \/>\nif self.client.Process(1) == None:<br \/>\nprint &#8216;ERROR: lost connection!&#8217;<\/p>\n<p>def OutputHelp():<br \/>\nprint &#8216;Use XMPP to send messages to gtalk: &#8216;<br \/>\nprint &#8216;Usage: &#8216; + programName + &#8216; &#8216;<br \/>\nprint &#8216;Example: &#8216; + programName + &#8216; &#8216;<br \/>\nprint &#8216; &#8216;<\/p>\n<p>if __name__ == &#8216;__main__&#8217; :<br \/>\nprogramName = sys.argv[0]<br \/>\nif len(sys.argv) &gt; 1:<br \/>\nOutputHelp()<br \/>\nsys.exit(0)<\/p>\n<p>password = getpass.getpass(prompt=&#8217;Please input the password for &#8220;xxxxx@gmail.com&#8221;: &#8216;).strip()<br \/>\nif password == &#8220;&#8221;:<br \/>\nsys.exit(0)<br \/>\nxmpp_msg = Xmpp_Msg_Class(&#8216;CHART&#8217;, &#8216;xxxxx@gmail.com&#8217;, password, &#8216;talk.google.com&#8217;, &#8216;gmail.com&#8217;, 443)<br \/>\nxmpp_msg.start()<br \/>\ntime.sleep(5)<br \/>\nxmpp_msg.SendMsg([&#8216;yyyyy@gmail.com&#8217;], &#8216;test\u6d4b\u8bd51&#8217;)<br \/>\nwhile True:<br \/>\ninput = raw_input(&#8216;======&gt;&gt;&gt;msg(&#8220;q&#8221; is quit)&gt;&gt;&gt;: &#8216;).strip()<br \/>\nif input == &#8216;q&#8217;:<br \/>\nxmpp_msg.quit()<br \/>\nbreak<br \/>\nif input == &#8221;:<br \/>\ncontinue<br \/>\nxmpp_msg.SendMsg([&#8216;yyyyy@gmail.com&#8217;], input)<br \/>\nxmpp_msg.join()<\/p>\n<p>sys.exit()<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\u5f00\u901a\u4e24\u4e2agtalk\u5e10\u53f7\uff0c\u76f8\u4e92\u52a0\u4e3a\u597d\u53cb\u3002\u5982xxxxx@gmail.com\u548cyyyyy@gmail.com\u3002  &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[116,115,119,118,117],"class_list":["post-151","post","type-post","status-publish","format-standard","hentry","category-14","tag-gtalk","tag-xmpp","tag-119","tag-118","tag-117"],"views":1553,"_links":{"self":[{"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=151"}],"version-history":[{"count":2,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":153,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/151\/revisions\/153"}],"wp:attachment":[{"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}