[Python] A local web server listens on 0.0.0.0. What IP interface is the most useful to...

Discussão em 'Python' iniciado por Stack, Julho 7, 2025.

  1. Stack

    Stack Membro Participativo

    I'm writing a general user software (in Python) that also has the option to run a web server (internally using Werkzeug). The web server listens on 0.0.0.0 (and hence on all local addresses). After the web server has started, I want to display one address to the user ("web interface now on 192.168.178.2..." or similar).

    Background: That software is a note-taking and diagramming software. The user is not a web admin. The purpose of the inbuilt web server is to share information with others on the same network (or via internet), like meeting notes or diagrams. A person (like a colleague) can go to that address and see things from the software. In a later implementation there might be a QR code somebody could point their phone at, to see the shared diagrams/texts/etc.

    I can find out what addresses there are by cycling through the IP interfaces:

    ips = ['0.0.0.0']
    for iface in netifaces.interfaces():
    addrs = netifaces.ifaddresses(iface)
    inet_addrs = addrs.get(netifaces.AF_INET, [])
    for addr in inet_addrs:
    ip = addr.get('addr')
    if ip:
    ips += [ip]


    Assuming that most normal people don't know about 0.0.0.0, nor 127.0.0.1, how do I decide what to display to the user? What is the most useful for people who don't know anything about networking? Is there a general rule (like "look for '192.168...' and if not found, take the last one", or "always take the first one", ...)? Or should I just write "localhost"?

    (This question is different from Finding local IP addresses using Python's stdlib, because I know how to get the interfaces. Question is, which one to pick and show the user? The question is about user interface design and networking. Is there a heuristic? A common agreement? Is it best to always show them a 192.168... address if there is one? How do I avoid showing docker IPs or VPN bridge devices? Is there a way to tell which addresses are visible in the LAN and which are purely local? Or is it better to skip all this and just show "localhost" instead, letting the user figure stuff out?)

    Continue reading...

Compartilhe esta Página