Have you considered storing your username and password as environmental variable on the machine that is running the script? This will prevent you from having to store the sensitive information within the script itself. Then only the machine's admin can access/modify the sensitive information.
Through cmd prompt, set the desired variables (below syntax is for a Windows machine):
SET username=domain\\user
SET password=your_password
To ensure you've correctly set the variables, type SET into the cmd prompt and see if the variables are listed.
Once correctly set, then use python's os module to access the variables and use as desired:
import os
import requests
from requests_ntlm import HttpNtlmAuth
username = os.environ.get('username')
password = os.environ.get('password')
SITE = "https://sharepointsite.com/"
response = requests.get(SITE, auth=HttpNtlmAuth(username, password))
IMPORTANT NOTES:
- If you close the
cmd prompt window, the environment variables you just set will be erased and your script will throw a "I can't find environment variables" error. To avoid this, either always keep the cmd window open while the script runs, or permanently set the environment variables (instructions here for Windows machines. Note: the instructions refer to changing the PATH environment variable but you'll get the idea on how to create/modify your own variables).
- Be careful not to overwrite an existing environment variable. First double check that a name is available by listing all variables (type
SET into cmd prompt).
- Environment variables are stored as string