Support for listening on random port and opening browser without delays

Open
#214 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
python
Domain
backend

Research direction

Start by reviewing the server startup path in livereload.server and the web.Application and Server references shown in the issue. Use the monkeypatch example to understand the requested random-port and browser-opening behavior. Done means the server supports these flows without delays and exposes enough startup hooks for subclassing or customization.

Written by the indexing model from the issue text.

Description

type: feature

Hello,

I wanted to support opening a browser on a random local port, and got it working with a bit of monkeypatching:

        server = make_server(…)
                
        import tornado.web
        import tornado.httpserver
        import tornado.netutil                   
        import webbrowser
                    
        class Application(tornado.web.Application):
            def listen(self, *args, **kw):
                sockets = tornado.netutil.bind_sockets(0, 'localhost')
                server = tornado.httpserver.HTTPServer(self)
                server.add_sockets(sockets)
                pairs = []
                for s in sockets:        
                    pairs.append(s.getsockname()[:2])
                pairs.sort()
                host, port = pairs[0]
                if ":" in host:    
                    host = f"[{host}]"    
                url = f"http://{host}:{port}"    
                log.info("Listening on %s", url)    
        
                async def open_browser():        
                    webbrowser.open_new_tab(url)     
 
                tornado.ioloop.IOLoop.current().add_callback(open_browser)    
                return server

        # Monkey patch to be able to start servers in a smoother way that the
        # defaults of livereload
        import livereload.server
        livereload.server.web.Application = Application
        server.serve()

Integrating this in livereload seems to require some refactoring of how the server is started, and I didn't engage on it. I hope that my monkeypatched code can be a useful reference.

Ideally, I would have liked to be able to subclass Server or the Application used by server, and have enough hooks to intervene in the listening bit.

Dominant language
Python
Stars
1.1k
Forks
149
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from lepture/python-livereload

All issues in lepture/python-livereload

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.