【问题标题】:Redirect all ips in config file to static address将配置文件中的所有ip重定向到静态地址
【发布时间】:2014-01-12 04:38:04
【问题描述】:

目前使用 Tornado 作为几个 WSGI 应用程序(主要是 Flask 应用程序)的包装器。最近注意到很多黑客攻击,想知道是否可以自动查看某个文件中定义的 IP 列表,然后将所有这些 IP 重定向到一个页面,上面写着“有人使用这个 IP 试图入侵我们的网站,证明你不是机器人,我们会重新允许你的 ip”。

运行服务器的龙卷风代码在这里:

from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from Wrapper.app import application

http_server = HTTPServer(WSGIContainer(application))
http_server.listen(80)
IOLoop.instance().start()

而 wrapper.app 如下:

from werkzeug.wsgi import DispatcherMiddleware
from Splash import splash_app
from SentimentDemo import sentiment_app
from FERDemo import FER_app

application = DispatcherMiddleware(splash_app, {
    '/api/sentiment': sentiment_app,
    '/api/fer': FER_app
})

我无法找到有关此类事情的任何文档,所以如果这个问题似乎不知情,我很抱歉,但即使只是一个开始寻找的地方也会很壮观。

【问题讨论】:

    标签: python flask tornado wsgi


    【解决方案1】:

    你想继承 WSGIContainer 并覆盖它的__call__ 方法。类似的东西

    class MyWSGIContainer(WSGIContainer):
        def __call__(self, request):
            if request.remote_ip in blacklist:
                self.write_redirect()
            else:
                super(MyWSGIContainer, self)(request)
    

    有关编写self.write_redirect() 的一些提示,请查看WSGIContainer here 的代码;您可以看到它如何格式化 HTTP 标头。您应该使用 HTTP 302 临时重定向。

    然后将您的 MyWSGIContainer 实例传递给 HTTPServer,而不是默认的 WSGIContainer。

    【讨论】:

    • 啊,壮观!正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 2021-07-07
    • 2019-04-13
    • 2019-05-28
    • 2021-08-27
    • 1970-01-01
    • 2022-10-19
    • 2012-11-28
    • 1970-01-01
    相关资源
    最近更新 更多