]> git.meshlink.io Git - catta/blob - avahi-python/avahi-bookmarks.in
* replace gtk2reactor by glib2reactor, closing Ticket #10
[catta] / avahi-python / 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, getopt
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 glib2reactor
37     glib2reactor.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 port = 8080
47 address = "127.0.0.1"
48 use_host_names = None
49 domain = "local"
50
51 class AvahiBookmarks(resource.Resource):
52     isLeaf = True
53
54     services = {}
55
56     def __init__(self, use_host_names):
57         resource.Resource.__init__(self)
58
59         self.bus = dbus.SystemBus()
60         self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
61
62         self.version_string = self.server.GetVersionString()
63
64         self.browse_service_type("_http._tcp")
65         self.browse_service_type("_https._tcp")
66         self.browse_service_type("_ftp._tcp")
67
68         if use_host_names is None:
69             try: 
70                 self.use_host_names = self.server.IsNSSSupportAvailable()
71             except:
72                 self.use_host_names = False
73         else:
74             self.use_host_names = use_host_names
75
76     def browse_service_type(self, stype):
77
78         global domain
79
80         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)
81         browser.connect_to_signal('ItemNew', self.new_service)
82         browser.connect_to_signal('ItemRemove', self.remove_service)
83
84     def find_path(self, txt):
85
86         l = avahi.txt_array_to_string_array(txt)
87
88         for k in l:
89             if k[:5] == "path=":
90                 if k[5:].startswith("/"):
91                     return k[5:]
92                 else:
93                     return "/" + k[5:]
94
95         return "/"
96
97     def render_GET(self, request):
98
99         t = '<html><head><title>Zeroconf Bookmarks</title></head><body><h1>Zeroconf Bookmarks</h1>'
100
101         if len(self.services) == 0:
102             t += '<p>Sorry, no web services have been registered on the local LAN.</p>'
103         else:
104             t += '<ul style="padding: 0px; margin: 20px; list-style-type: none">'
105
106             for k, v in self.services.iteritems():
107             
108                 if v[3] == 80:
109                     port = ''
110                 else:
111                     port = ':%i' % v[3]
112
113                 path = self.find_path(v[4])
114                 t += '<li><a href="%s://%s%s%s">%s</a></li>' % (urlproto[k[3]], v[2], port, path, k[2])
115                 
116             t += '</ul>'
117         
118         t += '<hr noshade/><p style="font-size: 8; font-family: sans-serif">Served by %s</p></body></html>' % self.version_string
119
120         return str(t)
121
122
123     def new_service(self, interface, protocol, name, type, domain, flags):
124
125         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))
126
127         if self.use_host_names:
128             h = host
129         else:
130             if aprotocol == avahi.PROTO_INET6:
131                 h = "[" + address + "]"
132             else:
133                 h = address
134
135         self.services[(interface, protocol, name, type, domain)] = (host, aprotocol, h, port, txt)
136
137     def remove_service(self, interface, protocol, name, type, domain):
138         del self.services[(interface, protocol, name, type, domain)]
139
140
141 def usage(retval = 0):
142     print "%s [options]\n" % sys.argv[0]
143     print "   -h --help             Show this help"
144     print "   -p --port PORT        Specify the port to use (default %u)" % port
145     print "   -a --address ADDRESS  Specify the address to bind to (default %s)" % address
146     print "   -H --host-names       Show links with real hostnames"
147     print "   -A --addresses        Show links with numeric IP addresses"
148     print "   -d --domain DOMAIN    Specify the domain to browse" 
149     sys.exit(retval)
150
151 try:
152     opts, args = getopt.getopt(sys.argv[1:], "hp:a:HAd:", ["help", "port=", "address=", "host-names", "addresses", "domain="])
153 except getopt.GetoptError:
154     usage(2)
155
156 for o, a in opts:
157     if o in ("-h", "--help"):
158         usage()
159
160     if o in ("-p", "--port"):
161         port = int(a)
162
163     if o in ("-a", "--address"):
164         address = a
165
166     if o in ("-H", "--host-names"):
167         use_host_names = True
168
169     if o in ("-A", "--addresses"):
170         use_host_names = False
171
172     if o in ("-d", "--domain"):
173         domain = a
174
175 site = server.Site(AvahiBookmarks(use_host_names))
176 reactor.listenTCP(port, site, interface=address)
177
178 print "Now point your web browser to http://%s:%u/!" % (address, port)
179
180 try:
181     reactor.run()
182 except KeyboardInterrupt, k:
183     pass