How to view the HTTP headers, response code and html content using urllib3 in Python?

Viewed 1777

I am interested in retrieving the response code, body and HTTP headers using urllib3. The previous code which I had written was in Python 2 and now I have to rewrite it for Python 3.

import urllib3

http = urllib3.PoolManager();
response = http.request('GET', 'http://192.168.43.131:8000')
print(response)

I tried different sources, but can someone point me in the right direction of the give a few pointers?

2 Answers

I think you mean this:

import json

import urllib3

http = urllib3.PoolManager()
response = http.request('GET', 'http://192.168.43.131:8000')
print(response.status)
print(response.headers)
print(json.loads(response.data.decode('utf-8'))) # body

doc: https://urllib3.readthedocs.io/en/latest/user-guide.html

Related