You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wanted to support opening a browser on a random local port, and got it working with a bit of monkeypatching:
server=make_server(…)
importtornado.webimporttornado.httpserverimporttornado.netutilimportwebbrowserclassApplication(tornado.web.Application):
deflisten(self, *args, **kw):
sockets=tornado.netutil.bind_sockets(0, 'localhost')
server=tornado.httpserver.HTTPServer(self)
server.add_sockets(sockets)
pairs= []
forsinsockets:
pairs.append(s.getsockname()[:2])
pairs.sort()
host, port=pairs[0]
if":"inhost:
host=f"[{host}]"url=f"http://{host}:{port}"log.info("Listening on %s", url)
asyncdefopen_browser():
webbrowser.open_new_tab(url)
tornado.ioloop.IOLoop.current().add_callback(open_browser)
returnserver# Monkey patch to be able to start servers in a smoother way that the# defaults of livereloadimportlivereload.serverlivereload.server.web.Application=Applicationserver.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.
The text was updated successfully, but these errors were encountered:
Hello,
I wanted to support opening a browser on a random local port, and got it working with a bit of monkeypatching:
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 theApplication
used by server, and have enough hooks to intervene in the listening bit.The text was updated successfully, but these errors were encountered: