diff options
Diffstat (limited to 'examples/ping.py')
-rwxr-xr-x | examples/ping.py | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/examples/ping.py b/examples/ping.py index fe4d23a4..1a1c2e94 100755 --- a/examples/ping.py +++ b/examples/ping.py @@ -21,8 +21,8 @@ import sleekxmpp # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input @@ -37,7 +37,7 @@ class PingTest(sleekxmpp.ClientXMPP): def __init__(self, jid, password, pingjid): sleekxmpp.ClientXMPP.__init__(self, jid, password) if pingjid is None: - pingjid = self.jid + pingjid = self.boundjid.bare self.pingjid = pingjid # The session_start event will be triggered when @@ -62,16 +62,18 @@ class PingTest(sleekxmpp.ClientXMPP): """ self.send_presence() self.get_roster() - result = self['xep_0199'].send_ping(self.pingjid, - timeout=10, - errorfalse=True) - logging.info("Pinging...") - if result is False: - logging.info("Couldn't ping.") - self.disconnect() - sys.exit(1) - else: - logging.info("Success! RTT: %s", str(result)) + + try: + rtt = self['xep_0199'].ping(self.pingjid, + timeout=10) + logging.info("Success! RTT: %s", rtt) + except IqError as e: + logging.info("Error pinging %s: %s", + self.pingjid, + e.iq['error']['condition']) + except IqTimeout: + logging.info("No response from %s", self.pingjid) + finally: self.disconnect() |