X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-utils%2Favahi-bookmarks.in;h=49c4e3b9f29224704eae0ea5da3075d498b89c38;hb=5a685a0f05389e671a32b01ef729dbb39cb39020;hp=72f8e44981b945c0242ed4020771713721b621d0;hpb=d76069e946b4e89c828c96340677e40f583080c9;p=catta diff --git a/avahi-utils/avahi-bookmarks.in b/avahi-utils/avahi-bookmarks.in index 72f8e44..49c4e3b 100755 --- a/avahi-utils/avahi-bookmarks.in +++ b/avahi-utils/avahi-bookmarks.in @@ -19,7 +19,7 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 # USA. -import sys +import sys, getopt try: import avahi, gobject, dbus @@ -38,9 +38,15 @@ try: from twisted.internet import reactor from twisted.web import server, resource except ImportError: - print "Sorry, to use this tool you need to install twisted." + print "Sorry, to use this tool you need to install twisted and twisted.web." sys.exit(1) +urlproto = { "_http._tcp" : "http", "_https._tcp" : "https", "_ftp._tcp" : "ftp" } + +port = 8080 +address = "127.0.0.1" +use_host_names = False + class AvahiBookmarks(resource.Resource): isLeaf = True @@ -49,22 +55,33 @@ class AvahiBookmarks(resource.Resource): def __init__(self): resource.Resource.__init__(self) - self.bus = dbus.SystemBus() self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER) self.version_string = self.server.GetVersionString() - self.browser = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "_http._tcp", "local")), avahi.DBUS_INTERFACE_SERVICE_BROWSER) + self.browse_service_type("_http._tcp") + + # Hurrah! if I enable one of the following lines, python segfaults. + #self.browse_service_type("_https._tcp") + #self.browse_service_type("_ftp._tcp") - self.browser.connect_to_signal('ItemNew', self.new_service) - self.browser.connect_to_signal('ItemRemove', self.remove_service) + def browse_service_type(self, stype): + + browser = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, stype, "local")), avahi.DBUS_INTERFACE_SERVICE_BROWSER) + browser.connect_to_signal('ItemNew', self.new_service) + browser.connect_to_signal('ItemRemove', self.remove_service) def find_path(self, txt): - for k in txt: + l = avahi.txt_array_to_string_array(txt) + + for k in l: if k[:5] == "path=": - return k[5:] + if k[5:].startswith("/"): + return k[5:] + else: + return "/" + k[5:] return "/" @@ -85,9 +102,7 @@ class AvahiBookmarks(resource.Resource): port = ':%i' % v[3] path = self.find_path(v[4]) - - - t += '
  • %s
  • ' % (v[2], port, path, k[2]) + t += '
  • %s
  • ' % (urlproto[k[3]], v[2], port, path, k[2]) t += '' @@ -100,21 +115,52 @@ class AvahiBookmarks(resource.Resource): interface, protocol, name, type, domain, host, aprotocol, address, port, txt = self.server.ResolveService(interface, protocol, name, type, domain, avahi.PROTO_UNSPEC) - self.services[(interface, protocol, name, type, domain)] = (host, aprotocol, address, port, txt) + if use_host_names: + h = host + else: + if aprotocol == avahi.PROTO_INET6: + h = "[" + address + "]" + else: + h = address + + self.services[(interface, protocol, name, type, domain)] = (host, aprotocol, h, port, txt) def remove_service(self, interface, protocol, name, type, domain): del self.services[(interface, protocol, name, type, domain)] -port = 8080 +def usage(retval = 0): + print "%s [options]\n" % sys.argv[0] + print " -h --help Show this help" + print " -p --port PORT Specify the port to use (default %u)" % port + print " -a --address ADDRESS Specify the address to bind to (default %s)" % address + print " -H --host-names Show all services, regardless of the type" + sys.exit(retval) + +try: + opts, args = getopt.getopt(sys.argv[1:], "hp:a:H", ["help", "port=", "address=", "host-names"]) +except getopt.GetoptError: + usage(2) -if __name__ == '__main__': - site = server.Site(AvahiBookmarks()) - reactor.listenTCP(port, site) +for o, a in opts: + if o in ("-h", "--help"): + usage() - print "Now point your web browser to http://localhost:%u/!" % port + if o in ("-p", "--port"): + port = int(a) - try: - reactor.run() - except KeyboardInterrupt, k: - pass + if o in ("-a", "--address"): + address = a + + if o in ("-H", "--host-names"): + use_host_names = True + +site = server.Site(AvahiBookmarks()) +reactor.listenTCP(port, site, interface=address) + +print "Now point your web browser to http://%s:%u/!" % (address, port) + +try: + reactor.run() +except KeyboardInterrupt, k: + pass