解决 TypeError: BlockedIPSMiddleware() takes no arguments
解决 TypeError: BlockedIPSMiddleware() takes no arguments
在定义中间件类的时候出现了这个错误
环境介绍:
python3.8+Django2.2
创建这个中间件类是为了阻止某些IP地址访问你的服务器
就把某人拉入黑名单一样
你的应用下创建 middleware.py 文件
from django.http import HttpResponse
class BlockedIPSMiddleware(object):
"""中间件类"""
# 黑名单IP列表
EXCLUDE_IPS = ['192.168.119.1']
# 中间件函数
def process_view(self, request, view_func, *view_args, **view_kwargs):
"""视图函数调用之前会先调用此函数"""
# 获取对方访问服务器的IP地址
user_ip = request.META['REMOTE_ADDR']
# 进行校验,是否在你的黑名单中
if user_ip in BlockedIPSMiddleware.EXCLUDE_IPS:
# 如果在你的黑名单中,给予禁止
return HttpResponse('<h1>Forbidden</h1>')
在settings.py注册你的中间件类
纳闷 setting.py 里面怎么没有 MIDDLEWARE_CLASSES
干脆直接写在MIDDLEWARE里面吧
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'booktest.middleware.BlockedIPSMiddleware', # 注册中间件类
]
运行服务器是出现了
^C(enzo) victoire@ubuntu:~/enzo_test/test5$ python manage.py runserver 192.168.119.2:8000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
December 29, 2020 - 10:51:44
Django version 2.2.17, using settings 'test5.settings'
Starting development server at http://192.168.119.2:8000/
Quit the server with CONTROL-C.
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/usr/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/home/victoire/.virtualenvs/enzo/lib/python3.8/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/home/victoire/.virtualenvs/enzo/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 137, in inner_run
handler = self.get_handler(*args, **options)
File "/home/victoire/.virtualenvs/enzo/lib/python3.8/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 27, in get_handler
handler = super().get_handler(*args, **options)
File "/home/victoire/.virtualenvs/enzo/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 64, in get_handler
return get_internal_wsgi_application()
File "/home/victoire/.virtualenvs/enzo/lib/python3.8/site-packages/django/core/servers/basehttp.py", line 45, in get_internal_wsgi_application
return import_string(app_path)
File "/home/victoire/.virtualenvs/enzo/lib/python3.8/site-packages/django/utils/module_loading.py", line 17, in import_string
module = import_module(module_path)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/victoire/enzo_test/test5/test5/wsgi.py", line 16, in <module>
application = get_wsgi_application()
File "/home/victoire/.virtualenvs/enzo/lib/python3.8/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
return WSGIHandler()
File "/home/victoire/.virtualenvs/enzo/lib/python3.8/site-packages/django/core/handlers/wsgi.py", line 135, in __init__
self.load_middleware()
File "/home/victoire/.virtualenvs/enzo/lib/python3.8/site-packages/django/core/handlers/base.py", line 37, in load_middleware
mw_instance = middleware(handler)
TypeError: BlockedIPSMiddleware() takes no arguments
发现是这个错误
TypeError: BlockedIPSMiddleware() takes no arguments
我也不知道没有论点是什么意思
拿这个错误去Google下
找到了如下
解决方法:
from django.http import HttpResponse
from django.utils.deprecation import MiddlewareMixin
class BlockedIPSMiddleware(MiddlewareMixin):
EXCLUDE_IPS = ['192.168.119.1']
def process_view(self, request, view_func, *view_args, **view_kwargs):
user_ip = request.META['REMOTE_ADDR']
if user_ip in BlockedIPSMiddleware.EXCLUDE_IPS:
return HttpResponse('<h1>Forbidden</h1>')
运行服务器 成功
^C(enzo) victoire@ubuntu:~/enzo_test/test5$ python manage.py runserver 192.168.119.2:8000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
December 29, 2020 - 10:53:34
Django version 2.2.17, using settings 'test5.settings'
Starting development server at http://192.168.119.2:8000/
Quit the server with CONTROL-C.
测试这个被禁止的ip地址下访问服务器,果然不能访问
自学django,只有网络可以帮我,网上继续查找相关信息
发现:
出现这个错误的原因是 Django2.X 版本取消了中间件类
你可以使用上述方法 继续2.X版本之前的方法使用中间件类
自己网上找资料发现:
这个的案例 在2.X以后的版本中 可以这样使用
在你的应用下创建一个 middleware.py 文件
from django.http import HttpResponse
def blocked_ips(get_response):
def middleware(request):
EXCLUDE_IPS = ['192.168.119.1']
user_ip = request.META['REMOTE_ADDR']
if user_ip in EXCLUDE_IPS:
return HttpResponse('<h1>Forbidden</h1>')
return get_response(request)
return middleware
在settings.py注册你的中间件方法
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'booktest.middleware.blocked_ips', # 注册中间件
]
运行服务器,发现被禁止的IP地址,无法访问本服务器
自学之路,好难,一知半解,稀里糊涂,不知道能走多远。。。
管它能走多远,走就行了
这是我的第三篇笔记 前两篇 都是自己给自己点赞 加油????
本文地址:https://blog.csdn.net/weixin_49509128/article/details/111940657
上一篇: B00015 C++实现的图类
下一篇: 函数与打印