HTTP/2 PYTHON AttributeError: 'NoneType' 对象没有属性 'write'

Posted

技术标签:

【中文标题】HTTP/2 PYTHON AttributeError: \'NoneType\' 对象没有属性 \'write\'【英文标题】:HTTP/2 PYTHON AttributeError: 'NoneType' object has no attribute 'write'HTTP/2 PYTHON AttributeError: 'NoneType' 对象没有属性 'write' 【发布时间】:2020-05-18 23:17:52 【问题描述】:

HTTP/2 是万维网使用的 HTTP 网络协议的主要修订版。 HTTP/2 的主要目标是通过启用完整的请求和响应多路复用来减少延迟,通过有效压缩 HTTP 标头字段来最小化协议开销,并添加对请求优先级和服务器推送的支持

我正在尝试使用 HTTP/2 与客户端一起实现一个简单的服务器,我遵循此处给出的示例 https://python-hyper.org/projects/h2/en/stable/twisted-post-example.html# 但是每次我尝试将文件从客户端发送到服务器时,我都会收到此错误

AttributeError: 'NoneType' object has no attribute 'write'

错误来自这部分代码

def sendFileData(self):
"""
Send some file data on the connection.
"""
# Firstly, check what the flow control window is for stream 1.
window_size = self.conn.local_flow_control_window(stream_id=1)

# Next, check what the maximum frame size is.
max_frame_size = self.conn.max_outbound_frame_size

# We will send no more than the window size or the remaining file size
# of data in this call, whichever is smaller.
bytes_to_send = min(window_size, self.file_size)

# We now need to send a number of data frames.
while bytes_to_send > 0:
    chunk_size = min(bytes_to_send, max_frame_size)
    data_chunk = self.fileobj.read(chunk_size)
    self.conn.send_data(stream_id=1, data=data_chunk)

    bytes_to_send -= chunk_size
    self.file_size -= chunk_size

# We've prepared a whole chunk of data to send. If the file is fully
# sent, we also want to end the stream: we're done here.
if self.file_size == 0:
    self.conn.end_stream(stream_id=1)
else:
    # We've still got data left to send but the window is closed. Save
    # a Deferred that will call us when the window gets opened.
    self.flow_control_deferred = defer.Deferred()
    self.flow_control_deferred.addCallback(self.sendFileData)
This line--> self.transport.write(self.conn.data_to_send())

【问题讨论】:

请提供一个最小的、可重现的示例,否则我们只是在猜测您的代码可能会做什么。错误消息非常不言自明:self.transport 是 None 因此尝试对其调用 write 将失败。如何做到这一点是一个挑战。 ***.com/questions/60031077/… 【参考方案1】:

HTTP/2 使用self.channel 来写响应。所以把self.transport.write()改成self.channel.write()

【讨论】:

以上是关于HTTP/2 PYTHON AttributeError: 'NoneType' 对象没有属性 'write'的主要内容,如果未能解决你的问题,请参考以下文章

Python:获取所有节点的度数,然后在networkx中绘制箱线图

如何从用于 vm 的 Azure Python SDK 获取 OS 磁盘相关的详细信息

使用opencv进行人脸识别时出现属性错误

模块“xlwings”没有属性“书”

如何在 PyQt5 中返回 QlistWidget 中项目的值

HTTP / 2 PYTHON AttributeError:'NoneType'对象没有属性'write'