杰瑞科技汇

Python default host是什么?如何配置?

Excellent question. The answer depends on the context, as "Python" doesn't run a server by itself. The term "host" can refer to either the network host where a server is running or the default host a web server binds to.

Python default host是什么?如何配置?-图1
(图片来源网络,侵删)

Let's break it down by the most common scenarios.

For Python's Built-in Web Servers (Development)

When you use Python's simple, built-in HTTP server for development, the default behavior is:

  • Default Network Host: 0.0.0
  • Default Port: 8000

What does 0.0.0 mean?

This is the most important part of the answer. 0.0.0 is a special address that means "all available interfaces" or "listen on every network interface."

  • On your local machine: If you run python -m http.server, it will be accessible from:

    Python default host是什么?如何配置?-图2
    (图片来源网络,侵删)
    • http://localhost:8000 (via the loopback interface)
    • http://127.0.0.1:8000 (via the loopback interface)
    • http://YOUR_LOCAL_IP:8000 (e.g., http://192.168.1.10:8000 via your Wi-Fi or Ethernet)
  • On a server: If you run a server (like Flask or Django with the built-in server) on a cloud machine and bind it to 0.0.0, it will be accessible from the public internet, not just from within the server itself.

How to Change It

You can easily override the default host and port from the command line:

# Listen only on localhost (127.0.0.1) on port 8080
python -m http.server 8080
# Listen on all interfaces (0.0.0.0) on port 9000
python -m http.server 9000 --bind 0.0.0.0
# Listen on a specific IP address on port 3000
python -m http.server 3000 --bind 192.168.1.10

For Web Frameworks (Flask, Django, FastAPI)

These frameworks use Python's built-in wsgiref or wsgiref.simple_server by default for development. Therefore, they inherit the same default behavior.

Flask

By default, the development server binds to 0.0.1 (localhost), which is more restrictive than 0.0.0.

# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
    return "Hello, World!"
if __name__ == '__main__':
    # This defaults to host='127.0.0.1', port=5000
    app.run()

To make it accessible from other machines (like for testing on your phone), you must explicitly set host='0.0.0.0'.

if __name__ == '__main__':
    # Listen on all interfaces
    app.run(host='0.0.0.0', port=5000)

Django

Django's runserver command defaults to 0.0.1:8000.

# Default: runs on http://127.0.0.1:8000/
python manage.py runserver
# To make it accessible from other machines:
python manage.py runserver 0.0.0.0:8000

FastAPI

FastAPI's development server defaults to 0.0.1:8000.

# main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
    return {"Hello": "World"}
# To run with uvicorn:
# uvicorn main:app --reload
# This defaults to host="127.0.0.1", port=8000
# To make it accessible from other machines:
# uvicorn main:app --reload --host 0.0.0.0 --port 8000

For Production (Gunicorn, uWSGI)

In a production environment, you never use the built-in servers. You use a production-grade WSGI server like Gunicorn or uWSGI.

The "default host" here is almost always explicitly configured in a configuration file or command-line arguments. There is no single "Python default" for these.

The critical security principle in production is to never bind to 0.0.0 directly on the WSGI server. Instead, you should:

  1. Bind to 0.0.1 (localhost): Configure your WSGI server (e.g., Gunicorn) to listen only on 0.0.1.
  2. Use a Reverse Proxy: Place a dedicated web server like Nginx or Apache in front of it.
    • Nginx listens on port 80 (HTTP) and 443 (HTTPS) on 0.0.0 to accept traffic from the internet.
    • Nginx then forwards that traffic to the WSGI server running on localhost.

This architecture provides security, load balancing, and performance benefits.

Example Gunicorn Command:

# GOOD for production: Gunicorn listens only on localhost
gunicorn myproject.wsgi:application --bind 127.0.0.1:8000

Summary Table

Context / Tool Default Host Explanation
python -m http.server 0.0.0 Listens on all network interfaces, making it accessible from any local IP.
Flask app.run() 0.0.1 More restrictive, only accessible from localhost by default.
Django runserver 0.0.1 Only accessible from localhost by default.
FastAPI (via uvicorn) 0.0.1 Only accessible from localhost by default.
Production (Gunicorn) None No default. Must be explicitly set. Best practice is 0.0.1 when used behind a reverse proxy like Nginx.

Key Takeaway

The most important distinction is between 0.0.1 and 0.0.0.

  • 0.0.1 (or localhost): Accessible only from the machine itself. This is the default for most web frameworks and is the secure choice for production WSGI servers.
  • 0.0.0: Accessible from any network interface, including other machines on the same network. This is the default for Python's simple server and is useful for development but insecure for production WSGI servers.
分享:
扫描分享到社交APP
上一篇
下一篇