PyProxy/IP-API/IP-test.py
2025-01-09 14:16:05 +08:00

30 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from flask import Flask, request, render_template_string
app = Flask(__name__)
@app.route('/')
def index():
remote_addr = request.remote_addr
Forwarded_For = request.headers.get('X-Forwarded-For', request.remote_addr)
Real_IP = request.headers.get('X-Real-IP', request.remote_addr)
# 如果X-Forwarded-For包含多个IP地址取第一个
if Forwarded_For and ',' in Forwarded_For:
Forwarded_For = Forwarded_For.split(',')[0].strip()
return render_template_string('''
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IP地址测试接口</title>
</head>
<body>
<h4>IP地址: {{ remote_addr }}</h4>
<h4>X-Forwarded-For: {{ Forwarded_For }}</h4>
<h4>X-Real-IP: {{ Real_IP }}</h4>
</body>
</html>
''', remote_addr=remote_addr, Forwarded_For=Forwarded_For, Real_IP=Real_IP)
if __name__ == '__main__':
app.run(debug=True, port=7193) # 指定端口号为8080