Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除';服务器';Django框架中的响应头-V3.0.5_Python_Django_Security_Http Headers - Fatal编程技术网

Python 删除';服务器';Django框架中的响应头-V3.0.5

Python 删除';服务器';Django框架中的响应头-V3.0.5,python,django,security,http-headers,Python,Django,Security,Http Headers,在我开始提问之前,我已经提到了stackoverflow帖子- 请在下面找到中间件代码和settings.py(参考): 中间件.py: class SimpleMiddleware: def __init__(self, get_response): self.get_response = get_response # One-time configuration and initialization. def __call__(self, r

在我开始提问之前,我已经提到了stackoverflow帖子-

请在下面找到中间件代码和settings.py(参考):

中间件.py

class SimpleMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        # One-time configuration and initialization.

    def __call__(self, request):
        response = self.get_response(request)
        response.__setitem__('Server', '')
        return response

设置.py


MIDDLEWARE = [
    ....,
    ....,
    'middleware_demo.middleware.SimpleMiddleware',
]

使用上面的代码,我得到服务器响应,服务器头设置为空字符串,如下所示。这与预期的一样,不会透露服务器头的详细信息:

HTTP/1.1 200 OK
Date: Tue, 21 Apr 2020 12:55:25 GMT
Content-Type: text/html
Server: 
X-Frame-Options: DENY
Content-Length: 16351
X-Content-Type-Options: nosniff
我的目标是完全删除标题,并在middleware.py中尝试了两种相同的方法:

class SimpleMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        # One-time configuration and initialization.

    def __call__(self, request):
        response = self.get_response(request)
        response.__setitem__('Server', '')
        return response

方法1-

方法2-参考stackoverflow博客-

但是响应仍然设置了服务器头,并显示了版本详细信息,如下所示:

HTTP/1.1 200 OK
Date: Tue, 21 Apr 2020 13:00:26 GMT
Server: WSGIServer/0.2 CPython/3.6.5
Content-Type: text/html
X-Frame-Options: DENY
Content-Length: 16351
X-Content-Type-Options: nosniff
我的问题是,为什么服务器头值会被修改,并且在修改时显示为空字符串,但是当头值本身被删除时,我会在响应头中看到它。 我在这里错过了什么


此外,我还尝试将中间件激活行移动到第一个位置和最后一个位置,以防覆盖某些内容。还是相同的问题。

您使用的是Django
runserver
?在这种情况下,我认为默认标题是由设置的,因此您必须查看它,以查看是否可以阻止设置标题。在生产环境中,无论如何都不会使用
runserver
,因此我不必担心在Django中去掉服务器头。例如,如果您使用Nginx/gunicorn部署Django,您将在Nginx配置中配置服务器头。您是否使用Django
runserver
?在这种情况下,我认为默认标题是由设置的,因此您必须查看它,以查看是否可以阻止设置标题。在生产环境中,无论如何都不会使用
runserver
,因此我不必担心在Django中去掉服务器头。例如,如果您使用Nginx/gunicorn部署Django,您将在Nginx配置中配置服务器头。
HTTP/1.1 200 OK
Date: Tue, 21 Apr 2020 13:00:26 GMT
Server: WSGIServer/0.2 CPython/3.6.5
Content-Type: text/html
X-Frame-Options: DENY
Content-Length: 16351
X-Content-Type-Options: nosniff