如何开发Avahi客户端/服务器
我正在尝试使用python开发客户端/服务器解决方案,服务器必须使用Avahi广播服务可用性。我正在使用以下代码来发布服务:
import avahiimport dbus
__all__ = ["ZeroconfService"]
class ZeroconfService:
"""A simple class to publish a network service with zeroconf using
avahi.
"""
def __init__(self, name, port, stype="_http._tcp",
domain="", host="", text=""):
self.name = name
self.stype = stype
self.domain = domain
self.host = host
self.port = port
self.text = text
def publish(self):
bus = dbus.SystemBus()
server = dbus.Interface(
bus.get_object(
avahi.DBUS_NAME,
avahi.DBUS_PATH_SERVER),
avahi.DBUS_INTERFACE_SERVER)
g = dbus.Interface(
bus.get_object(avahi.DBUS_NAME,
server.EntryGroupNew()),
avahi.DBUS_INTERFACE_ENTRY_GROUP)
g.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC,dbus.UInt32(0),
self.name, self.stype, self.domain, self.host,
dbus.UInt16(self.port), self.text)
g.Commit()
self.group = g
def unpublish(self):
self.group.Reset()
def test():
service = ZeroconfService(name="TestService", port=3000)
service.publish()
raw_input("Press any key to unpublish the service ")
service.unpublish()
if __name__ == "__main__":
test()
对于客户端,我正在尝试通过以下方式搜索服务:
# http://avahi.org/wiki/PythonBrowseExampleimport dbus, gobject, avahi
from dbus import DBusException
from dbus.mainloop.glib import DBusGMainLoop
# Looks for iTunes shares
TYPE = "_http._tcp"
def service_resolved(*args):
print 'service resolved'
print 'name:', args[2]
print 'address:', args[7]
print 'port:', args[8]
def print_error(*args):
print 'error_handler'
print args[0]
def myhandler(interface, protocol, name, stype, domain, flags):
print "Found service '%s' type '%s' domain '%s' " % (name, stype, domain)
if flags & avahi.LOOKUP_RESULT_LOCAL:
# local service, skip
pass
server.ResolveService(interface, protocol, name, stype,
domain, avahi.PROTO_UNSPEC, dbus.UInt32(0),
reply_handler=service_resolved, error_handler=print_error)
loop = DBusGMainLoop()
bus = dbus.SystemBus(mainloop=loop)
server = dbus.Interface( bus.get_object(avahi.DBUS_NAME, '/'),
'org.freedesktop.Avahi.Server')
sbrowser = dbus.Interface(bus.get_object(avahi.DBUS_NAME,
server.ServiceBrowserNew(avahi.IF_UNSPEC,
avahi.PROTO_UNSPEC, TYPE, 'local', dbus.UInt32(0))),
avahi.DBUS_INTERFACE_SERVICE_BROWSER)
sbrowser.connect_to_signal("ItemNew", myhandler)
gobject.MainLoop().run()
但是,客户端没有检测到服务何时启动。有什么想法我做错了吗?
回答:
我发现该代码按预期工作。我有防火墙规则阻止avahi相关的发布。
以上是 如何开发Avahi客户端/服务器 的全部内容, 来源链接: utcz.com/qa/397605.html