#!@PYTHON@ # -*-python-*- # $Id$ # This file is part of avahi. # # avahi is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2 of the # License, or (at your option) any later version. # # avahi is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public # License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with avahi; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 # USA. import sys try: import avahi, gobject, dbus except ImportError: print "Sorry, to use this tool you need to install Avahi, pygtk and python-dbus." sys.exit(1) try: import dbus.glib except ImportError: pass try: from twisted.internet import gtk2reactor gtk2reactor.install() from twisted.internet import reactor from twisted.web import server, resource except ImportError: print "Sorry, to use this tool you need to install twisted." sys.exit(1) urlproto = { "_http._tcp" : "http", "_https._tcp" : "https", "_ftp._tcp" : "ftp" } class AvahiBookmarks(resource.Resource): isLeaf = True services = {} 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.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") 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): l = avahi.txt_array_to_string_array(txt) for k in l: if k[:5] == "path=": return k[5:] return "/" def render_GET(self, request): t = '
Sorry, no web services have been registered on the local LAN.
' else: t += 'Served by %s
' % self.version_string return t def new_service(self, interface, protocol, name, type, domain): 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) def remove_service(self, interface, protocol, name, type, domain): del self.services[(interface, protocol, name, type, domain)] port = 8080 if __name__ == '__main__': site = server.Site(AvahiBookmarks()) reactor.listenTCP(port, site, interface="127.0.0.1") print "Now point your web browser to http://localhost:%u/!" % port try: reactor.run() except KeyboardInterrupt, k: pass