python - 如何在 Python 中使用 urllib3 查看 HTTP header 、响应代码和 html 内容?

标签 python urllib urllib3

我有兴趣使用 urllib3 检索响应代码、正文和 HTTP header 。我以前编写的代码是用 Python 2 编写的,现在我必须为 Python 3 重写它。

import urllib3

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

我尝试了不同的来源,但有人能给我指出正确的方向吗?

最佳答案

我想你的意思是:

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

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

关于python - 如何在 Python 中使用 urllib3 查看 HTTP header 、响应代码和 html 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60590168/

相关文章:

python - 如何将 Excel 工作表另存为 CSV

Python 新型类和 super 函数

python - 如何使用list创建具有多个值的字典

python - 如何查看下载过程?

python - 当在 urllib 中发生读取时

python - 示例 urllib3 和 python 中的线程

python - 具有多个属性的 Django 查询

Python 和向 urllib 请求添加数据的新方法

python-2.7 - 为 sslv3 问题修补 pyopenssl

python - py2neo.Graph的连接池生命周期 : would the connections be released when the instance is no longer referenced by programmer?