]> git.meshlink.io Git - catta/blob - avahi-utils/avahi-bookmarks.in
7504349cfa7f954f154568a43e6c83ab0735d725
[catta] / avahi-utils / avahi-bookmarks.in
1 #!@PYTHON@
2 # -*-python-*-
3 # $Id$ 
4
5 # This file is part of avahi.
6 #
7 # avahi is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU Lesser General Public License as
9 # published by the Free Software Foundation; either version 2 of the
10 # License, or (at your option) any later version.
11 #
12 # avahi is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 # License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with avahi; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 # USA.
21
22 import avahi, dbus, gobject, sys
23
24 from twisted.internet import gtk2reactor
25 gtk2reactor.install()
26 from twisted.internet import reactor
27 from twisted.web import server, resource
28
29 try:
30     import dbus.glib
31 except ImportError, e:
32     pass
33
34
35 class AvahiBookmarks(resource.Resource):
36     isLeaf = True
37
38     services = {}
39
40     def __init__(self):
41         resource.Resource.__init__(self)
42
43
44         self.bus = dbus.SystemBus()
45         self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
46
47         self.version_string = self.server.GetVersionString()
48
49         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)
50
51         self.browser.connect_to_signal('ItemNew', self.new_service)
52         self.browser.connect_to_signal('ItemRemove', self.remove_service)
53
54     def find_path(self, txt):
55
56         for k in txt:
57             if k[:5] == "path=":
58                 return k[5:]
59
60         return "/"
61
62     def render_GET(self, request):
63
64         t = '<html><head><title>Zeroconf Bookmarks</title></head><body><h1>Zeroconf Bookmarks</h1>'
65
66         if len(self.services) == 0:
67             t += '<p>Sorry, no web services have been registered on the local LAN.</p>'
68         else:
69             t += '<ul style="padding: 0px; margin: 20px; list-style-type: none">'
70
71             for k, v in self.services.iteritems():
72             
73                 if v[3] == 80:
74                     port = ''
75                 else:
76                     port = ':%i' % v[3]
77
78                 path = self.find_path(v[4])
79
80
81                 t += '<li><a href="http://%s%s%s">%s</a></li>' % (v[2], port, path, k[2])
82                 
83             t += '</ul>'
84         
85         t += '<hr noshade/><p style="font-size: 8; font-family: sans-serif">Served by %s</p></body></html>' % self.version_string
86
87         return t
88
89
90     def new_service(self, interface, protocol, name, type, domain):
91
92         interface, protocol, name, type, domain, host, aprotocol, address, port, txt = self.server.ResolveService(interface, protocol, name, type, domain, avahi.PROTO_UNSPEC)
93
94         self.services[(interface, protocol, name, type, domain)] = (host, aprotocol, address, port, txt)
95
96     def remove_service(self, interface, protocol, name, type, domain):
97         del self.services[(interface, protocol, name, type, domain)]
98
99
100 port = 8080
101
102 if __name__ == '__main__':
103     site = server.Site(AvahiBookmarks())
104     reactor.listenTCP(port, site)
105
106     print "Now point your web browser to http://localhost:%u/!" % port
107
108     try:
109         reactor.run()
110     except KeyboardInterrupt, k:
111         pass