http.client.ResponseNotReady: 空闲

http.client.ResponseNotReady: Idle

我正在尝试学习如何使用 http.client 进行编码,但是一个简单的代码以这个错误结束,我不知道该怎么做。

import http.client

conn=http.client.HTTPSConnection('www.google.com')
res=conn.getresponse()
print(res.status,res.reason)

它给我的错误是:

Traceback (most recent call last): File "C:/Users/A/PycharmProjects/untitled/testung.py", line 9, in res=conn.getresponse() File "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1312, in getresponse raise ResponseNotReady(self.__state) http.client.ResponseNotReady: Idle –

来自python官方文档:

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

Source

因此将您的代码更改为此

import http.client

conn=http.client.HTTPSConnection('www.google.com')
conn.request("GET", "/")
res=conn.getresponse()
print(conn)

产出

<http.client.HTTPSConnection object at 0x01DFEEB0>