python Flask获取用户的IP地址的代码

python Flask框架下获取用户的IP地址的范例代码,下面的代码包含了html页面和python代码,非常详细,如果你正使用Flask,也可以学习一下最基本的Flask使用方法。

from flask import Flask, render_template, request
# Initialize the Flask application
app = Flask(__name__)
# Default route, print user's IP
@app.route('/')
def index():
  ip = request.remote_addr
  return render_template('index.html', user_ip=ip)
if __name__ == '__main__':
  app.run(
        host="0.0.0.0",
        port=int("80")
  )

html模板代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css"
          rel="stylesheet">
  </head>
  <body>
    <div class="container">
      <div class="header">
        <h3 class="text-muted">How To Get The IP Address Of The User</h3>
      </div>
      <hr/>
      <div>
        You IP address is: <strong>{{user_ip}}</strong>
    <div class="header">
        <h3 class="text-muted">Code to retrieve the IP</h3>
      </div>
      <hr/>
<pre>
from flask import Flask, render_template, request
# Initialize the Flask application
app = Flask(__name__)
# Default route, print user's IP ,http://www.111cn.net
@app.route('/')
def index():
  ip = request.remote_addr
  return render_template('index.html', user_ip=ip)
</pre>
      </div>
    </div>
  </body>
</html>

时间: 2024-08-04 04:53:11

python Flask获取用户的IP地址的代码的相关文章

获取用户真实IP地址代码

获取用户真实IP地址代码 Public Function GetIp(getType)   If getType = 0 then    GetIp = Request.ServerVariables("HTTP_X_FORWARDED_FOR")    if GetIp = "" Then getIp = Request.ServerVariables("REMOTE_ADDR")   ElseIf getType = 1 Then    Ge

PHP获取用户访问IP地址的5种方法

PHP获取用户访问IP地址的5种方法: <?php                                                                                                                                 //方法1: $ip = $_SERVER["REMOTE_ADDR"]; echo $ip; //方法2: $user_IP = ($_SERVER["HTTP_V

Java中使用HttpRequest获取用户真实IP地址_JSP编程

在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid,nginx等反向代理软件就不能获取到客户端的真实IP地址了. 如果使用了反向代理软件,将http://192.168.1.110:2046/ 的URL反向代理为 http://www.jb51.net / 的URL时,用request.getRemoteAddr()方法获取的IP地址是:127.0.0.1 或 192.168.1.110

Nginx阻止用户代理及nginx获取用户真实IP地址

Nginx阻止用户代理 有些时候,需要阻止某些用户代理访问网站,比如ab,wget,curl等等,这就需要使用到$http_user_agent变量. 修改nginx.conf if ($http_user_agent ~* (Wget|ab) ) {    return 403; }   if ($http_user_agent ~* LWP::Simple|BBBike|wget) {             return 403; } 重启nginx # /usr/local/nginx-

PHP获取用户访问IP地址的5种方法_php技巧

本文实例为大家分享了5种PHP获取用户访问IP地址的方法,供大家参考,具体内容如下 <?php //方法1: $ip = $_SERVER["REMOTE_ADDR"]; echo $ip; //方法2: $user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"]; $user_IP = ($

asp获取用户真实IP地址的方法

 一般的程序都具备获取用户IP地址的命令,但是我们访问网站有两种一种是我们这种普通用户,一种是使用代理IP上网的用户,下面我来介绍介绍.   在asp中获取用户IP地址我们最简单的获取方法就是使用  代码如下   Request.ServerVariables("REMOTE_ADDR") 但是如果用户使用了代理服务器IP地址就不对了,但我们可通过  代码如下   Request.ServerVariables("REMOTE_ADDR") 结果上面的两个获取IP的

【整理】获取用户真实 ip 地址的 nginx 相关配置

nginx 为实现反向代理的需求增加了一个 ngx_http_proxy_module 模块.其中 proxy_set_header 指令就是该模块需要读取的配置.        HTTP header 中的 Host 含义为所请求的目的主机名.当 nginx 作为反向代理使用,而后端真实 web 服务器设置有类似 防盗链功能 ,或者根据 HTTP header 中的 Host 字段来进行 路由 或 过滤 功能的话,若作为反向代理的 nginx 不重写请求头中的 Host 字段,将会导致请求失败

获取用户真实ip地址的方法

一.没有使用代理服务器的情况: REMOTE_ADDR = 您的 IP HTTP_VIA = 没数值或不显示 HTTP_X_FORWARDED_FOR = 没数值或不显示   二.使用透明代理服务器的情况:Transparent Proxies REMOTE_ADDR = 最后一个代理服务器 IP HTTP_VIA = 代理服务器 IP HTTP_X_FORWARDED_FOR = 您的真实 IP ,经过多个代理服务器时,这个值类似如下:203.98.182.163, 203.98.182.16

LNAMP架构中后端Apache获取用户真实IP地址的2种方法_Linux

一.Nginx反向代理配置: 1.虚拟主机配置 复制代码 代码如下: location / {    try_files $uri @apache;} location @apache {internal;    proxy_pass http://127.0.0.1:8080;    include proxy.conf;} location ~ .*\.(php|php5)?$  {    proxy_pass http://127.0.0.1:8080;    include proxy.