'how to use MFA to login sharepoint python

I need to access a list on my SharePoint account that the MFA is already activated using python but i keep getting this error message : Failed to connect to SP site: ('Error authenticating against Office 365. Error from Office 365:', "AADSTS50076: Due to a configuration change made by your administrator, or because you moved to a new location, you must use multi-factor authentication to access ''.")

import sys 
from shareplum.site import Version 
from shareplum import Site, Office365 
AUTHOR_PAGE_URL = 'Page URL' 
SHAREPOINT_URL = 'SharePoint URL' 
SHAREPOINT_SITE = 'SharePoint Site' 
SHAREPOINT_LIST = 'List name' 
USERNAME = 'username' 
PASSWORD = 'password' 

def authenticate(sp_url, sp_site, user_name, password): 
   site = None 
   try: 
      authcookie = Office365(SHAREPOINT_URL, username=USERNAME, 
      password=PASSWORD).GetCookies() 
    site = Site(SHAREPOINT_SITE, version=Version.v365, authcookie=authcookie) 
except: 
    # We should log the specific type of error occurred. 
    print('Failed to connect to SP site: {}'.format(sys.exc_info()[1])) 
return site 

# Test the function 
sp_site = authenticate(SHAREPOINT_URL,SHAREPOINT_SITE,USERNAME,PASSWORD) 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source