Python Client for XMPP

Source: Internet
Author: User

See a lot, not very early not updated, is not support gevent

Finally, Sleekxmpp was chosen.

#!/usr/bin/env python# -*- coding: utf-8 -*-# https://github.com/fritzy/ sleekxmpp/tree/develop/examplesimport sysif  "Threading"  in sys.modules:         print  ("threading module loaded before patching!",   "Xmppclient")         del sys.modules["threading"]from  Gevent import monkey; monkey.patch_all () from gevent.queue import queuefrom  Sleekxmpp.util.misc_ops import setdefaultencoding; setdefaultencoding ("UTF8") import  Sleekxmppimport loggingimport geventclass xmppclient (sleekxmpp. CLIENTXMPP):     def __init__ (self, jid, password, address):         super (xmppclient, self). __init__ (Jid, password)                  self.register_plugin ("xep_0030")  # Service Discovery         self.register_plugin ("xep_0004")  # Data Forms         self.register_plugin ("xep_0060")  # PubSub         self.register_plugin ("xep_0199")  # XMPP Ping                 self.inbox = queue ()          self.address = address         self.job_send = None                 #  Add Event Trigger-start trigger         self.add_event_ Handler ("Session_Start",  self.session_start, threaded=true)          #  Add Event trigger-Receive message trigger &nbsP;       self.add_event_handler ("message",  self.message)                  if self.connect ( Address):             self.process (Block=False)             print  "xmpp connected"              self.init ()          else:            print  " Unable to connect. "     def session_start (self, event):          "" "        Arguments:             event -- an empty dictionary. the session_start& nbsp;                     event does not provide any additional                      data.          "" "        self.send_presence ()         self.get_roster () #          self.send_message (mto=self.target_jid,#                             Mbody= "Ooxx",#                            mtype= "chat")           &nbSp;  def message (self, msg):         print msg ["Type"], msg["Body"], msg["from"], msg["Lang"]#          if msg["type"] in  ("chat",  "normal"):#              msg.reply ("thanks for sending\n% (body) S"  % msg). Send ()         def put_data (self, data):          "" Send Message functions          @param  data:  (target_jid, mtype, msg)          "" "         self.inbox.put (data)              def send_data (self):        while true:       &nBsp;     data = self.inbox.get ()              target_jid, mtype, msg = data             self.send_message (Mto=target_jid, mbody=msg, mtype=mtype)                 def init ( Self):        self.job_send = job_send =  Gevent.spawn (Self.send_data)                  def _exit (Glet):             job_send.unlink (_exit)              Self.disconnect (Wait=false)                  job_send.link (_exit)  &Nbsp;          if __name__ ==  "__main__":     logging.basicconfig (level=logging. error,                         format= "% (levelname) -8s % (message) s")          xmpp = xmppclient ("[email protected]",  "123456",  (" The-xmpp-server-ip ",  5222))     xmpp.put_data ((" [email protected] ", " chat ",   "Ooxx")         xmpp = xmppclient ("[email  Protected] ", " 123456 ",  (" The-xmpp-server-ip ",  5222))     xmpp.put_data ((" [ Email protected] ", " chat ", " Xxoo "))         gevent.wait ( )


Python Client for XMPP

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.