Created
February 1, 2014 07:24
-
-
Save nooperpudd/8749184 to your computer and use it in GitHub Desktop.
xmpp test the client . via sleekxmpp.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# encoding:utf-8 | |
import logging | |
import sys | |
import logging | |
from optparse import OptionParser | |
from sleekxmpp import ClientXMPP | |
from sleekxmpp.exceptions import IqError, IqTimeout | |
import getpass | |
class EchoBot(ClientXMPP): | |
def __init__(self, jid, password,receive_jid=None,message=None): | |
super(EchoBot,self).__init__( jid, password) | |
self.receive_jid=receive_jid | |
self.msg=message | |
self.add_event_handler("session_start", self.start) | |
self.add_event_handler("message", self.message) | |
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 | |
# If you wanted more functionality, here's how to register plugins: | |
# self.use_ssl=False | |
# self.use_tls=False | |
# Here's how to access plugins once you've registered them: | |
# self['xep_0030'].add_feature('echo_demo') | |
# If you are working with an OpenFire server, you will | |
# need to use a different SSL version: | |
# import ssl | |
# self.ssl_version = ssl.PROTOCOL_SSLv3 | |
def start(self, event): | |
self.send_presence() | |
# | |
try: | |
self.get_roster() | |
except IqError as err: | |
logging.error('There was an error getting the roster') | |
logging.error(err.iq['error']['condition']) | |
self.disconnect() | |
except IqTimeout: | |
logging.error('Server is taking too long to respond') | |
self.disconnect() | |
# self.send_message(mto=self.receive_jid, | |
# mbody=self.msg, | |
# mtype='chat') | |
# self.send_message(mto=self.receive_jid,mbody=self.msg,mtype="chat") | |
# | |
# self.disconnect(wait=True) | |
def message(self, msg): | |
if msg['type'] in ('chat', 'normal'): | |
# recevie message | |
print "收到", msg["body"] | |
msg.reply("感谢发送!\n%(body)s" % msg).send() | |
if __name__ == '__main__': | |
logging.basicConfig(level=logging.INFO, | |
format='%(levelname)-8s %(message)s') | |
optp = OptionParser() | |
# Output verbosity options. | |
optp.add_option('-q', '--quiet', help='set logging to ERROR', | |
action='store_const', dest='loglevel', | |
const=logging.ERROR, default=logging.INFO) | |
optp.add_option('-d', '--debug', help='set logging to DEBUG', | |
action='store_const', dest='loglevel', | |
const=logging.DEBUG, default=logging.INFO) | |
optp.add_option('-v', '--verbose', help='set logging to COMM', | |
action='store_const', dest='loglevel', | |
const=5, default=logging.INFO) | |
# | |
# # JID and password options. | |
optp.add_option("-j", "--jid", dest="jid", | |
help="JID to use") | |
optp.add_option("-p", "--password", dest="password", | |
help="password to use") | |
optp.add_option("-t", "--to", dest="to", | |
help="JID to send the message to") | |
optp.add_option("-m", "--message", dest="message", | |
help="message to send") | |
# | |
opts, args = optp.parse_args() | |
if opts.jid is None: | |
opts.jid = raw_input("Component JID: ") | |
if opts.password is None: | |
opts.password = getpass.getpass("Password: ") | |
if opts.to is None: | |
opts.to=raw_input("send message jid:") | |
if opts.message is None: | |
opts.message=raw_input("message info:") | |
# xmpp = EchoBot('[email protected]', '12345678') | |
# xmpp.register_plugin('xep_0030') # Service Discovery | |
# xmpp.register_plugin('xep_0004') # Data Forms | |
# xmpp.register_plugin('xep_0060') # PubSub | |
# xmpp.register_plugin('xep_0199') # XMPP Ping | |
# xmpp.use_tls=False | |
xmpp = EchoBot(opts.jid, opts.password) | |
if xmpp.connect(): | |
xmpp.process(block=False) | |
# xmpp.send_message(mto=r"10005\[email protected]/Spark 2.6.3",mbody="你好",mtype="chat") | |
xmpp.send_message(mto=opts.to,mbody=opts.message,mtype="chat") | |
print("Done") | |
else: | |
print("Unable to connect.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment