from flask import Flask, request, jsonify
import json

app = Flask(__name__)


@app.route('/')
def index():
    """首页，显示所有请求头"""
    headers = dict(request.headers)
    http_version = request.environ.get('SERVER_PROTOCOL', 'Unknown')
    print (http_version)
    # 控制台输出
    print("=" * 50)
    print(f"请求方法: {request.method}")
    print(f"请求路径: {request.path}")
    print(f"客户端IP: {request.remote_addr}")
    print("请求头:")
    for key, value in headers.items():
        print(f"  {key}: {value}")
    print("=" * 50)

    # 网页返回
    return f"""
    <html>
    <head><title>HTTP Headers</title></head>
    <body>
        <h1>HTTP Headers 信息</h1>
        <p><strong>请求方法:</strong> {request.method}</p>
        <p><strong>请求路径:</strong> {request.path}</p>
        <p><strong>客户端IP:</strong> {request.remote_addr}</p>
        <h2>所有请求头:</h2>
        <pre>{json.dumps(dict(request.headers), indent=2)}</pre>
    </body>
    </html>
    """
    # return "ok"

if __name__ == '__main__':
    app.run(
        host='0.0.0.0',
        port=8000,
        debug=True,
        threaded=True
    )
