Robert Strauch
Robert Strauch

Reputation: 12896

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?

Upvotes: 3

Views: 4803

Answers (1)

Dušan Maďar
Dušan Maďar

Reputation: 9889

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)

Upvotes: 5

Related Questions