简体   繁体   中英

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:

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?

Use Session() . Documentation states:

The Session object allows you to persist certain parameters across requests.

import requests

s = requests.Session()
s.auth = (username, password)
s.verify = ssl_ca_file

s.get(base_url)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM