s

Python IIS 401 Unauthorized Access is denied due to invalid credentials - Solved edit button Edit

author
Murugan Andezuthu Dharmaratnam | calendar 28 December 2021 | 0

Exception

I was trying to get a simple hello world python application running on the server. I had issues setting up the IIS. I was getting this error "IIS 401 Unauthorized Access is denied due to invalid credentials You do not have permission to view this directory or page using the credentials that you supplied."

HTTP Error 401.3 - Unauthorized
You do not have permission to view this directory or page because of the access control list (ACL) configuration or encryption settings for this resource on the Web server.

Most likely causes:
The user authenticated by the Web server does not have permission to open the file on the file system.
If the resource is located on a Universal Naming Convention (UNC) share, the authenticated user may not have sufficient share and NTFS permissions, or the permissions on the share may not match the permissions on the physical path.
The file is encrypted.

Things you can try:
Open File Explorer and check the ACLs for the file that is being requested. Make sure that the user accessing the Web site is not being explicitly denied access, and that they do have permission to open the file.
Open File Explorer and check the ACLs for the share and the physical path. Ensure that both ACLs allow the user to access the resource.
Open File Explorer and check the encryption properties for the file that is being requested. (This setting is located in the Advanced attribute properties dialog.)
Create a tracing rule to track failed requests for this HTTP status code. For more information about creating a tracing rule for failed requests, click here.

Module	   CgiModule
Notification	   ExecuteRequestHandler
Handler	   Python
Error Code	   0x80070005
Requested URL	   http://localhost:80/main.py
Physical Path	   C:\inetpub\wwwroot\myapp\main.py
Logon Method	   Anonymous
Logon User	   Anonymous

More Information:
The user trying to access the page was successfully logged on, but the user does not have permission to access the resource. This means the access control list (ACL) for the resource either does not include the user or explicitly denies the user. Check the ACL for the resource and add the user to the ACL. If the content is located on a share, ensure both NTFS and share permissions allow the user access. It is also possible that the user is part of a group that is denied access.
View more information »

Solution

1.	Make sure that python installed on the server 
2.	Put the main.py application in C:\inetpub\wwwroot\myapp folder.
3.	Run main.py from command prompt and make sure that its working fine
4.	Configuring to run python on IIS 
        a.	Click on your application -> double click on Handler Mapping
        b.	Click on Add Script Map From the right menu
        c.	Request path : *.py
        d.	Executable: path to python.exe file
        e.	Name: Python
        f.	Click on OK
       g.	Click on Yes

1.	Find the folder where python is installed. In my case it was in C:\Users\muruganad\AppData\Local\Programs\Python\Python310
2.	Right click on the folder -> Select Properties & Select the Securities Tab
3.	Click On Edit 
4.	Click on Add Button
5.	Add IUSR & Click on OK
6.	Give Permission for everything in Allow & Click On Apply