【问题标题】: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 将失败。如何做到这一点是一个挑战。

标签: python twisted http2


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-06-16
    • 1970-01-01
    • 2016-05-17
    • 2017-12-28
    • 2017-10-05
    • 2018-03-17
    • 2019-01-10
    • 2013-08-06
    相关资源
    最近更新 更多