How do I use basic HTTP authentication with the python Requests library? How do I use basic HTTP authentication with the python Requests library? python python

How do I use basic HTTP authentication with the python Requests library?


You need to use a session object and send the authentication each request. The session will also track cookies for you:

session = requests.Session()session.auth = (user, password)auth = session.post('http://' + hostname)response = session.get('http://' + hostname + '/rest/applications')


import requestsfrom requests.auth import HTTPBasicAuthres = requests.post('https://api.github.com/user', verify=False, auth=HTTPBasicAuth('user', 'password'))print (res)

Note: sometimes, we may get SSL error certificate verify failed, to avoid, we can use verify=False


In Python3 it becomes easy:

import requestsresponse = requests.get(uri, auth=(user, password))