From f868890cfed17fcd183aa1bea51ca88610c3e99d Mon Sep 17 00:00:00 2001 From: Dan Sully Date: Mon, 3 Apr 2023 10:34:15 -0700 Subject: [PATCH] Support IPv6 addresses for --server-name (#3695) * Support IPv6 addresses for --server-name * Update changelog now that I have a PR number. --------- Co-authored-by: freddyaboulton --- CHANGELOG.md | 2 +- gradio/networking.py | 10 +++++++++- test/test_networking.py | 7 ++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 831ea30cc5..b5e49a3cce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,7 +23,7 @@ No changes to highlight. ## Full Changelog: -No changes to highlight. +- Fixed IPv6 listening to work with bracket [::1] notation. by [@dsully](https://github.com/dsully) in [PR 3695](https://github.com/gradio-app/gradio/pull/3695) ## Contributors Shoutout: diff --git a/gradio/networking.py b/gradio/networking.py index 055f981db0..7bc66dd9a9 100644 --- a/gradio/networking.py +++ b/gradio/networking.py @@ -137,6 +137,14 @@ def start_server( else: path_to_local_server = "http://{}:{}/".format(url_host_name, port) + # Strip IPv6 brackets from the address if they exist. + # This is needed as http://[::1]:port/ is a valid browser address, + # but not a valid IPv6 address, so asyncio will throw an exception. + if server_name.startswith("[") and server_name.endswith("]"): + host = server_name[1:-1] + else: + host = server_name + app = App.create_app(blocks) if blocks.save_to is not None: # Used for selenium tests @@ -144,7 +152,7 @@ def start_server( config = uvicorn.Config( app=app, port=port, - host=server_name, + host=host, log_level="warning", ssl_keyfile=ssl_keyfile, ssl_certfile=ssl_certfile, diff --git a/test/test_networking.py b/test/test_networking.py index 260a8b360f..869ca7f48f 100644 --- a/test/test_networking.py +++ b/test/test_networking.py @@ -4,6 +4,7 @@ import os import urllib import warnings +import pytest from fastapi.testclient import TestClient import gradio as gr @@ -53,13 +54,17 @@ class TestInterfaceErrors: class TestStartServer: - def test_start_server(self): + + # Test IPv4 and IPv6 hostnames as they would be passed from --server-name. + @pytest.mark.parametrize("host", ["127.0.0.1", "[::1]"]) + def test_start_server(self, host): io = Interface(lambda x: x, "number", "number") io.favicon_path = None io.config = io.get_config_file() io.show_error = True io.flagging_callback.setup(gr.Number(), io.flagging_dir) io.auth = None + io.host = host port = networking.get_first_available_port( networking.INITIAL_PORT_VALUE,