在SAE上基于Django搭建的Web工程有时需要禁止来自某些特定IP地址的访问请求。

例如一个为搭建在SAE的其他项目提供服务的内部工程,可以设置为只允许SAE内部的IP地址访问,从而提高项目的安全性。

要修改SAE Django工程的访问规则,需要变更工程的WSGI配置文件。

通过向WSGI配置文件添加中间件,可以根据客户端请求信息的IP地址、User-Agent,Referer等属性对访问请求进行过滤。

SAE Django工程根目录1/下的index.wsgi的路由配置源码如下:

#Router
import sae
from mysite import wsgi application = sae.create_wsgi_app(wsgi.application)

1/mysite/wsgi.py源码如下:

#encoding=utf8
"""
WSGI config for mysite project. This module contains the WSGI application used by Django's development server
and any production WSGI deployments. It should expose a module-level variable
named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover
this application via the ``WSGI_APPLICATION`` setting. Usually you will have the standard Django WSGI application here, but it also
might make sense to replace the whole Django WSGI application with a custom one
that later delegates to the Django one. For example, you could introduce WSGI
middleware here, or combine a Django application with an application of another
framework. """
import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") # This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application() # Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication
# application = HelloWorldApplication(application) import django.core.handlers.wsgi
_application = django.core.handlers.wsgi.WSGIHandler() def application(environ, start_response):
content = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>您访问的网站不存在 - Sina App Engine</title><style type="text/css">body{background-color:#fff}.kc{font-size:16px;text-decoration:none;font-family:"新宋体";color:#333}.ed{width:800px;margin:100px auto}.ec{width:800px;height:207px;margin:10px 0;background-color:#69bee6;-moz-border-radius:10px;-webkit-border-radius:10px;border-radius:10px;color:#fff}.et{margin-left:200px;font-size:30px;font-weight:bolder;font-family:"黑体";padding-top:65px}.ex{margin-left:200px;font-size:12px;padding-top:10px}.ex a{color:#fff}.fr{float:right}.fl{float:left}.bt{font-family:Arial;font-size:12px;color:#666}.bt a{color:#666;text-decoration:none}.bt a:hover{color:#69bee6;text-decoration:underline}.ei{margin-left:10px;margin-top:50px}</style></head><body><div class="ed"><div class="ec"><img src="http://lib.sinaapp.com/error_img.gif" class="fl ei"/><div class="et">您访问的网站不存在<br /></div><div class="ex">请检查您所输入的网址是否有误</div></div><div class="fl bt">页面将在 <span id="showtext"></span>秒 内跳转至 <a href="http://yunshangdian.com/?c=wiki&page=intro"> 新浪云商店 </a></div><div class="fr bt"><a href="http://www.sinaapp.com">Powered By SinaAppEngine</a></div></div><script type="text/javascript">/*<![CDATA[*/(function(){var h=10,g=document.getElementById("showtext");this.a=setInterval(function(){this.d()},1000);g.innerHTML=h;this.d=function(){0<h?g.innerHTML=h--:(clearInterval(this.a),window.location.href="http://yunshangdian.com/?c=wiki&page=intro")};this.b=setInterval(function(){this.c()},10);this.c=function(){var d=document.getElementsByTagName("a"),c;for(c in d){d[c].href&&/jiankongbao/i.test(d[c].href)&&(d[c].style.display="none",clearInterval(this.b))}};var b=document.createElement("script");b.type="text/javascript";b.async=!0;b.src="http://exp.jiankongbao.com/loadtrace.php?host_id=10667&style=5&type=1";var f=document.getElementsByTagName("script")[0];f.parentNode.insertBefore(b,f)})();/*]]>*/</script></body></html>'
remote_addr = environ.get('REMOTE_ADDR')
if remote_addr and not remote_addr.startswith('10.67'):
start_response('600 domain_not_exists', [('Content-Type', 'text/html; charset=utf-8'),])
return [content]
return _application(environ, start_response)

上述源码对来自SAE外部的(IP地址不以10.67开始)HTTP请求进行过滤,返回信息为600 domain_not_exists。

SAE Django如何禁止外部IP访问的更多相关文章

  1. django基于中间件的IP访问频率控制

    一.中间件的代码 注意:成功时返回的是None,那样才会走视图层,返回httpresponse就直接出去了 import time from django.utils.deprecation impo ...

  2. 踢出某正在访问的用户||永久禁止某IP访问

    转至:https://blog.csdn.net/weixin_34408717/article/details/85527305?utm_medium=distribute.pc_aggpage_s ...

  3. Linux 防火墙设置,禁止某个ip访问

    service  iptables  status        查看防火墙状态 service  iptables  start           开启防火墙 service  iptables  ...

  4. SQL Server不能通过外部IP访问,解决方法

    SQL Server不能通过外部IP访问,解决方法   版本:SQL server 2008 express with tools   打开配置管理器,开启 TCP,右键属性设置TCP端口:   设置 ...

  5. 【转载】 禁止国外IP访问你的网站

    在网站的运维过程中,我们通过网站记录的IP列表记录有时候会发现很多国外的IP的访问,如美国的IP等,而很多的服务器攻击行为的发起点很有可能在国外,此时为了服务器安全的考虑,我们可以考虑禁止国外IP访问 ...

  6. apache禁止使用IP访问的实现方法

    apache禁止访问目录列表 apache禁止访问目录列表对于开发人员来说还是蛮实用的,可以迅速查找根目录下的所有项目,但如果一个挂在互联网上的服务器为了提高安全性就必须禁止访问目录列表. 找到Apa ...

  7. 【转载】 腾讯云通过设置安全组禁止某些IP访问你的服务器

    有时候我们在运维网站的过程中会发现一些漏洞扫描者的IP信息,或者个人爬虫网站的IP信息,此时我们想禁止掉这些IP访问到你的服务器,可以通过腾讯云的安全组功能来设置禁止这些IP访问你的服务器,也可以通过 ...

  8. linux禁止特定ip访问某个端口

    linux禁止特定ip访问某个端口   解决方法: 禁止特定ip访问8501端口的命令0:iptables -I INPUT -s 192.168.0.232 -ptcp --dport 8501 - ...

  9. php禁止个别ip访问网站

    PHP禁止个别IP访问自己的网站,可以看看下面的方法. function get_ip_data(){ $ip=file_get_contents("http://ip.taobao.com ...

随机推荐

  1. 自学Python5.5-面向对象三大基本特征_继承

    自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...

  2. Codeforces Round #581 (Div. 2) B. Mislove Has Lost an Array (贪心)

    B. Mislove Has Lost an Array time limit per test1 second memory limit per test256 megabytes inputsta ...

  3. zencart通过产品id 批量添加推荐产品

    1.修改 admin/featured.php 查找 pre_add_confirmation 将 pre_add_confirmation 与  break; 之间的代码,用下面的代码替换即可 &l ...

  4. 【hdu 6067】Big Integer

    题意 给你一个 \((k-1)\times (n+1)\) 的 \(01\) 矩阵 \(g\),求满足下列条件的 \(k(k\le 10)\) 进制整数的数量: 1. 不超过 \(n\) 位且数的最高 ...

  5. Django学习系列17:在模板中渲染待办事项

    前面提到的问题中在表格中显示多个待办事项 是最后一个容易解决的问题.要编写一个新单元测试,检查模板是否也能显示多个待办事项: lists/tests.py def test_displays_all_ ...

  6. Mongodb中的js语法

    定义一个变量 > var len = 10; For循环 这里的db和data都可以作为对象 save是方法 接收一个临时定义的对象 > for(var i = 0; i < len ...

  7. Centos修改IP的两种方式

    nmtui命令调出修改窗口:nmtui    重启网卡:/etc/init.d/network restart 或者:service network restart 方式二: 进入网络配置文件目录 首 ...

  8. 【hiho1065】全图传送

    题目大意:给定一棵 N 个节点的树,点有点权,边有边权,给定 M 个询问,每次询问距离 U 节点不超过 R 的点集中,点权最大的点的编号是多少,若有相同点权,取编号较小的点. 题解: 发现是多组询问, ...

  9. Visual Studio开发首选!DevExtreme v19.1.6全新来袭

    DevExtreme Complete Subscription是性能最优的 HTML5,CSS 和 JavaScript 移动.Web开发框架,可以直接在Visual Studio集成开发环境,构建 ...

  10. Python语法之垃圾回收机制

    目录 一 引入 二.什么是垃圾回收机制? 三.为什么要用垃圾回收机制? 四.垃圾回收机制原理分析 4.1.什么是引用计数? 4.2.引用计数扩展阅读 一 引入 解释器在执行到定义变量的语法时,会申请内 ...