Densaugeo / uploadserver

Python's http.server extended to include a file upload page

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

New Feature Proposal: Publish Service Info with mDNS

shuangye opened this issue · comments

Although uploadserver is simple enough, some non-computer science people still have trouble using it. E.g., they do not know how to find out a computer's IP address.
Besides, if the host running uploadserver uses the IP address assigned from a DHCP server, it's possible that it changes. It's not so friendly to type the IP address in the web browser's address bar every time.

One solution could be using mDNS to publish the service info. This is the option used by VLC player, and some other mobiles Apps that transfer files wirelessly. Say you want to upload some media files to VLC player on your iPad, without a data cable. VLC player allows you to access a short URL like http://ipad.local to upload files.

The dow side is that we have to use a 3rd-party lib like zeroconf, breaking the simplicity of uploadserver.

    parser.add_argument('--host', '-H', type=str,
        help='Host name used for mDNS publishing')
    ...
    if args.host:
        publish_service(args.host, args.port)

def publish_service(host, port):
    from zeroconf import ServiceInfo, Zeroconf
    domain = ".local"
    if not host.endswith(domain):
        host += domain
    addresses = [i[4][0] for i in socket.getaddrinfo(socket.gethostname(), None)]
    info = ServiceInfo("_http._tcp.local.",
                       "Upload Server._http._tcp.local.",
                       port=port,
                       parsed_addresses=addresses,
                       server=f"{host}.")
    zeroconf = Zeroconf()
    zeroconf.register_service(info)

Run uploadserver with -H option like -H up, and now you can access it with URL http://up.local:8000

At least on my network, accessing http://hostname.local:8000 or http://hostname.lan:8000 already just works without needing any setup (using the computer's regular hostname). Does that not work on your setup?

On my Windows 10 PC, http://hostname:8000 is accessible, but not http://hostname.local:8000 or http://hostname.lan:8000
None is accessible from an iOS device.

That sounds like an issue with your network setup. I think .lan addresses are usually set up by routers, so that may not be available depending on your router, but the others should be.

If you're testing with iOS, note that mobile Safari sometimes behaves strangely when opening .local addresses, especially if they are served over https. I've seen it do things like open a web page and then get stuck in a refresh loop (even with no script on the page). The explanation I've heard for this is that Apple uses .local addresses for some internal OS things, and doesn't expect people to open them in a browser.

Managing network setup is outside uploadserver's scope, so I'm going to close this issue for now.