[英]Python “requests” library: HTTP basic authentication for each request
In a Python script I use the "Requests" library with HTTP basic authentication and a custom CA certificate to trust like this: 在Python脚本中,我使用具有HTTP基本身份验证和自定义CA证书的“请求”库来进行信任,如下所示:
import requests
response = requests.get(base_url, auth=(username, password), verify=ssl_ca_file)
All requests I need to make have to use these parameters. 我需要发出的所有请求都必须使用这些参数。 Is there a "Python" way to set these as default for all requests? 是否有一种“ Python”方式将所有请求的默认值设置为默认值?
Use Session()
. 使用Session()
。 Documentation states: 文档状态:
The Session object allows you to persist certain parameters across requests. 通过Session对象,您可以在请求中保留某些参数。
import requests
s = requests.Session()
s.auth = (username, password)
s.verify = ssl_ca_file
s.get(base_url)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.