Fix disco browser example to handle errors.
This commit is contained in:
parent
aebcf6ff82
commit
295d23ccf3
1 changed files with 44 additions and 39 deletions
|
@ -15,6 +15,7 @@ import getpass
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
|
||||||
import sleekxmpp
|
import sleekxmpp
|
||||||
|
from sleekxmpp.exceptions import IqError, IqTimeout
|
||||||
|
|
||||||
|
|
||||||
# Python versions before 3.0 do not use UTF-8 encoding
|
# Python versions before 3.0 do not use UTF-8 encoding
|
||||||
|
@ -83,50 +84,54 @@ class Disco(sleekxmpp.ClientXMPP):
|
||||||
self.get_roster()
|
self.get_roster()
|
||||||
self.send_presence()
|
self.send_presence()
|
||||||
|
|
||||||
if self.get in self.info_types:
|
try:
|
||||||
# By using block=True, the result stanza will be
|
if self.get in self.info_types:
|
||||||
# returned. Execution will block until the reply is
|
# By using block=True, the result stanza will be
|
||||||
# received. Non-blocking options would be to listen
|
# returned. Execution will block until the reply is
|
||||||
# for the disco_info event, or passing a handler
|
# received. Non-blocking options would be to listen
|
||||||
# function using the callback parameter.
|
# for the disco_info event, or passing a handler
|
||||||
info = self['xep_0030'].get_info(jid=self.target_jid,
|
# function using the callback parameter.
|
||||||
node=self.target_node,
|
info = self['xep_0030'].get_info(jid=self.target_jid,
|
||||||
block=True)
|
node=self.target_node,
|
||||||
if self.get in self.items_types:
|
block=True)
|
||||||
# The same applies from above. Listen for the
|
elif self.get in self.items_types:
|
||||||
# disco_items event or pass a callback function
|
# The same applies from above. Listen for the
|
||||||
# if you need to process a non-blocking request.
|
# disco_items event or pass a callback function
|
||||||
items = self['xep_0030'].get_items(jid=self.target_jid,
|
# if you need to process a non-blocking request.
|
||||||
node=self.target_node,
|
items = self['xep_0030'].get_items(jid=self.target_jid,
|
||||||
block=True)
|
node=self.target_node,
|
||||||
|
block=True)
|
||||||
|
else:
|
||||||
|
logging.error("Invalid disco request type.")
|
||||||
|
return
|
||||||
|
except IqError as e:
|
||||||
|
logging.error("Entity returned an error: %s" % e.iq['error']['condition'])
|
||||||
|
except IqTimeout:
|
||||||
|
logging.error("No response received.")
|
||||||
else:
|
else:
|
||||||
logging.error("Invalid disco request type.")
|
header = 'XMPP Service Discovery: %s' % self.target_jid
|
||||||
self.disconnect()
|
print(header)
|
||||||
return
|
|
||||||
|
|
||||||
header = 'XMPP Service Discovery: %s' % self.target_jid
|
|
||||||
print(header)
|
|
||||||
print('-' * len(header))
|
|
||||||
if self.target_node != '':
|
|
||||||
print('Node: %s' % self.target_node)
|
|
||||||
print('-' * len(header))
|
print('-' * len(header))
|
||||||
|
if self.target_node != '':
|
||||||
|
print('Node: %s' % self.target_node)
|
||||||
|
print('-' * len(header))
|
||||||
|
|
||||||
if self.get in self.identity_types:
|
if self.get in self.identity_types:
|
||||||
print('Identities:')
|
print('Identities:')
|
||||||
for identity in info['disco_info']['identities']:
|
for identity in info['disco_info']['identities']:
|
||||||
print(' - %s' % str(identity))
|
print(' - %s' % str(identity))
|
||||||
|
|
||||||
if self.get in self.feature_types:
|
if self.get in self.feature_types:
|
||||||
print('Features:')
|
print('Features:')
|
||||||
for feature in info['disco_info']['features']:
|
for feature in info['disco_info']['features']:
|
||||||
print(' - %s' % feature)
|
print(' - %s' % feature)
|
||||||
|
|
||||||
if self.get in self.items_types:
|
if self.get in self.items_types:
|
||||||
print('Items:')
|
print('Items:')
|
||||||
for item in items['disco_items']['items']:
|
for item in items['disco_items']['items']:
|
||||||
print(' - %s' % str(item))
|
print(' - %s' % str(item))
|
||||||
|
finally:
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in a new issue