软件测试|Python urllib3库使用指南

发布时间 2023-11-15 17:22:09作者: 霍格沃兹测试开发学社

简介

当涉及到进行网络请求和处理HTTP相关任务时,Python的urllib3库是一个强大且灵活的选择。它提供了一种简单的方式来执行HTTP请求、处理响应和处理连接池,使得与Web服务进行交互变得更加容易。本文将详细介绍如何使用urllib3库进行网络请求。

安装urllib3

首先我们需要安装urllib3库,我们可以直接使用pip命令进行安装,安装命令如下:

pip install urllib3

发起基本的 GET 请求

下面是一个使用urllib3库发起简单的GET请求的例子:

import urllib3

# 创建一个连接池管理器
http = urllib3.PoolManager()

# 发起 GET 请求
url = "https://www.baidu.com"
response = http.request('GET', url)

# 打印响应内容
print(response.data)

--------------------
响应内容如下:
b'<html>\r\n<head>\r\n\t<script>\r\n\t\tlocation.replace(location.href.replace("https://","http://"));\r\n\t</script>\r\n</head>\r\n<body>\r\n\t<noscript><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></noscript>\r\n</body>\r\n</html>'

在这个例子中,我们首先导入了urllib3库,然后创建了一个连接池管理器http。接着,我们使用request方法发起了一个GET请求,并指定了请求的URL。最后,我们打印了响应的内容。

发起带有参数的 GET 请求

如果需要在GET请求中传递查询参数,可以使用fields参数来实现:

import urllib3

http = urllib3.PoolManager()

params = {
    'name': 'theshy',
    'team': 'wbg'
}

url = "https://www.baidu.com"
response = http.request('GET', url, fields=params)

print(response.data)

发起 POST 请求

下面是一个使用urllib3库发起POST请求的例子:

import urllib3

http = urllib3.PoolManager()

data = {
    'name': 'theshy',
    'team': 'wbg'
}

url = "https://www.baidu.com"
response = http.request('POST', url, fields=data)

print(response.data)

在这个例子中,我们使用request方法发起了一个POST请求,并通过fields参数传递了POST请求的数据。

处理响应

urllib3的响应对象提供了许多有用的属性和方法来处理响应数据,例如:

import urllib3

http = urllib3.PoolManager()

url = "https://www.baidu.com"
response = http.request('GET', url)

print("Status code:", response.status)
print("Headers:", response.headers)
print("Data:", response.data)

-----------------
输出内容如下:
Status code: 200
Headers: HTTPHeaderDict({'Accept-Ranges': 'bytes', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'Content-Length': '227', 'Content-Security-Policy': "frame-ancestors 'self' https://chat.baidu.com http://mirror-chat.baidu.com https://fj-chat.baidu.com https://hba-chat.baidu.com https://hbe-chat.baidu.com https://njjs-chat.baidu.com https://nj-chat.baidu.com https://hna-chat.baidu.com https://hnb-chat.baidu.com http://debug.baidu-int.com;", 'Content-Type': 'text/html', 'Date': 'Wed, 09 Aug 2023 08:20:31 GMT', 'P3p': 'CP=" OTI DSP COR IVA OUR IND COM ", CP=" OTI DSP COR IVA OUR IND COM "', 'Pragma': 'no-cache', 'Server': 'BWS/1.1', 'Set-Cookie': 'BD_NOT_HTTPS=1; path=/; Max-Age=300, BIDUPSID=67ECF8FD208F495FB1FC93888A3580FB; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, PSTM=1691569231; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, BAIDUID=67ECF8FD208F495F18B77C28BD39D136:FG=1; max-age=31536000; expires=Thu, 08-Aug-24 08:20:31 GMT; domain=.baidu.com; path=/; version=1; comment=bd', 'Strict-Transport-Security': 'max-age=0', 'Traceid': '1691569231141853722613530868379522833862', 'X-Ua-Compatible': 'IE=Edge,chrome=1'})
Data: b'<html>\r\n<head>\r\n\t<script>\r\n\t\tlocation.replace(location.href.replace("https://","http://"));\r\n\t</script>\r\n</head>\r\n<body>\r\n\t<noscript><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></noscript>\r\n</body>\r\n</html>'

使用连接池

连接池可以提高性能,避免频繁地建立和关闭连接。urllib3库默认使用连接池,你可以直接使用PoolManager来管理连接池:

import urllib3

http = urllib3.PoolManager()

url = "https://www.baidu.com"
response = http.request('GET', url)

print(response.data)

总结

urllib3库为Python开发者提供了一个强大的工具,用于进行各种类型的HTTP请求、处理响应以及管理连接池。本文介绍了如何使用urllib3库来发起基本的GET和POST请求,处理响应以及处理网络请求错误。

获取更多技术资料,请点击!