Skip to content

Commit

Permalink
Support listening on multiple addresses.
Browse files Browse the repository at this point in the history
  • Loading branch information
comfyanonymous committed Sep 23, 2024
1 parent 9c41bc8 commit 447da7e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
7 changes: 6 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ def prompt_worker(q, server):
need_gc = False

async def run(server, address='', port=8188, verbose=True, call_on_start=None):
await asyncio.gather(server.start(address, port, verbose, call_on_start), server.publish_loop())
addresses = []
for addr in address.split(","):
addresses.append((addr, port))
await asyncio.gather(server.start_multi_address(addresses, call_on_start), server.publish_loop())


def hijack_progress(server):
Expand Down Expand Up @@ -248,6 +251,8 @@ def startup_server(scheme, address, port):
import webbrowser
if os.name == 'nt' and address == '0.0.0.0':
address = '127.0.0.1'
if ':' in address:
address = "[{}]".format(address)
webbrowser.open(f"{scheme}://{address}:{port}")
call_on_start = startup_server

Expand Down
28 changes: 20 additions & 8 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,9 @@ async def publish_loop(self):
await self.send(*msg)

async def start(self, address, port, verbose=True, call_on_start=None):
await self.start_multi_address([(address, port)], call_on_start=call_on_start)

async def start_multi_address(self, addresses, call_on_start=None):
runner = web.AppRunner(self.app, access_log=None)
await runner.setup()
ssl_ctx = None
Expand All @@ -827,17 +830,26 @@ async def start(self, address, port, verbose=True, call_on_start=None):
keyfile=args.tls_keyfile)
scheme = "https"

site = web.TCPSite(runner, address, port, ssl_context=ssl_ctx)
await site.start()
logging.info("Starting server\n")
for addr in addresses:
address = addr[0]
port = addr[1]
site = web.TCPSite(runner, address, port, ssl_context=ssl_ctx)
await site.start()

if not hasattr(self, 'address'):
self.address = address #TODO: remove this
self.port = port

if ':' in address:
address_print = "[{}]".format(address)
else:
address_print = address

self.address = address
self.port = port
logging.info("To see the GUI go to: {}://{}:{}".format(scheme, address_print, port))

if verbose:
logging.info("Starting server\n")
logging.info("To see the GUI go to: {}://{}:{}".format(scheme, address, port))
if call_on_start is not None:
call_on_start(scheme, address, port)
call_on_start(scheme, self.address, self.port)

def add_on_prompt_handler(self, handler):
self.on_prompt_handlers.append(handler)
Expand Down

0 comments on commit 447da7e

Please sign in to comment.