目前使用 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()
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
})
我无法找到任何关于此类事情的文档,所以如果这个问题看起来不知情,我提前表示歉意,但即使只是一个开始寻找的地方也会很壮观。
最佳答案
您想要子类化 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 header 。您应该使用 HTTP 302 临时重定向。
然后将您的 MyWSGIContainer 实例传递到 HTTPServer,而不是默认的 WSGIContainer。
关于python - 将配置文件中的所有 ip 重定向到静态地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21071380/