Skip to content

Cannot discover service - how to debug? #34

@cecchisandrone

Description

@cecchisandrone

I'm exposing a service using Python zeroconf library but I'm not able to discover it in the client written in Go with this library. I'm able to discover the Python service using avahi-browse on Linux, Bonjour Browser for Windows and Discovery for iOS. I've tried to run it on the same machine or on different machines (Linux, Windows), no way. Do you have any hint to debug this issue?
This is the source code I written:

Server:

import logging
import socket
import sys
from time import sleep
import netifaces as ni

from zeroconf import ServiceInfo, Zeroconf

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    if len(sys.argv) > 1:
        assert sys.argv[1:] == ['--debug']
        logging.getLogger('zeroconf').setLevel(logging.DEBUG)
    
    desc = {'path': '/~paulsm/'}

    info = ServiceInfo("_http._tcp.local.",
                       "A web server._http._tcp.local.",
                       socket.inet_aton("192.168.1.101"), 80, 1,10,
                       desc, "webserver.local.")

    zeroconf = Zeroconf()
    print("Registration of a service, press Ctrl-C to exit...")
    zeroconf.register_service(info)
    try:
        while True:
            sleep(0.1)
    except KeyboardInterrupt:
        pass
    finally:
        print("Unregistering...")
        zeroconf.unregister_service(info)
        zeroconf.close()

Client:

package main

import (
	"context"
	"flag"
	"log"
	"time"
	"fmt"
	"github.com/grandcat/zeroconf"
)

func main() {
	// Discover all services on the network (e.g. _workstation._tcp)
	resolver, err := zeroconf.NewResolver(nil)
	if err != nil {
		log.Fatalln("Failed to initialize resolver:", err.Error())
	}

	entries := make(chan *zeroconf.ServiceEntry)
	go func(results <-chan *zeroconf.ServiceEntry) {
		for entry := range results {
			str := fmt.Sprintf("Service: %s - Text: %s - Address: %s - Hostname: %s - Domain: %s", entry.Service, entry.Text, entry.AddrIPv4, entry.HostName, entry.Domain)
			log.Println(str)
		}
		log.Println("No more entries.")
	}(entries)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
	defer cancel()
	err = resolver.Browse(ctx, "_http._tcp", "local.", entries)
	if err != nil {
		log.Fatalln("Failed to browse:", err.Error())
	}
	<-ctx.Done()
}

Thanks for your help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions