Django 尝试SSE日志推送以及 AssertionError: Hop-by-hop headers not allowed 异常的分析

情况描述

近期计划测试一下django对日志打印的支持,一般都是用websocket的方式,想测试一下SSE (Server-sent events)的服务端推送,更简洁,发现过程中存在报错:

Traceback (most recent call last):
  File "D:\Software\Anaconda3\lib\wsgiref\handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "D:\IDE Projects\Music\venv\lib\site-packages\django\contrib\staticfiles\handlers.py", line 76, in __call__
    return self.application(environ, start_response)
  File "D:\IDE Projects\Music\venv\lib\site-packages\django\core\handlers\wsgi.py", line 142, in __call__
    start_response(status, response_headers)
  File "D:\Software\Anaconda3\lib\wsgiref\handlers.py", line 249, in start_response
    assert not is_hop_by_hop(name),"Hop-by-hop headers not allowed"
AssertionError: Hop-by-hop headers not allowed

部分代码

    response = HttpResponse(content_type='text/event-stream')
    response['Cache-Control'] = 'no-cache'
    # AssertionError: Hop-by-hop headers not allowed
    response['Connection'] = 'keep-alive'
    response['Transfer-Encoding'] = 'chunked'

初步分析

1)报错大致是说Hop-by-hop headers not allowed ,说是HTTP1.0 不支持这个头部

HTTP 首部字段将定义成缓存代理和非缓存代理的行为,分成 2 种类型。

端到端首部(End-to-end Header)
分在此类别中的首部会转发给请求 / 响应对应的最终接收目标,且必须保存在由缓存生成的响应中,另外规 定它必须被转发。

逐跳首部(Hop-by-hop Header)
分在此类别中的首部只对单次转发有效,会因通过缓存或代理而不再转发。HTTP/1.1 和之后版本中,如果要使用 hop-by-hop 首部,需提供 Connection 首部字段。

2)依据堆栈信息,找到对应的判断函数 is_hop_by_hop(name) ,只要头部存在如下的集合元素,就会被判定为 hop_by_hop

# wsgiref\handlers.py
        if __debug__:
            for name, val in headers:
                name = self._convert_string_type(name, "Header name")
                val = self._convert_string_type(val, "Header value")
                assert not is_hop_by_hop(name),"Hop-by-hop headers not allowed"

# handlers\wsgi.py
_hoppish = {
    'connection':1, 'keep-alive':1, 'proxy-authenticate':1,
    'proxy-authorization':1, 'te':1, 'trailers':1, 'transfer-encoding':1,
    'upgrade':1
}.__contains__

def is_hop_by_hop(header_name):
    """Return true if 'header_name' is an HTTP/1.1 "Hop-by-Hop" header"""
    return _hoppish(header_name.lower())

3)于是乎,我把Connection 和 Transfer-Encoding 注释掉,就正常了

附带SSE 在django的使用办法

(一)前端 HTML 部分代码

<p id="info">测试</p>
    <script>
        var need_close = false;

        var eventSource = new EventSource('/sse');

        info_elm = document.getElementById("info");
		// 开启连接的监听
		// 解决重复请求(默认会无限重连) 也可以由后端内容决定是否关闭
        eventSource.onopen = function(){
            if (need_close){
                eventSource.close();
            }
            need_close  = true;
        };

		// 收到事件的监听
        eventSource.onmessage = function(event) {
            // 处理接收到的事件数据
            info_elm.innerText = info_elm.innerText + event.data + '\n';
        };
    </script>

(二)django

### views.py
from django.http import StreamingHttpResponse

def sse(request):
	def read_file(file_name, interval=0.5):
	    """
	    用于读取文件内容,实时读取,没有内容时,通过 interval 控制访问频率,请勿访问大文件
	    :param file_name: 要读取的文件名
	    :param interval: 如果没有读取到内容,停留时间间隔
	    :return: 返回迭代器,可以遍历取出结果
	    """
	    with open(file_name, 'r') as file:
	        while True:
	            line = file.readline()
	            if line:
	                yield line
	            else:
	                time.sleep(interval)

    def event_stream():
    	# 测试读取当前文件
        with open('appname/views.py', encoding='utf-8') as file:
            for line in file:
            	# time.sleep(1)
                yield f'data: {line}\n\n'
    return StreamingHttpResponse(event_stream(), content_type='text/event-stream', headers={'Cache-Control':'no-cache'})

def hello(request):
    return render(request, 'sse.html' )


### urls.py
urlpatterns = [
    path('sse', views.sse),
    path('hello', views.hello),
]

(三) 结果

http://127.0.0.1:8000/sse

能看到只有一个请求,内容是逐步刷新出来的
在这里插入图片描述

http://127.0.0.1:8000/hello

能看到有三个请求,第二个是SSE请求,结束后,EventSource自动拉起了第三个请求,由于在onopen 配置了变量控制,所以后期不会有新的推送进来导致数据重复载入前端
在这里插入图片描述

(四) 不可行方案
如下方案主要是 response.flush() 并不能把数据刷新到客户端,所以这个与直接返回结果没太大区别,也可能是我目前使用方式不对或理解没到位。

官方文档: response.flush() 这个方法使一个 HttpResponse 实例成为一个类似文件的对象

def sse(request):

    response = HttpResponse( content_type='text/event-stream')
    response['Cache-Control'] = 'no-cache'

    for i in range(5):
        response.write(f'data:{i} \n\n')
        response.write('id: {i} ')
        response.write(f'message: {datetime.datetime.now()}')
        response.write('event: 发送数据')
        # 数据发送到客户端
        response.flush()
        time.sleep(1)
    return response

总结

1、发现网上很多给的代码都跑不起来,要么不和预期,终究还是得找官方
2、尝试看看源码,可能能找到问题原因还有一些没接触过的写法
3、可以用while True ,然后将数据读取写入到循环内部,然后通过控制时间间隔来减少推送

参考链接:
HTTP 首部字段详细介绍
Python __debug__内置变量的用法
SSE介绍
W3shcool SSE
关于SSE关闭的问题
EventSource专题

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值