3 # This file is part of avahi.
5 # avahi is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation; either version 2 of the
8 # License, or (at your option) any later version.
10 # avahi is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
13 # License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with avahi; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 import sys, getopt, os
23 import avahi, gobject, dbus
25 print "Sorry, to use this tool you need to install Avahi and python-dbus."
33 urlproto = { "_http._tcp" : "http", "_https._tcp" : "https", "_ftp._tcp" : "ftp" }
45 def __init__(self, use_host_names):
47 self.bus = dbus.SystemBus()
48 self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
50 self.version_string = self.server.GetVersionString()
52 self.browse_service_type("_http._tcp")
53 self.browse_service_type("_https._tcp")
54 self.browse_service_type("_ftp._tcp")
56 if use_host_names is None:
58 self.use_host_names = self.server.IsNSSSupportAvailable()
60 self.use_host_names = False
62 self.use_host_names = use_host_names
64 def browse_service_type(self, stype):
66 global domain, use_CGI
68 browser = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, stype, domain, dbus.UInt32(0))), avahi.DBUS_INTERFACE_SERVICE_BROWSER)
69 browser.connect_to_signal('ItemNew', self.new_service)
70 browser.connect_to_signal('ItemRemove', self.remove_service)
72 browser.connect_to_signal('AllForNow', self.all_for_now)
74 def find_path(self, txt):
76 l = avahi.txt_array_to_string_array(txt)
80 if k[5:].startswith("/"):
87 def render_html(self):
91 t = '<html><head><title>%s Zeroconf Bookmarks</title></head><body><h1>%s Zeroconf Bookmarks</h1>' % (domain, domain)
93 if len(self.services) == 0:
94 t += '<p>Sorry, no Zeroconf web services have been registered on the %s domain.</p>' % domain
96 t += '<ul style="padding: 0px; margin: 20px; list-style-type: none">'
98 for k, v in self.services.iteritems():
105 path = self.find_path(v[4])
106 t += '<li><a href="%s://%s%s%s">%s</a></li>' % (urlproto[k[3]], v[2], port, path, k[2])
110 t += '<hr noshade/><p style="font-size: 8; font-family: sans-serif">Served by %s</p></body></html>' % self.version_string
115 def new_service(self, interface, protocol, name, type, domain, flags):
117 interface, protocol, name, type, domain, host, aprotocol, address, port, txt, flags = self.server.ResolveService(interface, protocol, name, type, domain, avahi.PROTO_UNSPEC, dbus.UInt32(0))
119 if self.use_host_names:
122 if aprotocol == avahi.PROTO_INET6:
123 h = "[" + address + "]"
127 self.services[(interface, protocol, name, type, domain)] = (host, aprotocol, h, port, txt)
129 def remove_service(self, interface, protocol, name, type, domain):
131 del self.services[(interface, protocol, name, type, domain)]
134 # Only reachable with use_CGI
135 def all_for_now(self):
139 def usage(retval = 0):
141 print "%s [options]\n" % sys.argv[0]
142 print " -h --help Show this help"
143 print " -c --cgi Run as a CGI instead of as a server (default to server"
144 print " unless environment variable GATEWAY_INTERFACE is set)"
145 print " -t --timeout MS Specify the max time for CGI browsing (default %u)" % timeout
146 print " -p --port PORT Specify the port to use (default %u)" % port
147 print " -a --address ADDRESS Specify the address to bind to (default %s)" % address
148 print " -H --host-names Show links with real hostnames"
149 print " -A --addresses Show links with numeric IP addresses"
150 print " -d --domain DOMAIN Specify the domain to browse"
154 opts, args = getopt.getopt(sys.argv[1:], "hct:p:a:HAd:", ["help", "cgi", "port=", "timeout=", "address=", "host-names", "addresses", "domain="])
155 except getopt.GetoptError:
159 if o in ("-h", "--help"):
162 if o in ("-c", "--cgi"):
165 if o in ("-t", "--timeout"):
168 if o in ("-p", "--port"):
171 if o in ("-a", "--address"):
174 if o in ("-H", "--host-names"):
175 use_host_names = True
177 if o in ("-A", "--addresses"):
178 use_host_names = False
180 if o in ("-d", "--domain"):
184 use_CGI = os.environ.has_key("GATEWAY_INTERFACE")
187 cgi = AvahiBookmarks(use_host_names)
189 mainloop = gobject.MainLoop()
190 gobject.timeout_add(timeout, mainloop.quit)
194 except KeyboardInterrupt:
197 print 'Content-type: text/html\n\n' + cgi.render_html()
201 from twisted.internet import glib2reactor
202 glib2reactor.install()
203 from twisted.internet import reactor
204 from twisted.web import server, resource
206 print "Sorry, to use this tool as a server you need to install twisted and twisted.web.\n"
209 class AvahiBookmarksServer(AvahiBookmarks, resource.Resource):
212 def __init__(self, use_host_names):
213 resource.Resource.__init__(self)
214 AvahiBookmarks.__init__(self, use_host_names)
216 def render_GET(self, request):
217 return self.render_html()
219 site = server.Site(AvahiBookmarksServer(use_host_names))
220 reactor.listenTCP(port, site, interface=address)
222 print "Now point your web browser to http://%s:%u/!" % (address, port)
226 except KeyboardInterrupt: