Skip to content

Commit

Permalink
Use ipv4 host if ipv6 is not available (#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwillchen authored Jun 23, 2024
1 parent c6b2323 commit ba2e889
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
3 changes: 2 additions & 1 deletion mesop/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from mesop.server.logging import log_startup
from mesop.server.server import configure_flask_app
from mesop.server.static_file_serving import configure_static_file_serving
from mesop.utils.host_util import get_default_host
from mesop.utils.runfiles import get_runfile_location

FLAGS = flags.FLAGS
Expand Down Expand Up @@ -152,7 +153,7 @@ def main(argv: Sequence[str]):
log_startup(port=port())
logging.getLogger("werkzeug").setLevel(logging.WARN)

flask_app.run(host="::", port=port(), use_reloader=False)
flask_app.run(host=get_default_host(), port=port(), use_reloader=False)


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion mesop/colab/colab_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from mesop.server.logging import log_startup
from mesop.server.server import configure_flask_app
from mesop.server.static_file_serving import configure_static_file_serving
from mesop.utils.host_util import get_default_host


def colab_run(*, port: int = 32123, prod_mode: bool = False):
Expand Down Expand Up @@ -43,7 +44,7 @@ def colab_run(*, port: int = 32123, prod_mode: bool = False):
log_startup(port=port)

def run_flask_app():
flask_app.run(host="::", port=port, use_reloader=False)
flask_app.run(host=get_default_host(), port=port, use_reloader=False)

# Launch Flask in background thread so we don't hog up the main thread
# for regular Colab usage.
Expand Down
5 changes: 4 additions & 1 deletion mesop/server/wsgi_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from mesop.server.logging import log_startup
from mesop.server.server import configure_flask_app
from mesop.server.static_file_serving import configure_static_file_serving
from mesop.utils.host_util import get_default_host


class App:
Expand All @@ -20,7 +21,9 @@ def __init__(self, flask_app: Flask):

def run(self):
log_startup(port=port())
self._flask_app.run(host="::", port=port(), use_reloader=False)
self._flask_app.run(
host=get_default_host(), port=port(), use_reloader=False
)


def create_app(
Expand Down
42 changes: 42 additions & 0 deletions mesop/utils/host_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import socket


def get_default_host() -> str:
"""
Returns the default host (which is externally accessible)
based on the availability of IPv6.
"""
if has_ipv6():
return "::"
return "0.0.0.0"


# Context: https://github.com/urllib3/urllib3/pull/611
def has_ipv6():
"""Returns True if the system can bind an IPv6 address."""
# First, check if Python was compiled with IPv6 support
if not socket.has_ipv6:
return False

sock = None
try:
# Attempt to create an IPv6 socket
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)

# Try to bind to the IPv6 unspecified address
# Note: cannot use IPv6 loopback address: "::1"
# because it doesn't work on Colab, likely due to
# some networking configuration w/ the container.
#
# Using port 0 lets the OS choose an available port
sock.bind(("::", 0))

# If we get here, the bind was successful
return True
except Exception:
# An exception means IPv6 is not supported or cannot be used
return False
finally:
# Ensure the socket is closed, even if an exception occurred
if sock:
sock.close()

0 comments on commit ba2e889

Please sign in to comment.