]> git.meshlink.io Git - catta/blob - avahi-utils/avahi-bookmarks.in
* This should solve the avahi-bookmarks and checking for python modules problems...
[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 sys
23
24 try:
25     import avahi, gobject, dbus
26 except ImportError:
27     print "Sorry, to use this tool you need to install Avahi, pygtk and python-dbus."
28     sys.exit(1)
29
30 try:
31     import dbus.glib
32 except ImportError:
33     pass
34
35 try:
36     from twisted.internet import gtk2reactor
37     gtk2reactor.install()
38     from twisted.internet import reactor
39     from twisted.web import server, resource
40 except ImportError:
41     print "Sorry, to use this tool you need to install twisted and twisted.web."
42     sys.exit(1)
43
44 urlproto = { "_http._tcp" : "http",  "_https._tcp" : "https", "_ftp._tcp" : "ftp" }
45
46 class AvahiBookmarks(resource.Resource):
47     isLeaf = True
48
49     services = {}
50
51     def __init__(self):
52         resource.Resource.__init__(self)
53
54         self.bus = dbus.SystemBus()
55         self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
56
57         self.version_string = self.server.GetVersionString()
58
59         self.browse_service_type("_http._tcp")
60
61         # Hurrah! if I enable one of the following lines, python segfaults.
62         #self.browse_service_type("_https._tcp")
63         #self.browse_service_type("_ftp._tcp")
64
65     def browse_service_type(self, stype):
66
67         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)
68         browser.connect_to_signal('ItemNew', self.new_service)
69         browser.connect_to_signal('ItemRemove', self.remove_service)
70
71     def find_path(self, txt):
72
73         l = avahi.txt_array_to_string_array(txt)
74
75         for k in l:
76             if k[:5] == "path=":
77                 if k[5]:
78                     return "/" + k[5:]
79                 else
80                     return k[5:]
81
82         return "/"
83
84     def render_GET(self, request):
85
86         t = '<html><head><title>Zeroconf Bookmarks</title></head><body><h1>Zeroconf Bookmarks</h1>'
87
88         if len(self.services) == 0:
89             t += '<p>Sorry, no web services have been registered on the local LAN.</p>'
90         else:
91             t += '<ul style="padding: 0px; margin: 20px; list-style-type: none">'
92
93             for k, v in self.services.iteritems():
94             
95                 if v[3] == 80:
96                     port = ''
97                 else:
98                     port = ':%i' % v[3]
99
100                 path = self.find_path(v[4])
101
102                 if v[1] == avahi.PROTO_INET6:
103                     ip = "[" + v[2] + "]"
104                 else:
105                     ip = v[2]
106
107                 t += '<li><a href="%s://%s%s%s">%s</a></li>' % (urlproto[k[4]], ip, port, path, k[2])
108                 
109             t += '</ul>'
110         
111         t += '<hr noshade/><p style="font-size: 8; font-family: sans-serif">Served by %s</p></body></html>' % self.version_string
112
113         return t
114
115
116     def new_service(self, interface, protocol, name, type, domain):
117
118         interface, protocol, name, type, domain, host, aprotocol, address, port, txt = self.server.ResolveService(interface, protocol, name, type, domain, avahi.PROTO_UNSPEC)
119
120         self.services[(interface, protocol, name, type, domain)] = (host, aprotocol, address, port, txt)
121
122     def remove_service(self, interface, protocol, name, type, domain):
123         del self.services[(interface, protocol, name, type, domain)]
124
125
126 port = 8080
127
128 if __name__ == '__main__':
129     site = server.Site(AvahiBookmarks())
130     reactor.listenTCP(port, site, interface="127.0.0.1")
131
132     print "Now point your web browser to http://localhost:%u/!" % port
133
134     try:
135         reactor.run()
136     except KeyboardInterrupt, k:
137         pass