import sys
import functools
import time

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gio, GLib, Gtk

class GioResolver():
    """
    Asynchronous resolver using GIO. process() method has to be
    called in order to proceed the pending requests.
    """

    def __init__(self):
        super().__init__()
        self.gio_resolver = Gio.Resolver.get_default()

    def start_resolve(self, host):
        resq = self.gio_resolver.lookup_records_async(
            host, Gio.ResolverRecordType.SRV, None, self._on_ready_srv)

    def _on_ready_srv(self, source_object, result):
        try:
            variant_results = source_object.lookup_records_finish(result)
        except GLib.Error as e:
            if e.domain == 'g-resolver-error-quark':
                result_list = []
                print(time.strftime('%H:%M:%S', time.gmtime()), e.message)
                return
            else:
                raise
        print(time.strftime('%H:%M:%S', time.gmtime()), variant_results)

if __name__ == '__main__':
    resolver = GioResolver()

    def clicked(widget):
        resolver.start_resolve('_xmpp-client._tcp.jabber.org')
        resolver.start_resolve('_xmpp-client._tcp.jabber.org')
        resolver.start_resolve('_xmpp-client._tcp.jabber.org')

    win = Gtk.Window()
    win.set_border_width(6)
    win.connect('remove', Gtk.main_quit)
    hbox = Gtk.HBox()
    but = Gtk.Button(' Lookup SRV ')
    hbox.pack_start(but, 0, True, 0)
    but.connect('clicked', clicked)
    win.add(hbox)
    win.show_all()
    Gtk.main()