3

I'm trying to learn how to code with http.client but a simple code ends in this error and I don't know what to do.

import http.client

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

and the error it gives me is:

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 –

Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
peiman razavi
  • 102
  • 1
  • 6
  • Your error trace doesn't match your code. Why is get passed as an argument in the getresponse method? – AzyCrw4282 Mar 01 '20 at 15:38
  • you are right i'm sorry i meant to say this `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` – peiman razavi Mar 02 '20 at 05:47
  • Thanks. See my update. – AzyCrw4282 Mar 02 '20 at 14:21

1 Answers1

4

From python official documentation:

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

Source

So changing your code to this

import http.client

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

Outputs

<http.client.HTTPSConnection object at 0x01DFEEB0>
AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35