HTTP Basic authentication

Basic access authentication assumes that the client will be identified by a username and a password. When the browser client initially accesses a site using this system, the server replies with a response of type 401, which contains the WWW-Authenticate tag with the Basic value and the name of the protected domain (such as WWW-Authenticate: Basic realm = "www.domainProtected.com").

The browser responds to the server with an Authorization tag, which contains the Basic value and the concatenation in the Base64 encoding of the login, the colon punctuation mark :, and the password (for example, Authorization : Basic b3dhc3A6cGFzc3dvcmQ =). Assuming that we have a URL protected with this type of authentication, in Python, with the Requests module, we can use the HTTPBasicAuth class.

In this script, we are using the HTTPBasicAuth class and providing the user credentials as a tuple.

You can find the following code in the basic_authentication.py file:

#!/usr/bin/env python3
import requests
from requests.auth import HTTPBasicAuth
requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'password'))
# requests provides a shorthand for this authentication method
response = requests.get('https://api.github.com/user', auth=('user', 'password'))
print('Response.status_code:'+ str(response.status_code))
if response.status_code == 200:
print('Login successful :'+response.text)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset