TypeError: object() takes no parameters

  File "E:\ENVS\python3_LCM\lib\site-packages\django\core\management\commands\runserver.py", line 137, in inner_run
    handler = self.get_handler(*args, **options)
  File "E:\ENVS\python3_LCM\lib\site-packages\django\contrib\staticfiles\management\commands\runserver.py", line 27, in get_handler
    handler = super().get_handler(*args, **options)
  File "E:\ENVS\python3_LCM\lib\site-packages\django\core\management\commands\runserver.py", line 64, in get_handler
    return get_internal_wsgi_application()
  File "E:\ENVS\python3_LCM\lib\site-packages\django\core\servers\basehttp.py", line 45, in get_internal_wsgi_application
    return import_string(app_path)
  File "E:\ENVS\python3_LCM\lib\site-packages\django\utils\module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "D:\acaonda\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\86187\Desktop\VFC\catalog\catalog\wsgi.py", line 21, in <module>
    application = get_wsgi_application()
  File "E:\ENVS\python3_LCM\lib\site-packages\django\core\wsgi.py", line 13, in get_wsgi_application
    return WSGIHandler()
  File "E:\ENVS\python3_LCM\lib\site-packages\django\core\handlers\wsgi.py", line 136, in __init__
    self.load_middleware()
  File "E:\ENVS\python3_LCM\lib\site-packages\django\core\handlers\base.py", line 36, in load_middleware
    mw_instance = middleware(handler)
TypeError: object() takes no parameters


错误middleware.py 代码:

# Copyright (c) 2017-2018 ZTE, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

import uuid
from onaplogging.mdcContext import MDC

from catalog.pub.config.config import FORWARDED_FOR_FIELDS, SERVICE_NAME


class LogContextMiddleware(object):
    #  the last IP behind multiple proxies,  if no exist proxies
    #  get local host ip.

    def _getLastIp(self, request):

        ip = ""
        try:
            for field in FORWARDED_FOR_FIELDS:
                if field in request.META:
                    if ',' in request.META[field]:
                        parts = request.META[field].split(',')
                        ip = parts[-1].strip().split(":")[0]
                    else:
                        ip = request.META[field].split(":")[0]

            if ip == "":
                ip = request.META.get("HTTP_HOST").split(":")[0]

        except Exception:
            pass

        return ip

    def process_request(self, request):
        # Fetch TRANSACTIONID Id and pass to plugin server
        ReqeustID = request.META.get("HTTP_X_ONAP-RequestID", None)
        if ReqeustID is None:
            ReqeustID = uuid.uuid3(uuid.NAMESPACE_URL, SERVICE_NAME)
            request.META["HTTP_X_ONAP-RequestID"] = ReqeustID
        MDC.put("requestID", ReqeustID)
        # generate the unique  id
        InovocationID = uuid.uuid3(uuid.NAMESPACE_DNS, SERVICE_NAME)
        MDC.put("invocationID", InovocationID)
        MDC.put("serviceName", SERVICE_NAME)
        # access ip
        MDC.put("serviceIP", self._getLastIp(request))

        return None

    def process_response(self, request, response):
        MDC.clear()
        return response


更改后加__init __  , __call__ 方法:

# Copyright (c) 2017-2018 ZTE, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

import uuid
from onaplogging.mdcContext import MDC

from catalog.pub.config.config import FORWARDED_FOR_FIELDS, SERVICE_NAME


class LogContextMiddleware(object):
    #  the last IP behind multiple proxies,  if no exist proxies
    #  get local host ip.
    def __init__(self, get_response):
        self.get_response = get_response

    def _getLastIp(self, request):

        ip = ""
        try:
            for field in FORWARDED_FOR_FIELDS:
                if field in request.META:
                    if ',' in request.META[field]:
                        parts = request.META[field].split(',')
                        ip = parts[-1].strip().split(":")[0]
                    else:
                        ip = request.META[field].split(":")[0]

            if ip == "":
                ip = request.META.get("HTTP_HOST").split(":")[0]

        except Exception:
            pass

        return ip

    def process_request(self, request):
        # Fetch TRANSACTIONID Id and pass to plugin server
        ReqeustID = request.META.get("HTTP_X_ONAP-RequestID", None)
        if ReqeustID is None:
            ReqeustID = uuid.uuid3(uuid.NAMESPACE_URL, SERVICE_NAME)
            request.META["HTTP_X_ONAP-RequestID"] = ReqeustID
        MDC.put("requestID", ReqeustID)
        # generate the unique  id
        InovocationID = uuid.uuid3(uuid.NAMESPACE_DNS, SERVICE_NAME)
        MDC.put("invocationID", InovocationID)
        MDC.put("serviceName", SERVICE_NAME)
        # access ip
        MDC.put("serviceIP", self._getLastIp(request))

        return None

    def process_response(self, request, response):
        MDC.clear()
        return response

    def __call__(self, request):
        self.process_request(request)
        response = self.get_response(request)
        self.process_response(request, response)
        return response

中间件类必须接受一个response参数,就是说必须在中间件类中定义一个__init__函数和一个__call__函数

问题解决。

参考地址:https://blog.csdn.net/weixin_44222965/article/details/86841726

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值