Search code examples
pythonpython-requestsbasic-authentication

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?


Solution

  • 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)