代码之家  ›  专栏  ›  技术社区  ›  phihag

灵活、稳定、便携的服务发现

  •  5
  • phihag  · 技术社区  · 16 年前

    我正在寻找一种方法,让局域网中的客户端在没有任何配置的情况下找到我的服务器应用程序的所有实例。我想使用现有的解决方案,而不是自己动手。就我个人而言,我需要用Python来完成,但我很乐意听到任何其他语言的解决方案。

    那我为什么不使用 avahi OpenSLP 或其他 Zeroconf / SLP

    • 它不得要求超级用户权限,即仅使用端口>1024.
    • 它必须允许在一台机器上提供相同和不同服务类型的多个服务,并且即使启动广告服务器的实例停止或崩溃,也要继续广告这些服务。

    我希望是这样的:

    def registerService(service): # (type, port)
        if listen(multicast, someport):
            if fork() == child:
                services = [service]
                for q in queriesToMe():
                    if q == DISCOVERY:
                        answer(filter(q.criteria, services))
                    elif q == ADVERTISE and q.sender == "localhost":
                        services.append(q.service)
        else:
            advertiseAt("localhost", service)
    
    3 回复  |  直到 4 年前
        1
  •  3
  •   phihag    16 年前

    我编写了一个符合所有这些标准的应用程序/库(目前是Python和CLI接口)。它被称为 minusconf 事实证明,分叉甚至是不必要的。

        2
  •  2
  •   Justin T Conroy david.s    12 年前

    https://twistedmatrix.com/documents/current/core/howto/udp.html#auto3

    from twisted.internet.protocol import DatagramProtocol
    from twisted.internet import reactor
    
    class MulticastPingPong(DatagramProtocol):
        MULTICAST_ADDR = ('228.0.0.5', 8005)
        CMD_PING = "PING"
        CMD_PONG = "PONG"
    
        def startProtocol(self):
            """
            Called after protocol has started listening.
            """
            # Set the TTL>1 so multicast will cross router hops:
            self.transport.setTTL(5)
            # Join a specific multicast group:
            self.transport.joinGroup(self.MULTICAST_ADDR[0])
    
            self.send_alive()
    
        def send_alive(self):
            """
            Sends a multicast signal asking for clients.
            The receivers will reply if they want to be found.
            """
            self.transport.write(self.CMD_PING, self.MULTICAST_ADDR)
    
        def datagramReceived(self, datagram, address):
            print "Datagram %s received from %s" % (repr(datagram), repr(address))
    
            if datagram.startswith(self.CMD_PING):
                # someone publishes itself, we reply that we are here
                self.transport.write(self.CMD_PONG, address)
            elif datagram.startswith(self.CMD_PONG):
                # someone reply to our publish message
                print "Got client: ", address[0], address[1]
    
    
    if __name__ == '__main__':
        reactor.listenMulticast(8005, MulticastPingPong(), listenMultiple=True)
        reactor.run()
    
        3
  •  1
  •   Van Gale    16 年前

    我假设你可以控制客户端应用程序,而不仅仅是服务器应用程序,在这种情况下 Pyro

    灵活:

    固体:

    纯Python,在多个平台上经过良好测试。

    我认为Pyro很轻。也许对于网络命名服务来说,要求一个Python脚本是不切实际的?

    推荐文章