Flask中获取HTTPS请求的代理IP的原始IP地址

在Flask中获取HTTPS请求的代理IP的原始IP地址,你可以使用X-Forwarded-For HTTP头。这个头可能包含多个IP地址,其中第一个通常是原始的客户端IP。

以下是一个Flask示例代码,展示了如何获取原始的客户端IP地址:

python 复制代码
from flask import Flask, request
 
app = Flask(__name__)
 
@app.route('/')
def index():
    x_forwarded_for = request.headers.get('X-Forwarded-For')
    if x_forwarded_for:
        original_ip = x_forwarded_for.split(',')[0]
    else:
        original_ip = request.remote_addr
    return f"Original IP: {original_ip}"
 
if __name__ == '__main__':
    app.run(debug=True)
相关推荐
Lyn_Li15 分钟前
Kaggle Top 5 | 198只股票、200条数据的金融预测——BattleFin高分方案从零复现
python·kaggle·比赛复盘·金融预测
小九九的爸爸5 小时前
前端想要入门Agent开发,要具备哪些Python基础?
python·agent·ai编程
阿耶同学6 小时前
手把手教你用 LangGraph 搭建三层嵌套 Agent 架构
python·程序员
花酒锄作田1 天前
Pydantic校验配置文件
python
hboot1 天前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi1 天前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi1 天前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽1 天前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
用户8358086187911 天前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python
Warson_L2 天前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python