I will recommend you to use Office365-REST-Python-Client, you can get the samples in github. The following code connect sharepoint with user credential.
from office365.sharepoint.client_context import ClientContext
from tests import test_site_url, settings
ctx = ClientContext(test_site_url)
ctx.with_user_credentials(settings.get('user_credentials', 'username'),
settings.get('user_credentials', 'password'))
web = ctx.web.get().execute_query()
print(web.url)
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.