SNicolaou
SNicolaou

Reputation: 550

Running a Python script within IIS

I am trying to run a python script within my flask web app. Basically the user can click on a button and the script should run on the server that is hosting my flask python web app. I am hosting it on Windows Server 2016 and IIS (i know, I should consider linux)

enter image description here

When the user clicks on the button, the server executes the following script using the os.system function which is configured as below.

def run_python_script(script_path_and_arguments):    
    try:
        command = 'python {}'.format(script_path_and_arguments)
        system(command)
        return True, 'Success'
    except Exception as e:
        logger.error('Failed to run script, exception: {}'.format(e))
        return False, 'Unknown Error'

enter image description here

When I run the script locally on my workstation it executes ok. When I run the script from the server, from the command line, it runs ok, When I try to run it through the website, by clicking the button it fails.

It seems to me that it must be some permision/security issue when it runs under the IIS. Would you have any idea where I should be looking at to resolve it?

on the server the python.exe is in c:\Anaconda3\

Edit: I have now tried to capture the output by running the script with the subprocess module: it gives a plain exception "Command 'python ...' returned non-zero exit status 1"

def run_python_script(tscript_path_and_arguments):
    try:
        command = 'python {}'.format(script_path_and_arguments) 
        output_bytes = subprocess.check_output(command, stderr=subprocess.STDOUT)
        output = output_bytes.decode('utf-8')
        lines = output.split('\n')
        for line in lines:
            logger.info(msg='Script output =  {}'.format(line))
        return True, 'Success'
    except Exception as e:
        logger.error('Failed to run script, exception: {}'.format(e))
        return False, 'Unknown Error'

Upvotes: 0

Views: 2423

Answers (1)

SNicolaou
SNicolaou

Reputation: 550

it turns out that the flask app is not using the pythonpath system environment variable but the one that is configured in fastcgi settings: enter image description here

Upvotes: 1

Related Questions