将 urllib.request.urlopen 移动到请求
Moving urllib.request.urlopen to requests
我在遗留代码中有以下行,我想更改为使用 requests
:
response = urllib.request.urlopen(url, data)
我正在查看 documentation 并试图找出 urlopen
使用的是哪种 HTTP 方法,但我看不到任何相关信息。我已将此行更改为以下行,正如我最初从服务器中发现的那样:
response = requests.post(
url,
data=data,
verify=False,
headers={"Content-type": "application/x-www-form-urlencoded"},
)
当我 运行 系统测试时,我注意到如果 POST
不受支持(或相反),urlopen
也会执行 GET
请求。我理解对吗?是否有相应的请求?
我必须转到请求才能在我的测试中使用 Mocker()
。
根据to urllib documentation,您使用的方法(GET/POST)确定如下:
- "The default is 'GET' if data is None or 'POST' otherwise."
由于请求包没有对应于 urlopen
的函数可以执行 GET 或 POST,从 urlopen
迁移的简单(简单?)方法将当数据为 None 时使用 requests.get
,否则使用 requests.post
。
我在遗留代码中有以下行,我想更改为使用 requests
:
response = urllib.request.urlopen(url, data)
我正在查看 documentation 并试图找出 urlopen
使用的是哪种 HTTP 方法,但我看不到任何相关信息。我已将此行更改为以下行,正如我最初从服务器中发现的那样:
response = requests.post(
url,
data=data,
verify=False,
headers={"Content-type": "application/x-www-form-urlencoded"},
)
当我 运行 系统测试时,我注意到如果 POST
不受支持(或相反),urlopen
也会执行 GET
请求。我理解对吗?是否有相应的请求?
我必须转到请求才能在我的测试中使用 Mocker()
。
根据to urllib documentation,您使用的方法(GET/POST)确定如下:
- "The default is 'GET' if data is None or 'POST' otherwise."
由于请求包没有对应于 urlopen
的函数可以执行 GET 或 POST,从 urlopen
迁移的简单(简单?)方法将当数据为 None 时使用 requests.get
,否则使用 requests.post
。