2 # -*- coding: utf-8 -*-
5 Slixmpp: The Slick XMPP Library
6 Copyright (C) 2010 Nathanael C. Fritz
7 This file is part of Slixmpp.
9 See the file LICENSE for copying permission.
15 from optparse
import OptionParser
19 # Python versions before 3.0 do not use UTF-8 encoding
20 # by default. To ensure that Unicode is handled properly
21 # throughout Slixmpp, we will set the default encoding
23 if sys
.version_info
< (3, 0):
24 from slixmpp
.util
.misc_ops
import setdefaultencoding
25 setdefaultencoding('utf8')
30 class PingTest(slixmpp
.ClientXMPP
):
33 A simple Slixmpp bot that will send a ping request
37 def __init__(self
, jid
, password
, pingjid
):
38 slixmpp
.ClientXMPP
.__init
__(self
, jid
, password
)
40 pingjid
= self
.boundjid
.bare
41 self
.pingjid
= pingjid
43 # The session_start event will be triggered when
44 # the bot establishes its connection with the server
45 # and the XML streams are ready for use. We want to
46 # listen for this event so that we we can initialize
48 self
.add_event_handler("session_start", self
.start
, threaded
=True)
50 def start(self
, event
):
52 Process the session_start event.
54 Typical actions for the session_start event are
55 requesting the roster and broadcasting an initial
59 event -- An empty dictionary. The session_start
60 event does not provide any additional
67 rtt
= self
['xep_0199'].ping(self
.pingjid
,
69 logging
.info("Success! RTT: %s", rtt
)
71 logging
.info("Error pinging %s: %s",
73 e
.iq
['error']['condition'])
75 logging
.info("No response from %s", self
.pingjid
)
80 if __name__
== '__main__':
81 # Setup the command line arguments.
84 # Output verbosity options.
85 optp
.add_option('-q', '--quiet', help='set logging to ERROR',
86 action
='store_const', dest
='loglevel',
87 const
=logging
.ERROR
, default
=logging
.INFO
)
88 optp
.add_option('-d', '--debug', help='set logging to DEBUG',
89 action
='store_const', dest
='loglevel',
90 const
=logging
.DEBUG
, default
=logging
.INFO
)
91 optp
.add_option('-v', '--verbose', help='set logging to COMM',
92 action
='store_const', dest
='loglevel',
93 const
=5, default
=logging
.INFO
)
94 optp
.add_option('-t', '--pingto', help='set jid to ping',
95 action
='store', type='string', dest
='pingjid',
98 # JID and password options.
99 optp
.add_option("-j", "--jid", dest
="jid",
101 optp
.add_option("-p", "--password", dest
="password",
102 help="password to use")
104 opts
, args
= optp
.parse_args()
107 logging
.basicConfig(level
=opts
.loglevel
,
108 format
='%(levelname)-8s %(message)s')
111 opts
.jid
= raw_input("Username: ")
112 if opts
.password
is None:
113 opts
.password
= getpass
.getpass("Password: ")
115 # Setup the PingTest and register plugins. Note that while plugins may
116 # have interdependencies, the order in which you register them does
118 xmpp
= PingTest(opts
.jid
, opts
.password
, opts
.pingjid
)
119 xmpp
.register_plugin('xep_0030') # Service Discovery
120 xmpp
.register_plugin('xep_0004') # Data Forms
121 xmpp
.register_plugin('xep_0060') # PubSub
122 xmpp
.register_plugin('xep_0199') # XMPP Ping
124 # If you are working with an OpenFire server, you may need
125 # to adjust the SSL version used:
126 # xmpp.ssl_version = ssl.PROTOCOL_SSLv3
128 # If you want to verify the SSL certificates offered by a server:
129 # xmpp.ca_certs = "path/to/ca/cert"
131 # Connect to the XMPP server and start processing XMPP stanzas.
133 # If you do not have the dnspython library installed, you will need
134 # to manually specify the name of the server if it does not match
135 # the one in the JID. For example, to use Google Talk you would
138 # if xmpp.connect(('talk.google.com', 5222)):
140 xmpp
.process(block
=True)
143 print("Unable to connect.")