将配置文件中的所有IP重定向到静态地址

2024-06-25 23:21:13 发布

您现在位置:Python中文网/ 问答频道 /正文

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

运行服务器的tornado代码如下:

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()

以及包装器.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
})

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


Tags: fromimportipapp应用程序httpwsgiserver
1条回答
网友
1楼 · 发布于 2024-06-25 23:21:13

您希望将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头的。您应该使用HTTP302临时重定向。你知道吗

然后将MyWSGIContainer实例传递到HTTPServer,而不是缺省的WSGIContainer。你知道吗

相关问题 更多 >