Changed the server's host from localhost to 0.0.0.0#206
Changed the server's host from localhost to 0.0.0.0#206cv-gh wants to merge 5 commits intomicrosoft:mainfrom
localhost to 0.0.0.0#206Conversation
Co-authored-by: cv-gh <8566560+cv-gh@users.noreply.github.com>
Co-authored-by: cv-gh <8566560+cv-gh@users.noreply.github.com>
Co-authored-by: cv-gh <8566560+cv-gh@users.noreply.github.com>
Bind server to 0.0.0.0 for container/cloud compatibility
|
Duplicate #187 |
There was a problem hiding this comment.
Pull request overview
This pull request modifies the server startup configuration in the Agent Framework Python sample to change the host binding from localhost to 0.0.0.0. The intent is to make the server accessible from external network interfaces rather than only from the local machine.
Changes:
- Updated the server host binding from
localhostto0.0.0.0in therun_appcall - Modified the startup log message to indicate the server is listening on all interfaces
|
|
||
| try: | ||
| run_app(app, host="localhost", port=port, handle_signals=True) | ||
| run_app(app, host="0.0.0.0", port=port, handle_signals=True) |
There was a problem hiding this comment.
Binding to 0.0.0.0 unconditionally exposes the server to all network interfaces, which may pose a security risk for local development. Other samples in this repository (python/openai/sample-agent/host_agent_server.py:303-304 and python/google-adk/sample-agent/main.py:61) use conditional binding based on environment detection (e.g., checking for WEBSITE_HOSTNAME or WEBSITE_SITE_NAME environment variables to determine if running in Azure). Consider adopting the same pattern to bind to 0.0.0.0 only when deployed to production environments, and use localhost for local development.
| print(f"📚 Endpoint: http://localhost:{port}/api/messages") | ||
| print(f"❤️ Health: http://localhost:{port}/api/health\n") |
There was a problem hiding this comment.
The log messages on lines 336-337 still reference "http://localhost:{port}" for the endpoint and health URLs, which is inconsistent with the server now listening on 0.0.0.0. When the server binds to 0.0.0.0, clients should connect using the actual hostname or IP address, not localhost. Consider updating these messages to reflect the actual accessible URLs, or if following the conditional binding pattern suggested above, display the appropriate URL based on whether the server is running in production or local development mode.
| print(f"📚 Endpoint: http://localhost:{port}/api/messages") | |
| print(f"❤️ Health: http://localhost:{port}/api/health\n") | |
| print(f"📚 Endpoint: http://<host>:{port}/api/messages") | |
| print(f"❤️ Health: http://<host>:{port}/api/health\n") |
|
|
||
| try: | ||
| run_app(app, host="localhost", port=port, handle_signals=True) | ||
| run_app(app, host="0.0.0.0", port=port, handle_signals=True) |
There was a problem hiding this comment.
This change deviates from the established pattern used in other Python samples in the codebase. Specifically, python/openai/sample-agent/host_agent_server.py:303-304 uses conditional binding with "bind_host = '0.0.0.0' if host else 'localhost'" where host is determined by the WEBSITE_HOSTNAME environment variable, and python/google-adk/sample-agent/main.py:61 uses "host = '0.0.0.0' if isProduction else 'localhost'" where isProduction checks for WEBSITE_SITE_NAME. Consider aligning with this pattern for consistency across the repository.
This pull request updates the server startup behavior to improve accessibility for external clients. The server now listens on all network interfaces instead of just localhost, making it reachable from outside the host machine.
Server accessibility improvements:
localhostto0.0.0.0in both the startup log message and therun_appcall, allowing connections from any network interface.