gpt4 book ai didi

python - 为什么无法在 Python 中获取 Response?

转载 作者:可可西里 更新时间:2023-11-01 17:34:23 25 4
gpt4 key购买 nike

我在 python.org 中阅读了示例 http.client

import http.client
conn = http.client.HTTPSConnection("www.python.org")
conn.request("GET", "/")
r1 = conn.getresponse()
print(r1.status, r1.reason)
data1 = r1.read() # This will return entire content.
# The following example demonstrates reading data in chunks.
conn.request("GET", "/")
r1 = conn.getresponse()

没问题,但下面的代码不是:

 import http.client
conn = http.client.HTTPSConnection("www.python.org")
conn.request("GET", "/")
r1 = conn.getresponse()
# The different here
conn.request("GET", "/")
r1 = conn.getresponse()
Error: http.client.ResponseNotReady: Request-sent

我听不懂。

最佳答案

来自python官方文档:

HTTPConnection.getresponse() Should be called after a request is sent to get the response from the server. Returns an HTTPResponse instance.

Note: that you must have read the whole response before you can send a new request to the server.

Source

正如您从注释中注意到的那样,您必须在发送请求之前阅读整个响应,这一切都不同......

您可以查看下面的使用完整链接以获取有关 httplib 的更多信息:

关于python - 为什么无法在 Python 中获取 Response?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36050868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com